From 409b9f9b0fcc8b80a2c04752b94bf394209e7feb Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 27 Jun 2016 23:19:09 -0400 Subject: [PATCH] Add aliases for field flag to allow printing auth results. Also fix the write command to use the shared function with aliases. Fixes #1566 --- command/util.go | 47 ++++++++++++++++++++++++++++++++--------------- command/write.go | 19 +------------------ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/command/util.go b/command/util.go index fc10487d39..6b50aa6a05 100644 --- a/command/util.go +++ b/command/util.go @@ -31,27 +31,44 @@ func DefaultTokenHelper() (token.TokenHelper, error) { func PrintRawField(ui cli.Ui, secret *api.Secret, field string) int { var val interface{} - switch field { - case "wrapping_token": - if secret.WrapInfo != nil { + switch { + case secret.Auth != nil: + switch field { + case "token": + val = secret.Auth.ClientToken + case "token_accessor": + val = secret.Auth.Accessor + case "token_duration": + val = secret.Auth.LeaseDuration + case "token_renewable": + val = secret.Auth.Renewable + case "token_policies": + val = secret.Auth.Policies + default: + val = secret.Data[field] + } + + case secret.WrapInfo != nil: + switch field { + case "wrapping_token": val = secret.WrapInfo.Token - } - case "wrapping_token_ttl": - if secret.WrapInfo != nil { + case "wrapping_token_ttl": val = secret.WrapInfo.TTL - } - case "wrapping_token_creation_time": - if secret.WrapInfo != nil { + case "wrapping_token_creation_time": val = secret.WrapInfo.CreationTime.String() - } - case "wrapped_accessor": - if secret.WrapInfo != nil { + case "wrapped_accessor": val = secret.WrapInfo.WrappedAccessor + default: + val = secret.Data[field] } - case "refresh_interval": - val = secret.LeaseDuration + default: - val = secret.Data[field] + switch field { + case "refresh_interval": + val = secret.LeaseDuration + default: + val = secret.Data[field] + } } if val != nil { diff --git a/command/write.go b/command/write.go index 0342f5dbb7..cf67a8b665 100644 --- a/command/write.go +++ b/command/write.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "reflect" "strings" "github.com/hashicorp/vault/helper/kv-builder" @@ -75,23 +74,7 @@ func (c *WriteCommand) Run(args []string) int { // Handle single field output if field != "" { - if val, ok := secret.Data[field]; ok { - // c.Ui.Output() prints a CR character which in this case is - // not desired. Since Vault CLI currently only uses BasicUi, - // which writes to standard output, os.Stdout is used here to - // directly print the message. If mitchellh/cli exposes method - // to print without CR, this check needs to be removed. - if reflect.TypeOf(c.Ui).String() == "*cli.BasicUi" { - fmt.Fprintf(os.Stdout, fmt.Sprintf("%v", val)) - } else { - c.Ui.Output(fmt.Sprintf("%v", val)) - } - return 0 - } else { - c.Ui.Error(fmt.Sprintf( - "Field %s not present in secret", field)) - return 1 - } + return PrintRawField(c.Ui, secret, field) } return OutputSecret(c.Ui, format, secret)