diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 2666893e8b484ca47b23528434a20244b599292b..bf543df5ebd5e8f09622c33f7c0dbf24893190fe 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -156,7 +156,7 @@ func (l *listCmd) run() error { fmt.Fprintf(l.out, "\tnext: %s\n", res.Next) } - rels := res.Releases + rels := filterList(res.Releases) if l.short { for _, r := range rels { @@ -168,6 +168,30 @@ func (l *listCmd) run() error { return nil } +// filterList returns a list scrubbed of old releases. +func filterList(rels []*release.Release) []*release.Release { + idx := map[string]int32{} + + for _, r := range rels { + name, version := r.GetName(), r.GetVersion() + if max, ok := idx[name]; ok { + // check if we have a greater version already + if max > version { + continue + } + } + idx[name] = version + } + + uniq := make([]*release.Release, 0, len(idx)) + for _, r := range rels { + if idx[r.GetName()] == r.GetVersion() { + uniq = append(uniq, r) + } + } + return uniq +} + // statusCodes gets the list of status codes that are to be included in the results. func (l *listCmd) statusCodes() []release.Status_Code { if l.all { diff --git a/cmd/helm/list_test.go b/cmd/helm/list_test.go index 587a7c39c48e585f1bc68a2c7361da5db485ce78..6e3321664c42c6a1a47b960f8f3abd49d47cf73d 100644 --- a/cmd/helm/list_test.go +++ b/cmd/helm/list_test.go @@ -118,6 +118,14 @@ func TestListCmd(t *testing.T) { }, expected: "thomas-guide\nwild-idea\ncrazy-maps", }, + { + name: "with old releases", + resp: []*release.Release{ + helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}), + helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_FAILED}), + }, + expected: "thomas-guide", + }, } var buf bytes.Buffer