feat(helm): warn on 'helm list' truncation with default --max limit

add a stderr warning when results are truncated by the default --max limit.

Closes #31617

Signed-off-by: John Weathers <weathers.johnb@gmail.com>
pull/31887/head
John Weathers 2 months ago
parent 58b8545073
commit 66aa579adc

@ -135,6 +135,8 @@ type List struct {
Failed bool
Pending bool
Selector string
// Truncated indicates results were omitted due to Limit
Truncated bool
}
// NewList constructs a new *List
@ -213,7 +215,7 @@ func (l *List) Run() ([]ri.Releaser, error) {
}
// Calculate the limit and offset, and then truncate results if necessary.
limit := len(results)
limit := len(rresults)
if l.Limit > 0 && l.Limit < limit {
limit = l.Limit
}
@ -221,6 +223,8 @@ func (l *List) Run() ([]ri.Releaser, error) {
if l := len(rresults); l < last {
last = l
}
l.Truncated = last < len(rresults)
rresults = rresults[l.Offset:last]
return releaseV1ListToReleaserList(rresults)

@ -175,6 +175,55 @@ func TestList_LimitOffsetOutOfBounds(t *testing.T) {
is.Len(list, 2)
}
func TestList_Truncation(t *testing.T) {
is := assert.New(t)
// Create 3 releases
lister := newListFixture(t)
makeMeSomeReleases(t, lister.cfg.Releases)
lister.Limit = 5
lister.Offset = 0
list, err := lister.Run()
is.NoError(err)
is.Len(list, 3)
is.False(lister.Truncated, "Expected Truncated to be false when limit > total")
lister.Limit = 3
lister.Offset = 0
list, err = lister.Run()
is.NoError(err)
is.Len(list, 3)
is.False(lister.Truncated, "Expected Truncated to be false when limit == total")
lister.Limit = 2
lister.Offset = 0
list, err = lister.Run()
is.NoError(err)
is.Len(list, 2)
is.True(lister.Truncated, "Expected Truncated to be true when limit < total")
lister.Limit = 1
lister.Offset = 1
list, err = lister.Run()
is.NoError(err)
is.Len(list, 1)
is.True(lister.Truncated, "Expected Truncated to be true when offset + limit < total")
lister.Limit = 1
lister.Offset = 2
list, err = lister.Run()
is.NoError(err)
is.Len(list, 1)
is.False(lister.Truncated, "Expected Truncated to be false when offset + limit == total")
lister.Limit = 1
lister.Offset = 3
list, err = lister.Run()
is.NoError(err)
is.Len(list, 0)
is.False(lister.Truncated, "Expected Truncated to be false when offset >= total")
}
func TestList_StateMask(t *testing.T) {
is := assert.New(t)
lister := newListFixture(t)

@ -113,7 +113,16 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}
}
return outfmt.Write(out, newReleaseListWriter(results, client.TimeFormat, client.NoHeaders, settings.ShouldDisableColor()))
if err := outfmt.Write(out, newReleaseListWriter(results, client.TimeFormat, client.NoHeaders, settings.ShouldDisableColor())); err != nil {
return err
}
outputFlag := cmd.Flag("output")
if outputFlag.Value.String() == "table" && client.Truncated && !cmd.Flags().Changed("max") {
fmt.Fprintln(os.Stderr, "WARNING: results were truncated due to the default --max limit. Use --max to show more releases.")
}
return nil
},
}

Loading…
Cancel
Save