Improve time display

Signed-off-by: Jinil Lee <usingsky@gmail.com>
pull/9555/head
Jinil Lee 5 years ago
parent 213a7df2dc
commit dec783f90f

@ -21,7 +21,6 @@ import (
"io" "io"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -32,7 +31,7 @@ import (
"helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/cli/output"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil" "helm.sh/helm/v3/pkg/releaseutil"
helmtime "helm.sh/helm/v3/pkg/time" "helm.sh/helm/v3/pkg/time"
) )
var historyHelp = ` var historyHelp = `
@ -44,11 +43,11 @@ configures the maximum length of the revision list returned.
The historical release set is printed as a formatted table, e.g: The historical release set is printed as a formatted table, e.g:
$ helm history angry-bird $ helm history angry-bird
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Initial install 1 Mon Oct 3 10:15:13 UTC 2016 superseded alpine-0.1.0 1.0 Initial install
2 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Upgraded successfully 2 Mon Oct 3 10:15:13 UTC 2016 superseded alpine-0.1.0 1.0 Upgraded successfully
3 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Rolled back to 2 3 Mon Oct 3 10:15:13 UTC 2016 superseded alpine-0.1.0 1.0 Rolled back to 2
4 Mon Oct 3 10:15:13 2016 deployed alpine-0.1.0 1.0 Upgraded successfully 4 Mon Oct 3 10:15:13 UTC 2016 deployed alpine-0.1.0 1.0 Upgraded successfully
` `
func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
@ -85,12 +84,12 @@ func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
} }
type releaseInfo struct { type releaseInfo struct {
Revision int `json:"revision"` Revision int `json:"revision"`
Updated helmtime.Time `json:"updated"` Updated string `json:"updated"`
Status string `json:"status"` Status string `json:"status"`
Chart string `json:"chart"` Chart string `json:"chart"`
AppVersion string `json:"app_version"` AppVersion string `json:"app_version"`
Description string `json:"description"` Description string `json:"description"`
} }
type releaseHistory []releaseInfo type releaseHistory []releaseInfo
@ -107,7 +106,7 @@ func (r releaseHistory) WriteTable(out io.Writer) error {
tbl := uitable.New() tbl := uitable.New()
tbl.AddRow("REVISION", "UPDATED", "STATUS", "CHART", "APP VERSION", "DESCRIPTION") tbl.AddRow("REVISION", "UPDATED", "STATUS", "CHART", "APP VERSION", "DESCRIPTION")
for _, item := range r { for _, item := range r {
tbl.AddRow(item.Revision, item.Updated.Format(time.ANSIC), item.Status, item.Chart, item.AppVersion, item.Description) tbl.AddRow(item.Revision, item.Updated, item.Status, item.Chart, item.AppVersion, item.Description)
} }
return output.EncodeTable(out, tbl) return output.EncodeTable(out, tbl)
} }
@ -142,18 +141,17 @@ func getReleaseHistory(rls []*release.Release) (history releaseHistory) {
v := r.Version v := r.Version
d := r.Info.Description d := r.Info.Description
a := formatAppVersion(r.Chart) a := formatAppVersion(r.Chart)
u := time.Display(r.Info.LastDeployed, time.UnixDate)
rInfo := releaseInfo{ rInfo := releaseInfo{
Revision: v, Revision: v,
Updated: u,
Status: s, Status: s,
Chart: c, Chart: c,
AppVersion: a, AppVersion: a,
Description: d, Description: d,
} }
if !r.Info.LastDeployed.IsZero() {
rInfo.Updated = r.Info.LastDeployed
}
history = append(history, rInfo) history = append(history, rInfo)
} }

