Skip to content

Commit

Permalink
Also format TTLs in non-secret responses (#5367)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored and chrishoffman committed Sep 21, 2018
1 parent 9dc2a85 commit 03e24be
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
}

0 comments on commit 03e24be

Please sign in to comment.