From 6e0e024d90dfdcc044def9bdffdbc3a1c533a0ed Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 11 Jul 2018 15:09:04 -0400 Subject: [PATCH] Allow lease_duration to be pulled out with -field (#4906) * Allow lease_duration to be pulled out with -field This also provides an easy way to verify that when -field is used we don't string format the value. This also changes the human string helper to accept more than one type of incoming int. * Address review feedback --- command/base_helpers.go | 17 +++++++++++++++-- command/format.go | 19 +++++-------------- command/util.go | 8 ++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/command/base_helpers.go b/command/base_helpers.go index ef629f8d3..98d2f3a93 100644 --- a/command/base_helpers.go +++ b/command/base_helpers.go @@ -1,6 +1,7 @@ package command import ( + "encoding/json" "fmt" "io" "strings" @@ -238,6 +239,18 @@ func humanDuration(d time.Duration) string { // humanDurationInt prints the given int as if it were a time.Duration number // of seconds. -func humanDurationInt(i int) string { - return humanDuration(time.Duration(i) * time.Second) +func humanDurationInt(i interface{}) interface{} { + switch i.(type) { + case int: + return humanDuration(time.Duration(i.(int)) * time.Second) + case int64: + return humanDuration(time.Duration(i.(int64)) * time.Second) + case json.Number: + if i, err := i.(json.Number).Int64(); err == nil { + return humanDuration(time.Duration(i) * time.Second) + } + } + + // If we don't know what type it is, just return the original value + return i } diff --git a/command/format.go b/command/format.go index 2f6f609f0..7e8230b08 100644 --- a/command/format.go +++ b/command/format.go @@ -196,12 +196,12 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { if secret.LeaseDuration > 0 { if secret.LeaseID != "" { out = append(out, fmt.Sprintf("lease_id %s %s", hopeDelim, secret.LeaseID)) - out = append(out, fmt.Sprintf("lease_duration %s %s", hopeDelim, humanDurationInt(secret.LeaseDuration))) + out = append(out, fmt.Sprintf("lease_duration %s %v", hopeDelim, humanDurationInt(secret.LeaseDuration))) out = append(out, fmt.Sprintf("lease_renewable %s %t", hopeDelim, secret.Renewable)) } else { // This is probably the generic secret backend which has leases, but we // print them as refresh_interval to reduce confusion. - out = append(out, fmt.Sprintf("refresh_interval %s %s", hopeDelim, humanDurationInt(secret.LeaseDuration))) + out = append(out, fmt.Sprintf("refresh_interval %s %v", hopeDelim, humanDurationInt(secret.LeaseDuration))) } } @@ -213,7 +213,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { if secret.Auth.LeaseDuration == 0 { out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, "∞")) } else { - out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, humanDurationInt(secret.Auth.LeaseDuration))) + out = append(out, fmt.Sprintf("token_duration %s %v", hopeDelim, humanDurationInt(secret.Auth.LeaseDuration))) } out = append(out, fmt.Sprintf("token_renewable %s %t", hopeDelim, secret.Auth.Renewable)) out = append(out, fmt.Sprintf("token_policies %s %q", hopeDelim, secret.Auth.TokenPolicies)) @@ -227,7 +227,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { if secret.WrapInfo != nil { out = append(out, fmt.Sprintf("wrapping_token: %s %s", hopeDelim, secret.WrapInfo.Token)) out = append(out, fmt.Sprintf("wrapping_accessor: %s %s", hopeDelim, secret.WrapInfo.Accessor)) - out = append(out, fmt.Sprintf("wrapping_token_ttl: %s %s", hopeDelim, humanDurationInt(secret.WrapInfo.TTL))) + out = append(out, fmt.Sprintf("wrapping_token_ttl: %s %v", hopeDelim, humanDurationInt(secret.WrapInfo.TTL))) out = append(out, fmt.Sprintf("wrapping_token_creation_time: %s %s", hopeDelim, secret.WrapInfo.CreationTime.String())) out = append(out, fmt.Sprintf("wrapping_token_creation_path: %s %s", hopeDelim, secret.WrapInfo.CreationPath)) if secret.WrapInfo.WrappedAccessor != "" { @@ -251,16 +251,7 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error { 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)) - } - } + v = humanDurationInt(v) } out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, v)) diff --git a/command/util.go b/command/util.go index 842d695a6..7bec6ee05 100644 --- a/command/util.go +++ b/command/util.go @@ -63,6 +63,14 @@ func RawField(secret *api.Secret, field string) interface{} { default: switch field { + case "lease_duration": + val = secret.LeaseDuration + case "lease_id": + val = secret.LeaseID + case "request_id": + val = secret.RequestID + case "renewable": + val = secret.Renewable case "refresh_interval": val = secret.LeaseDuration case "data":