@ -29,6 +29,7 @@ import (
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/cli/output"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/time"
) )
var listHelp = ` var listHelp = `
@ -156,21 +157,12 @@ func newReleaseListWriter(releases []*release.Release, timeFormat string) *relea
Name: r.Name, Name: r.Name,
Namespace: r.Namespace, Namespace: r.Namespace,
Revision: strconv.Itoa(r.Version), Revision: strconv.Itoa(r.Version),
Updated: time.Display(r.Info.LastDeployed, timeFormat),
Status: r.Info.Status.String(), Status: r.Info.Status.String(),
Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version),
AppVersion: r.Chart.Metadata.AppVersion, AppVersion: r.Chart.Metadata.AppVersion,
} }
t := "-"
if tspb := r.Info.LastDeployed; !tspb.IsZero() {
if timeFormat != "" {
t = tspb.Format(timeFormat)
} else {
t = tspb.String()
}
}
element.Updated = t
elements = append(elements, element) elements = append(elements, element)
} }
return &releaseListWriter{elements} return &releaseListWriter{elements}

@ -21,7 +21,6 @@ import (
"io" "io"
"log" "log"
"strings" "strings"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -30,6 +29,7 @@ import (
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/cli/output"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/time"
) )
// NOTE: Keep the list of statuses up-to-date with pkg/release/status.go. // NOTE: Keep the list of statuses up-to-date with pkg/release/status.go.
@ -114,9 +114,7 @@ func (s statusPrinter) WriteTable(out io.Writer) error {
return nil return nil
} }
fmt.Fprintf(out, "NAME: %s\n", s.release.Name) fmt.Fprintf(out, "NAME: %s\n", s.release.Name)
if !s.release.Info.LastDeployed.IsZero() { fmt.Fprintf(out, "LAST DEPLOYED: %s\n", time.Display(s.release.Info.LastDeployed, time.UnixDate))
fmt.Fprintf(out, "LAST DEPLOYED: %s\n", s.release.Info.LastDeployed.Format(time.ANSIC))
}
fmt.Fprintf(out, "NAMESPACE: %s\n", s.release.Namespace) fmt.Fprintf(out, "NAMESPACE: %s\n", s.release.Namespace)
fmt.Fprintf(out, "STATUS: %s\n", s.release.Info.Status.String()) fmt.Fprintf(out, "STATUS: %s\n", s.release.Info.Status.String())
fmt.Fprintf(out, "REVISION: %d\n", s.release.Version) fmt.Fprintf(out, "REVISION: %d\n", s.release.Version)
@ -135,8 +133,8 @@ func (s statusPrinter) WriteTable(out io.Writer) error {
} }
fmt.Fprintf(out, "TEST SUITE: %s\n%s\n%s\n%s\n", fmt.Fprintf(out, "TEST SUITE: %s\n%s\n%s\n%s\n",
h.Name, h.Name,
fmt.Sprintf("Last Started: %s", h.LastRun.StartedAt.Format(time.ANSIC)), fmt.Sprintf("Last Started: %s", time.Display(h.LastRun.StartedAt, time.UnixDate)),
fmt.Sprintf("Last Completed: %s", h.LastRun.CompletedAt.Format(time.ANSIC)), fmt.Sprintf("Last Completed: %s", time.Display(h.LastRun.CompletedAt, time.UnixDate)),
fmt.Sprintf("Phase: %s", h.LastRun.Phase), fmt.Sprintf("Phase: %s", h.LastRun.Phase),
) )
} }

@ -26,6 +26,24 @@ import (
"time" "time"
) )
const (
ANSIC = time.ANSIC
UnixDate = time.UnixDate
RubyDate = time.RubyDate
RFC822 = time.RFC822
RFC822Z = time.RFC822Z
RFC850 = time.RFC850
RFC1123 = time.RFC1123
RFC1123Z = time.RFC1123Z
RFC3339 = time.RFC3339
RFC3339Nano = time.RFC3339Nano
Kitchen = time.Kitchen
Stamp = time.Stamp
StampMilli = time.StampMilli
StampMicro = time.StampMicro
StampNano = time.StampNano
)
// emptyString contains an empty JSON string value to be used as output // emptyString contains an empty JSON string value to be used as output
var emptyString = `""` var emptyString = `""`
@ -89,3 +107,15 @@ 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) 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) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate(d)} }
func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} } func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} }
func Display(t Time, timeFormat string) string {
d := "-"
if !t.IsZero() {
if timeFormat != "" {
d = t.Local().Format(timeFormat)
} else {
d = t.Local().String()
}
}
return d
}

Loading…
Cancel
Save