From 073e4c872e4d4fc6bc4fd7f4d55c0a47058ba811 Mon Sep 17 00:00:00 2001 From: Mathias Lafeldt Date: Thu, 10 Dec 2015 11:32:31 +0100 Subject: [PATCH] Allow to output secrets in YAML format This can be done with https://github.com/ghodss/yaml, which reuses existing JSON struct tags for YAML. --- command/format.go | 16 ++++++++++++++++ command/read.go | 2 +- command/renew.go | 2 +- command/token_create.go | 2 +- command/token_renew.go | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/command/format.go b/command/format.go index 4edb44091a..c5ef787dcb 100644 --- a/command/format.go +++ b/command/format.go @@ -5,7 +5,9 @@ import ( "encoding/json" "fmt" "strconv" + "strings" + "github.com/ghodss/yaml" "github.com/hashicorp/vault/api" "github.com/mitchellh/cli" "github.com/ryanuber/columnize" @@ -15,6 +17,8 @@ func OutputSecret(ui cli.Ui, format string, secret *api.Secret) int { switch format { case "json": return outputFormatJSON(ui, secret) + case "yaml": + return outputFormatYAML(ui, secret) case "table": fallthrough default: @@ -36,6 +40,18 @@ func outputFormatJSON(ui cli.Ui, s *api.Secret) int { return 0 } +func outputFormatYAML(ui cli.Ui, s *api.Secret) int { + b, err := yaml.Marshal(s) + if err != nil { + ui.Error(fmt.Sprintf( + "Error formatting secret: %s", err)) + return 1 + } + + ui.Output(strings.TrimSpace(string(b))) + return 0 +} + func outputFormatTable(ui cli.Ui, s *api.Secret, whitespace bool) int { config := columnize.DefaultConfig() config.Delim = "♨" diff --git a/command/read.go b/command/read.go index 7a13764a1a..c862a7ab64 100644 --- a/command/read.go +++ b/command/read.go @@ -100,7 +100,7 @@ General Options: Read Options: -format=table The format for output. By default it is a whitespace- - delimited table. This can also be json. + delimited table. This can also be json or yaml. -field=field If included, the raw value of the specified field will be output raw to stdout. diff --git a/command/renew.go b/command/renew.go index 9135f79b7d..f0474d3c7f 100644 --- a/command/renew.go +++ b/command/renew.go @@ -84,7 +84,7 @@ General Options: Renew Options: -format=table The format for output. By default it is a whitespace- - delimited table. This can also be json. + delimited table. This can also be json or yaml. ` return strings.TrimSpace(helpText) } diff --git a/command/token_create.go b/command/token_create.go index fb3ffcfc71..b0400a11e4 100644 --- a/command/token_create.go +++ b/command/token_create.go @@ -134,7 +134,7 @@ Token Options: it is automatically revoked. -format=table The format for output. By default it is a whitespace- - delimited table. This can also be json. + delimited table. This can also be json or yaml. ` return strings.TrimSpace(helpText) diff --git a/command/token_renew.go b/command/token_renew.go index c140baf594..c9f3273f4e 100644 --- a/command/token_renew.go +++ b/command/token_renew.go @@ -82,7 +82,7 @@ General Options: Token Renew Options: -format=table The format for output. By default it is a whitespace- - delimited table. This can also be json. + delimited table. This can also be json or yaml. ` return strings.TrimSpace(helpText)