diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 7ed340482..02000ca1d 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -66,7 +66,6 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { if client.AllNamespaces { client.SetConfiguration(newActionConfig(true)) } - client.All = client.Limit == -1 client.SetStateMask() results, err := client.Run() @@ -86,8 +85,8 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f := cmd.Flags() f.BoolVarP(&client.Short, "short", "q", false, "output short (quiet) listing format") f.BoolVarP(&client.ByDate, "date", "d", false, "sort by release date") - f.BoolVarP(&client.SortDesc, "reverse", "r", false, "reverse the sort order") - f.BoolVarP(&client.All, "all", "a", false, "show all releases, not just the ones marked deployed") + f.BoolVarP(&client.SortReverse, "reverse", "r", false, "reverse the sort order") + f.BoolVarP(&client.All, "all", "a", false, "show all releases, not just the ones marked deployed or failed") f.BoolVar(&client.Uninstalled, "uninstalled", false, "show uninstalled releases") f.BoolVar(&client.Superseded, "superseded", false, "show superseded releases") f.BoolVar(&client.Uninstalling, "uninstalling", false, "show releases that are currently being uninstalled") diff --git a/cmd/helm/list_test.go b/cmd/helm/list_test.go index 08dff52aa..8939119d8 100644 --- a/cmd/helm/list_test.go +++ b/cmd/helm/list_test.go @@ -26,14 +26,19 @@ import ( func TestListCmd(t *testing.T) { defaultNamespace := "default" - timestamp1 := time.Unix(1452902400, 0).UTC() - timestamp2 := time.Unix(1452902401, 0).UTC() + + sampleTimeSeconds := int64(1452902400) + timestamp1 := time.Unix(sampleTimeSeconds+1, 0).UTC() + timestamp2 := time.Unix(sampleTimeSeconds+2, 0).UTC() + timestamp3 := time.Unix(sampleTimeSeconds+3, 0).UTC() + timestamp4 := time.Unix(sampleTimeSeconds+4, 0).UTC() chartInfo := &chart.Chart{ Metadata: &chart.Metadata{ Name: "chickadee", Version: "1.0.0", }, } + releaseFixture := []*release.Release{ { Name: "starlord", @@ -60,7 +65,7 @@ func TestListCmd(t *testing.T) { Version: 1, Namespace: defaultNamespace, Info: &release.Info{ - LastDeployed: timestamp2, + LastDeployed: timestamp1, Status: release.StatusUninstalled, }, Chart: chartInfo, @@ -105,6 +110,26 @@ func TestListCmd(t *testing.T) { }, Chart: chartInfo, }, + { + Name: "hummingbird", + Version: 1, + Namespace: defaultNamespace, + Info: &release.Info{ + LastDeployed: timestamp3, + Status: release.StatusDeployed, + }, + Chart: chartInfo, + }, + { + Name: "iguana", + Version: 2, + Namespace: defaultNamespace, + Info: &release.Info{ + LastDeployed: timestamp4, + Status: release.StatusDeployed, + }, + Chart: chartInfo, + }, } tests := []cmdTestCase{{ @@ -152,6 +177,11 @@ func TestListCmd(t *testing.T) { cmd: "list --reverse", golden: "output/list-reverse.txt", rels: releaseFixture, + }, { + name: "list releases sorted by reversed release date", + cmd: "list --date --reverse", + golden: "output/list-date-reversed.txt", + rels: releaseFixture, }, { name: "list releases in short output format", cmd: "list --short", diff --git a/cmd/helm/testdata/output/list-all.txt b/cmd/helm/testdata/output/list-all.txt index c93262d24..1872236fd 100644 --- a/cmd/helm/testdata/output/list-all.txt +++ b/cmd/helm/testdata/output/list-all.txt @@ -1,3 +1,10 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +drax default 1 2016-01-16 00:00:01 +0000 UTC uninstalling chickadee-1.0.0 +gamora default 1 2016-01-16 00:00:01 +0000 UTC superseded chickadee-1.0.0 +groot default 1 2016-01-16 00:00:01 +0000 UTC uninstalled chickadee-1.0.0 +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +starlord default 1 2016-01-16 00:00:01 +0000 UTC superseded chickadee-1.0.0 +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 +thanos default 1 2016-01-16 00:00:01 +0000 UTC pending-install chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-date-reversed.txt b/cmd/helm/testdata/output/list-date-reversed.txt new file mode 100644 index 000000000..5785d4d3f --- /dev/null +++ b/cmd/helm/testdata/output/list-date-reversed.txt @@ -0,0 +1,5 @@ +NAME NAMESPACE REVISION UPDATED STATUS CHART +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-date.txt b/cmd/helm/testdata/output/list-date.txt index c93262d24..77c8132cd 100644 --- a/cmd/helm/testdata/output/list-date.txt +++ b/cmd/helm/testdata/output/list-date.txt @@ -1,3 +1,5 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-failed.txt b/cmd/helm/testdata/output/list-failed.txt index f9e56e313..f87bbe18f 100644 --- a/cmd/helm/testdata/output/list-failed.txt +++ b/cmd/helm/testdata/output/list-failed.txt @@ -1,2 +1,2 @@ NAME NAMESPACE REVISION UPDATED STATUS CHART -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-filter.txt b/cmd/helm/testdata/output/list-filter.txt index c93262d24..55a4ac8af 100644 --- a/cmd/helm/testdata/output/list-filter.txt +++ b/cmd/helm/testdata/output/list-filter.txt @@ -1,3 +1,5 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-max.txt b/cmd/helm/testdata/output/list-max.txt index 8d93a9a09..34f64136c 100644 --- a/cmd/helm/testdata/output/list-max.txt +++ b/cmd/helm/testdata/output/list-max.txt @@ -1,2 +1,2 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-offset.txt b/cmd/helm/testdata/output/list-offset.txt index f9e56e313..e97f020ab 100644 --- a/cmd/helm/testdata/output/list-offset.txt +++ b/cmd/helm/testdata/output/list-offset.txt @@ -1,2 +1,4 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-pending.txt b/cmd/helm/testdata/output/list-pending.txt index 1a03b12dd..1cb7f8411 100644 --- a/cmd/helm/testdata/output/list-pending.txt +++ b/cmd/helm/testdata/output/list-pending.txt @@ -1,2 +1,2 @@ NAME NAMESPACE REVISION UPDATED STATUS CHART -thanos default 1 2016-01-16 00:00:00 +0000 UTC pending-install chickadee-1.0.0 +thanos default 1 2016-01-16 00:00:01 +0000 UTC pending-install chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-reverse.txt b/cmd/helm/testdata/output/list-reverse.txt index c93262d24..7df0baadb 100644 --- a/cmd/helm/testdata/output/list-reverse.txt +++ b/cmd/helm/testdata/output/list-reverse.txt @@ -1,3 +1,5 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-short.txt b/cmd/helm/testdata/output/list-short.txt index 5f7151e5e..0a63be990 100644 --- a/cmd/helm/testdata/output/list-short.txt +++ b/cmd/helm/testdata/output/list-short.txt @@ -1,2 +1,4 @@ -starlord +hummingbird +iguana rocket +starlord diff --git a/cmd/helm/testdata/output/list-superseded.txt b/cmd/helm/testdata/output/list-superseded.txt index c93262d24..e83a8b797 100644 --- a/cmd/helm/testdata/output/list-superseded.txt +++ b/cmd/helm/testdata/output/list-superseded.txt @@ -1,3 +1,3 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +gamora default 1 2016-01-16 00:00:01 +0000 UTC superseded chickadee-1.0.0 +starlord default 1 2016-01-16 00:00:01 +0000 UTC superseded chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list-uninstalling.txt b/cmd/helm/testdata/output/list-uninstalling.txt index 99c634c11..a90771361 100644 --- a/cmd/helm/testdata/output/list-uninstalling.txt +++ b/cmd/helm/testdata/output/list-uninstalling.txt @@ -1,2 +1,2 @@ NAME NAMESPACE REVISION UPDATED STATUS CHART -drax default 1 2016-01-16 00:00:00 +0000 UTC uninstalling chickadee-1.0.0 +drax default 1 2016-01-16 00:00:01 +0000 UTC uninstalling chickadee-1.0.0 diff --git a/cmd/helm/testdata/output/list.txt b/cmd/helm/testdata/output/list.txt index c93262d24..55a4ac8af 100644 --- a/cmd/helm/testdata/output/list.txt +++ b/cmd/helm/testdata/output/list.txt @@ -1,3 +1,5 @@ -NAME NAMESPACE REVISION UPDATED STATUS CHART -starlord default 2 2016-01-16 00:00:00 +0000 UTC deployed chickadee-1.0.0 -rocket default 1 2016-01-16 00:00:01 +0000 UTC failed chickadee-1.0.0 +NAME NAMESPACE REVISION UPDATED STATUS CHART +hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 +iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 +rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 +starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 diff --git a/pkg/action/list.go b/pkg/action/list.go index 063b4d28e..b42cc812c 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -87,12 +87,12 @@ const ListAll = ListDeployed | ListUninstalled | ListUninstalling | ListPendingI type Sorter uint const ( - // ByDate sorts by date - ByDate Sorter = iota - // ByNameAsc sorts by ascending lexicographic order - ByNameAsc // ByNameDesc sorts by descending lexicographic order - ByNameDesc + ByNameDesc Sorter = iota + 1 + // ByDateAsc sorts by ascending dates (oldest updated release first) + ByDateAsc + // ByDateDesc sorts by descending dates (latest updated release first) + ByDateDesc ) // List is the action for listing releases. @@ -109,6 +109,9 @@ type List struct { // // see pkg/releaseutil for several useful sorters Sort Sorter + // Overrides the default lexicographic sorting + ByDate bool + SortReverse bool // StateMask accepts a bitmask of states for items to show. // The default is ListDeployed StateMask ListStates @@ -119,8 +122,6 @@ type List struct { // Filter is a filter that is applied to the results Filter string Short bool - ByDate bool - SortDesc bool Uninstalled bool Superseded bool Uninstalling bool @@ -194,9 +195,22 @@ func (l *List) Run() ([]*release.Release, error) { // sort is an in-place sort where order is based on the value of a.Sort func (l *List) sort(rels []*release.Release) { + if l.SortReverse { + l.Sort = ByNameDesc + } + + if l.ByDate { + l.Sort = ByDateDesc + if l.SortReverse { + l.Sort = ByDateAsc + } + } + switch l.Sort { - case ByDate: + case ByDateDesc: releaseutil.SortByDate(rels) + case ByDateAsc: + releaseutil.Reverse(rels, releaseutil.SortByDate) case ByNameDesc: releaseutil.Reverse(rels, releaseutil.SortByName) default: @@ -227,6 +241,9 @@ func (l *List) SetStateMask() { if l.Failed { state |= ListFailed } + if l.Superseded { + state |= ListSuperseded + } // Apply a default if state == 0 { diff --git a/pkg/action/list_test.go b/pkg/action/list_test.go index cb62f0e43..48e9a376a 100644 --- a/pkg/action/list_test.go +++ b/pkg/action/list_test.go @@ -81,6 +81,7 @@ func TestList_AllNamespaces(t *testing.T) { lister := newListFixture(t) makeMeSomeReleases(lister.cfg.Releases, t) lister.AllNamespaces = true + lister.SetStateMask() list, err := lister.Run() is.NoError(err) is.Len(list, 3) @@ -103,13 +104,10 @@ func TestList_Limit(t *testing.T) { is := assert.New(t) lister := newListFixture(t) lister.Limit = 2 - // Sort because otherwise there is no guaranteed order - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) list, err := lister.Run() is.NoError(err) is.Len(list, 2) - // Lex order means one, three, two is.Equal("one", list[0].Name) is.Equal("three", list[1].Name) @@ -119,8 +117,6 @@ func TestList_BigLimit(t *testing.T) { is := assert.New(t) lister := newListFixture(t) lister.Limit = 20 - // Sort because otherwise there is no guaranteed order - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) list, err := lister.Run() is.NoError(err) @@ -137,8 +133,6 @@ func TestList_LimitOffset(t *testing.T) { lister := newListFixture(t) lister.Limit = 2 lister.Offset = 1 - // Sort because otherwise there is no guaranteed order - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) list, err := lister.Run() is.NoError(err) @@ -154,8 +148,6 @@ func TestList_LimitOffsetOutOfBounds(t *testing.T) { lister := newListFixture(t) lister.Limit = 2 lister.Offset = 3 // Last item is index 2 - // Sort because otherwise there is no guaranteed order - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) list, err := lister.Run() is.NoError(err) @@ -170,8 +162,6 @@ func TestList_LimitOffsetOutOfBounds(t *testing.T) { func TestList_StateMask(t *testing.T) { is := assert.New(t) lister := newListFixture(t) - // Sort because otherwise there is no guaranteed order - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) one, err := lister.cfg.Releases.Get("one", 1) is.NoError(err) @@ -200,7 +190,6 @@ func TestList_Filter(t *testing.T) { is := assert.New(t) lister := newListFixture(t) lister.Filter = "th." - lister.Sort = ByNameAsc makeMeSomeReleases(lister.cfg.Releases, t) res, err := lister.Run() @@ -233,6 +222,11 @@ func makeMeSomeReleases(store *storage.Storage, t *testing.T) { three.Name = "three" three.Namespace = "default" three.Version = 3 + four := releaseStub() + four.Name = "four" + four.Namespace = "default" + four.Version = 4 + four.Info.Status = release.StatusSuperseded for _, rel := range []*release.Release{one, two, three} { if err := store.Create(rel); err != nil { diff --git a/pkg/releaseutil/sorter.go b/pkg/releaseutil/sorter.go index 35506f268..f500aeea3 100644 --- a/pkg/releaseutil/sorter.go +++ b/pkg/releaseutil/sorter.go @@ -38,8 +38,8 @@ type ByDate struct{ list } // Less compares to releases func (s ByDate) Less(i, j int) bool { - ti := s.list[i].Info.LastDeployed.Second() - tj := s.list[j].Info.LastDeployed.Second() + ti := s.list[i].Info.LastDeployed.Unix() + tj := s.list[j].Info.LastDeployed.Unix() return ti < tj }