From 5aee91ccb9f5fa9d26b9506e52631933ec2b8f64 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 27 Apr 2015 14:55:29 -0700 Subject: [PATCH] command: refactor to share output formating code --- command/format.go | 59 ++++++++++++++++++++++++++++++++++++++++++ command/read.go | 55 +-------------------------------------- command/renew.go | 5 +--- command/token_renew.go | 5 +--- 4 files changed, 62 insertions(+), 62 deletions(-) create mode 100644 command/format.go diff --git a/command/format.go b/command/format.go new file mode 100644 index 0000000000..da7fc024ae --- /dev/null +++ b/command/format.go @@ -0,0 +1,59 @@ +package command + +import ( + "bytes" + "encoding/json" + "fmt" + + "github.com/hashicorp/vault/api" + "github.com/mitchellh/cli" + "github.com/ryanuber/columnize" +) + +func OutputSecret(ui cli.Ui, format string, secret *api.Secret) int { + switch format { + case "json": + return outputFormatJSON(ui, secret) + case "table": + fallthrough + default: + return outputFormatTable(ui, secret, true) + } +} + +func outputFormatJSON(ui cli.Ui, s *api.Secret) int { + b, err := json.Marshal(s) + if err != nil { + ui.Error(fmt.Sprintf( + "Error formatting secret: %s", err)) + return 1 + } + + var out bytes.Buffer + json.Indent(&out, b, "", "\t") + ui.Output(out.String()) + return 0 +} + +func outputFormatTable(ui cli.Ui, s *api.Secret, whitespace bool) int { + config := columnize.DefaultConfig() + config.Delim = "♨" + config.Glue = "\t" + config.Prefix = "" + + input := make([]string, 0, 5) + input = append(input, fmt.Sprintf("Key %s Value", config.Delim)) + + if s.LeaseID != "" && s.LeaseDuration > 0 { + input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID)) + input = append(input, fmt.Sprintf( + "lease_duration %s %d", config.Delim, s.LeaseDuration)) + } + + for k, v := range s.Data { + input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v)) + } + + ui.Output(columnize.Format(input, config)) + return 0 +} diff --git a/command/read.go b/command/read.go index 98e5f6585c..0885a52235 100644 --- a/command/read.go +++ b/command/read.go @@ -1,13 +1,8 @@ package command import ( - "bytes" - "encoding/json" "fmt" "strings" - - "github.com/hashicorp/vault/api" - "github.com/ryanuber/columnize" ) // ReadCommand is a Command that reads data from the Vault. @@ -51,55 +46,7 @@ func (c *ReadCommand) Run(args []string) int { return 1 } - return c.output(format, secret) -} - -func (c *ReadCommand) output(format string, secret *api.Secret) int { - switch format { - case "json": - return c.formatJSON(secret) - case "table": - fallthrough - default: - return c.formatTable(secret, true) - } -} - -func (c *ReadCommand) formatJSON(s *api.Secret) int { - b, err := json.Marshal(s) - if err != nil { - c.Ui.Error(fmt.Sprintf( - "Error formatting secret: %s", err)) - return 1 - } - - var out bytes.Buffer - json.Indent(&out, b, "", "\t") - c.Ui.Output(out.String()) - return 0 -} - -func (c *ReadCommand) formatTable(s *api.Secret, whitespace bool) int { - config := columnize.DefaultConfig() - config.Delim = "♨" - config.Glue = "\t" - config.Prefix = "" - - input := make([]string, 0, 5) - input = append(input, fmt.Sprintf("Key %s Value", config.Delim)) - - if s.LeaseID != "" && s.LeaseDuration > 0 { - input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID)) - input = append(input, fmt.Sprintf( - "lease_duration %s %d", config.Delim, s.LeaseDuration)) - } - - for k, v := range s.Data { - input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v)) - } - - c.Ui.Output(columnize.Format(input, config)) - return 0 + return OutputSecret(c.Ui, format, secret) } func (c *ReadCommand) Synopsis() string { diff --git a/command/renew.go b/command/renew.go index 479a2d9045..0b009c078e 100644 --- a/command/renew.go +++ b/command/renew.go @@ -55,10 +55,7 @@ func (c *RenewCommand) Run(args []string) int { return 1 } - // Use the ReadCommand in order to format our output - var read ReadCommand - read.Meta = c.Meta - return read.output(format, secret) + return OutputSecret(c.Ui, format, secret) } func (c *RenewCommand) Synopsis() string { diff --git a/command/token_renew.go b/command/token_renew.go index 0f4665c2dc..4aa211dfad 100644 --- a/command/token_renew.go +++ b/command/token_renew.go @@ -54,10 +54,7 @@ func (c *TokenRenewCommand) Run(args []string) int { return 1 } - // Use the ReadCommand in order to format our output - var read ReadCommand - read.Meta = c.Meta - return read.output(format, secret) + return OutputSecret(c.Ui, format, secret) } func (c *TokenRenewCommand) Synopsis() string {