Unverified Commit e9203b82 authored by Adam Reese's avatar Adam Reese Committed by GitHub

fix(helm): filter helm list to print latest release (#3335)

`helm list` should only list latest release

fixes #3208
parent 5a73f9ef
...@@ -156,7 +156,7 @@ func (l *listCmd) run() error { ...@@ -156,7 +156,7 @@ func (l *listCmd) run() error {
fmt.Fprintf(l.out, "\tnext: %s\n", res.Next) fmt.Fprintf(l.out, "\tnext: %s\n", res.Next)
} }
rels := res.Releases rels := filterList(res.Releases)
if l.short { if l.short {
for _, r := range rels { for _, r := range rels {
...@@ -168,6 +168,30 @@ func (l *listCmd) run() error { ...@@ -168,6 +168,30 @@ func (l *listCmd) run() error {
return nil 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. // statusCodes gets the list of status codes that are to be included in the results.
func (l *listCmd) statusCodes() []release.Status_Code { func (l *listCmd) statusCodes() []release.Status_Code {
if l.all { if l.all {
......
...@@ -118,6 +118,14 @@ func TestListCmd(t *testing.T) { ...@@ -118,6 +118,14 @@ func TestListCmd(t *testing.T) {
}, },
expected: "thomas-guide\nwild-idea\ncrazy-maps", 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 var buf bytes.Buffer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment