From f61332f379fb8734e8124435de416a6fcf3338a2 Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Thu, 25 Jun 2020 19:10:30 +0200 Subject: [PATCH 1/6] add time-format flag to list command Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 59 ++++++++++++++++++++++++++++++------------- pkg/action/list.go | 1 + pkg/time/time.go | 5 ++++ pkg/time/time_test.go | 9 +++++++ 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 08a26be04..5b9ba8769 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -29,6 +29,7 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/release" + helmtime "helm.sh/helm/v3/pkg/time" ) var listHelp = ` @@ -46,8 +47,8 @@ regular expressions (Perl compatible) that are applied to the list of releases. Only items that match the filter will be returned. $ helm list --filter 'ara[a-z]+' - NAME UPDATED CHART - maudlin-arachnid Mon May 9 16:07:08 2016 alpine-0.1.0 + NAME UPDATED CHART + maudlin-arachnid 1977-09-02 22:04:05 +0000 UTC alpine-0.1.0 If no results are found, 'helm list' will exit 0, but with no output (or in the case of no '-q' flag, only headers). @@ -104,16 +105,17 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { } return nil default: - return outfmt.Write(out, newReleaseListWriter(results)) + return outfmt.Write(out, newReleaseListWriter(results, client.FormatTime)) } } - return outfmt.Write(out, newReleaseListWriter(results)) + return outfmt.Write(out, newReleaseListWriter(results, client.FormatTime)) }, } f := cmd.Flags() f.BoolVarP(&client.Short, "short", "q", false, "output short (quiet) listing format") + f.BoolVar(&client.FormatTime, "format-time", false, "format time") f.BoolVarP(&client.ByDate, "date", "d", false, "sort by release date") f.BoolVarP(&client.SortReverse, "reverse", "r", false, "reverse the sort order") f.BoolVarP(&client.All, "all", "a", false, "show all releases without any filter applied") @@ -147,28 +149,51 @@ type releaseListWriter struct { releases []releaseElement } -func newReleaseListWriter(releases []*release.Release) *releaseListWriter { +func newReleaseListWriter(releases []*release.Release, formatTime bool) *releaseListWriter { // Initialize the array so no results returns an empty array instead of null elements := make([]releaseElement, 0, len(releases)) for _, r := range releases { - element := releaseElement{ - Name: r.Name, - Namespace: r.Namespace, - Revision: strconv.Itoa(r.Version), - Status: r.Info.Status.String(), - Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), - AppVersion: r.Chart.Metadata.AppVersion, - } - t := "-" - if tspb := r.Info.LastDeployed; !tspb.IsZero() { - t = tspb.String() + var element releaseElement + + if formatTime { + element = timeFormattedElement(r) + } else { + element = releaseElement{ + Name: r.Name, + Namespace: r.Namespace, + Revision: strconv.Itoa(r.Version), + Status: r.Info.Status.String(), + Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), + AppVersion: r.Chart.Metadata.AppVersion, + } + t := "-" + if tspb := r.Info.LastDeployed; !tspb.IsZero() { + t = tspb.String() + } + element.Updated = t } - element.Updated = t + elements = append(elements, element) } return &releaseListWriter{elements} } +func timeFormattedElement(r *release.Release) releaseElement { + t := "-" + if tspb := r.Info.LastDeployed; !tspb.IsZero() { + t = helmtime.Format(tspb) + } + return releaseElement{ + Name: r.Name, + Namespace: r.Namespace, + Revision: strconv.Itoa(r.Version), + Updated: t, + Status: r.Info.Status.String(), + Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), + AppVersion: r.Chart.Metadata.AppVersion, + } +} + func (r *releaseListWriter) WriteTable(out io.Writer) error { table := uitable.New() table.AddRow("NAME", "NAMESPACE", "REVISION", "UPDATED", "STATUS", "CHART", "APP VERSION") diff --git a/pkg/action/list.go b/pkg/action/list.go index 5ba0c4770..c4686f2c8 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -122,6 +122,7 @@ type List struct { // Filter is a filter that is applied to the results Filter string Short bool + FormatTime bool Uninstalled bool Superseded bool Uninstalling bool diff --git a/pkg/time/time.go b/pkg/time/time.go index 44f3fedfb..93ebc8520 100644 --- a/pkg/time/time.go +++ b/pkg/time/time.go @@ -89,3 +89,8 @@ func (t Time) Round(d time.Duration) Time { return Time{Time: t.Time.Round(d) func (t Time) Sub(u Time) time.Duration { return t.Time.Sub(u.Time) } func (t Time) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate(d)} } func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} } + +// Format formats time in custom output format +func Format(t Time) string { + return t.Format("2006-01-02 15:04:05 -0700 MST") +} diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go index 20f0f8e29..c1eb4ba77 100644 --- a/pkg/time/time_test.go +++ b/pkg/time/time_test.go @@ -81,3 +81,12 @@ func TestZeroValueUnmarshal(t *testing.T) { t.Errorf("expected time to be equal to zero value, got %v", myTime) } } + +func TestFormat(t *testing.T) { + when := Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) + expected := "2009-11-17 20:34:58 +0000 UTC" + got := Format(when) + if expected != got { + t.Errorf("expected %s, got %s", expected, got) + } +} From 78177de664942452485aee890ddf07fa1cf4e420 Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Thu, 25 Jun 2020 19:13:01 +0200 Subject: [PATCH 2/6] fix ls command example Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 5b9ba8769..957033714 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -47,8 +47,8 @@ regular expressions (Perl compatible) that are applied to the list of releases. Only items that match the filter will be returned. $ helm list --filter 'ara[a-z]+' - NAME UPDATED CHART - maudlin-arachnid 1977-09-02 22:04:05 +0000 UTC alpine-0.1.0 + NAME UPDATED CHART + maudlin-arachnid 1977-09-02 22:04:05.46104 +0000 UTC alpine-0.1.0 If no results are found, 'helm list' will exit 0, but with no output (or in the case of no '-q' flag, only headers). From 190e0b4a818df967215ad26a9cebdf114795614a Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Wed, 23 Sep 2020 19:28:57 +0200 Subject: [PATCH 3/6] refactor time formatting Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 957033714..4d65e824b 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -153,47 +153,30 @@ func newReleaseListWriter(releases []*release.Release, formatTime bool) *release // Initialize the array so no results returns an empty array instead of null elements := make([]releaseElement, 0, len(releases)) for _, r := range releases { - var element releaseElement - - if formatTime { - element = timeFormattedElement(r) - } else { - element = releaseElement{ - Name: r.Name, - Namespace: r.Namespace, - Revision: strconv.Itoa(r.Version), - Status: r.Info.Status.String(), - Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), - AppVersion: r.Chart.Metadata.AppVersion, - } - t := "-" - if tspb := r.Info.LastDeployed; !tspb.IsZero() { + element := releaseElement{ + Name: r.Name, + Namespace: r.Namespace, + Revision: strconv.Itoa(r.Version), + Status: r.Info.Status.String(), + Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), + AppVersion: r.Chart.Metadata.AppVersion, + } + + t := "-" + if tspb := r.Info.LastDeployed; !tspb.IsZero() { + if formatTime { + t = helmtime.Format(tspb) + } else { t = tspb.String() } - element.Updated = t } + element.Updated = t elements = append(elements, element) } return &releaseListWriter{elements} } -func timeFormattedElement(r *release.Release) releaseElement { - t := "-" - if tspb := r.Info.LastDeployed; !tspb.IsZero() { - t = helmtime.Format(tspb) - } - return releaseElement{ - Name: r.Name, - Namespace: r.Namespace, - Revision: strconv.Itoa(r.Version), - Updated: t, - Status: r.Info.Status.String(), - Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), - AppVersion: r.Chart.Metadata.AppVersion, - } -} - func (r *releaseListWriter) WriteTable(out io.Writer) error { table := uitable.New() table.AddRow("NAME", "NAMESPACE", "REVISION", "UPDATED", "STATUS", "CHART", "APP VERSION") From c5e9732a9fdda5c1bb74a1d29827cf60158d218c Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Wed, 23 Sep 2020 19:50:29 +0200 Subject: [PATCH 4/6] rename to time format flag Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 12 ++++++------ pkg/action/list.go | 2 +- pkg/time/time.go | 4 ++-- pkg/time/time_test.go | 9 ++++++++- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 4d65e824b..87f7f2ecc 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -105,17 +105,17 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { } return nil default: - return outfmt.Write(out, newReleaseListWriter(results, client.FormatTime)) + return outfmt.Write(out, newReleaseListWriter(results, client.TimeFormat)) } } - return outfmt.Write(out, newReleaseListWriter(results, client.FormatTime)) + return outfmt.Write(out, newReleaseListWriter(results, client.TimeFormat)) }, } f := cmd.Flags() f.BoolVarP(&client.Short, "short", "q", false, "output short (quiet) listing format") - f.BoolVar(&client.FormatTime, "format-time", false, "format time") + f.StringVar(&client.TimeFormat, "time-format", "", "format time. Example: --time-format 2009-11-17 20:34:10 +0000 UTC") f.BoolVarP(&client.ByDate, "date", "d", false, "sort by release date") f.BoolVarP(&client.SortReverse, "reverse", "r", false, "reverse the sort order") f.BoolVarP(&client.All, "all", "a", false, "show all releases without any filter applied") @@ -149,7 +149,7 @@ type releaseListWriter struct { releases []releaseElement } -func newReleaseListWriter(releases []*release.Release, formatTime bool) *releaseListWriter { +func newReleaseListWriter(releases []*release.Release, timeFormat string) *releaseListWriter { // Initialize the array so no results returns an empty array instead of null elements := make([]releaseElement, 0, len(releases)) for _, r := range releases { @@ -164,8 +164,8 @@ func newReleaseListWriter(releases []*release.Release, formatTime bool) *release t := "-" if tspb := r.Info.LastDeployed; !tspb.IsZero() { - if formatTime { - t = helmtime.Format(tspb) + if timeFormat != "" { + t = helmtime.Format(tspb, timeFormat) } else { t = tspb.String() } diff --git a/pkg/action/list.go b/pkg/action/list.go index c4686f2c8..ebbc56b01 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -122,7 +122,7 @@ type List struct { // Filter is a filter that is applied to the results Filter string Short bool - FormatTime bool + TimeFormat string Uninstalled bool Superseded bool Uninstalling bool diff --git a/pkg/time/time.go b/pkg/time/time.go index 93ebc8520..3309da735 100644 --- a/pkg/time/time.go +++ b/pkg/time/time.go @@ -91,6 +91,6 @@ func (t Time) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} } // Format formats time in custom output format -func Format(t Time) string { - return t.Format("2006-01-02 15:04:05 -0700 MST") +func Format(t Time, format string) string { + return t.Format(format) } diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go index c1eb4ba77..35c719033 100644 --- a/pkg/time/time_test.go +++ b/pkg/time/time_test.go @@ -84,8 +84,15 @@ func TestZeroValueUnmarshal(t *testing.T) { func TestFormat(t *testing.T) { when := Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) + expected := "2009-11-17 20:34:58 +0000 UTC" - got := Format(when) + got := Format(when, "2006-01-02 15:04:05 -0700 MST") + if expected != got { + t.Errorf("expected %s, got %s", expected, got) + } + + expected = "2009-11-17 20:34 +0000 UTC" + got = Format(when, "2006-01-02 15:04 -0700 MST") if expected != got { t.Errorf("expected %s, got %s", expected, got) } From b17cf19a2eab5664375c8578531940cafe4aa035 Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Wed, 23 Sep 2020 20:03:30 +0200 Subject: [PATCH 5/6] fix example time format Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 87f7f2ecc..cec17a245 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -47,8 +47,8 @@ regular expressions (Perl compatible) that are applied to the list of releases. Only items that match the filter will be returned. $ helm list --filter 'ara[a-z]+' - NAME UPDATED CHART - maudlin-arachnid 1977-09-02 22:04:05.46104 +0000 UTC alpine-0.1.0 + NAME UPDATED CHART + maudlin-arachnid 2020-06-18 14:17:46.125134977 +0000 UTC alpine-0.1.0 If no results are found, 'helm list' will exit 0, but with no output (or in the case of no '-q' flag, only headers). From 3ca46f3b2370f5f2f571af0f0eba6575a91e8729 Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Wed, 23 Sep 2020 23:32:56 +0200 Subject: [PATCH 6/6] remove redudant time func Signed-off-by: Abhilash Gnan --- cmd/helm/list.go | 3 +-- pkg/time/time.go | 5 ----- pkg/time/time_test.go | 16 ---------------- 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index cec17a245..b71278c7c 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -29,7 +29,6 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/release" - helmtime "helm.sh/helm/v3/pkg/time" ) var listHelp = ` @@ -165,7 +164,7 @@ func newReleaseListWriter(releases []*release.Release, timeFormat string) *relea t := "-" if tspb := r.Info.LastDeployed; !tspb.IsZero() { if timeFormat != "" { - t = helmtime.Format(tspb, timeFormat) + t = tspb.Format(timeFormat) } else { t = tspb.String() } diff --git a/pkg/time/time.go b/pkg/time/time.go index 3309da735..44f3fedfb 100644 --- a/pkg/time/time.go +++ b/pkg/time/time.go @@ -89,8 +89,3 @@ func (t Time) Round(d time.Duration) Time { return Time{Time: t.Time.Round(d) func (t Time) Sub(u Time) time.Duration { return t.Time.Sub(u.Time) } func (t Time) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate(d)} } func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} } - -// Format formats time in custom output format -func Format(t Time, format string) string { - return t.Format(format) -} diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go index 35c719033..20f0f8e29 100644 --- a/pkg/time/time_test.go +++ b/pkg/time/time_test.go @@ -81,19 +81,3 @@ func TestZeroValueUnmarshal(t *testing.T) { t.Errorf("expected time to be equal to zero value, got %v", myTime) } } - -func TestFormat(t *testing.T) { - when := Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) - - expected := "2009-11-17 20:34:58 +0000 UTC" - got := Format(when, "2006-01-02 15:04:05 -0700 MST") - if expected != got { - t.Errorf("expected %s, got %s", expected, got) - } - - expected = "2009-11-17 20:34 +0000 UTC" - got = Format(when, "2006-01-02 15:04 -0700 MST") - if expected != got { - t.Errorf("expected %s, got %s", expected, got) - } -}