From 0cb26ab19cf4b24aae48c08c776e5b2546e797d2 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 20 Sep 2018 09:01:21 +0900 Subject: [PATCH] Also format TTLs in non-secret responses --- command/format.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/command/format.go b/command/format.go index 2ef2556639c1..6a1afbc76466 100644 --- a/command/format.go +++ b/command/format.go @@ -131,8 +131,7 @@ func (t TableFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) case []string: return t.OutputList(ui, nil, data) case map[string]interface{}: - t.OutputMap(ui, data.(map[string]interface{})) - return nil + return t.OutputMap(ui, data.(map[string]interface{})) default: return errors.New("cannot use the table formatter for this type") } @@ -246,11 +245,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { v := secret.Data[k] // If the field "looks" like a TTL, print it as a time duration instead. - if k == "period" || strings.HasSuffix(k, "_period") || - k == "ttl" || strings.HasSuffix(k, "_ttl") || - k == "duration" || strings.HasSuffix(k, "_duration") || - k == "lease_max" || k == "ttl_max" { - + if looksLikeDuration(k) { v = humanDurationInt(v) } @@ -273,7 +268,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { return nil } -func (t TableFormatter) OutputMap(ui cli.Ui, data map[string]interface{}) { +func (t TableFormatter) OutputMap(ui cli.Ui, data map[string]interface{}) error { out := make([]string, 0, len(data)+1) if len(data) > 0 { keys := make([]string, 0, len(data)) @@ -283,14 +278,21 @@ func (t TableFormatter) OutputMap(ui cli.Ui, data map[string]interface{}) { sort.Strings(keys) for _, k := range keys { - out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, data[k])) + v := data[k] + + // If the field "looks" like a TTL, print it as a time duration instead. + if looksLikeDuration(k) { + v = humanDurationInt(v) + } + + out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, v)) } } // If we got this far and still don't have any data, there's nothing to print, // sorry. if len(out) == 0 { - return + return nil } // Prepend the header @@ -299,6 +301,7 @@ func (t TableFormatter) OutputMap(ui cli.Ui, data map[string]interface{}) { ui.Output(tableOutput(out, &columnize.Config{ Delim: hopeDelim, })) + return nil } // OutputSealStatus will print *api.SealStatusResponse in the CLI according to the format provided @@ -378,3 +381,13 @@ func OutputSealStatus(ui cli.Ui, client *api.Client, status *api.SealStatusRespo ui.Output(tableOutput(out, nil)) return 0 } + +// looksLikeDuration checks if the given key "k" looks like a duration value. +// This is used to pretty-format duration values in responses, especially from +// plugins. +func looksLikeDuration(k string) bool { + return k == "period" || strings.HasSuffix(k, "_period") || + k == "ttl" || strings.HasSuffix(k, "_ttl") || + k == "duration" || strings.HasSuffix(k, "_duration") || + k == "lease_max" || k == "ttl_max" +}