From dffd687fd8200346551e008f7bd9907fd45acbde Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 11 Jul 2018 12:07:48 -0400 Subject: [PATCH] Special case handle TTLs from JSON responses (#4893) --- command/format.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/command/format.go b/command/format.go index b6dd9c78c5..2f6f609f05 100644 --- a/command/format.go +++ b/command/format.go @@ -100,8 +100,7 @@ func (j JsonFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) e } // An output formatter for yaml output format of an object -type YamlFormatter struct { -} +type YamlFormatter struct{} func (y YamlFormatter) Format(data interface{}) ([]byte, error) { return yaml.Marshal(data) @@ -116,8 +115,7 @@ func (y YamlFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) e } // An output formatter for table output of an object -type TableFormatter struct { -} +type TableFormatter struct{} // We don't use this func (t TableFormatter) Format(data interface{}) ([]byte, error) { @@ -245,7 +243,27 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { sort.Strings(keys) for _, k := range keys { - out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, secret.Data[k])) + 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" { + + switch t := v.(type) { + case int: + v = humanDurationInt(t) + case int64: + v = humanDurationInt(int(t)) + case json.Number: + if i, err := t.Int64(); err == nil { + v = humanDurationInt(int(i)) + } + } + } + + out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, v)) } }