diff --git a/pkg/action/list.go b/pkg/action/list.go index 06727bd9a..e254a4977 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -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 @@ -151,6 +153,8 @@ func (l *List) Run() ([]ri.Releaser, error) { return nil, err } + l.Truncated = false + var filter *regexp.Regexp if l.Filter != "" { var err error @@ -213,7 +217,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 +225,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) diff --git a/pkg/action/list_test.go b/pkg/action/list_test.go index 643bcea42..e35b1f15c 100644 --- a/pkg/action/list_test.go +++ b/pkg/action/list_test.go @@ -175,6 +175,56 @@ 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") + + // Early return path should reset Truncated even after a truncated run + 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") + + 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") +} + func TestList_StateMask(t *testing.T) { is := assert.New(t) lister := newListFixture(t) diff --git a/pkg/cmd/list.go b/pkg/cmd/list.go index 54becddca..a28a81ce5 100644 --- a/pkg/cmd/list.go +++ b/pkg/cmd/list.go @@ -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(cmd.ErrOrStderr(), "WARNING: results were truncated due to the default --max limit. Use --max to show more releases.") + } + + return nil }, }