From b5e4e4bf250e7b81d28ec735f36334436b1d53d1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 31 Mar 2015 20:50:05 -0700 Subject: [PATCH] command/read: better UX on vault read --- command/read.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/command/read.go b/command/read.go index 3f997ae0e..44590f97c 100644 --- a/command/read.go +++ b/command/read.go @@ -5,6 +5,9 @@ import ( "encoding/json" "fmt" "strings" + + "github.com/hashicorp/vault/api" + "github.com/ryanuber/columnize" ) // ReadCommand is a Command that reads data from the Vault. @@ -13,7 +16,9 @@ type ReadCommand struct { } func (c *ReadCommand) Run(args []string) int { + var format string flags := c.Meta.FlagSet("read", FlagSetDefault) + flags.StringVar(&format, "format", "table", "") flags.Usage = func() { c.Ui.Error(c.Help()) } if err := flags.Parse(args); err != nil { return 1 @@ -41,10 +46,25 @@ func (c *ReadCommand) Run(args []string) int { return 1 } - b, err := json.Marshal(secret) + switch format { + case "json": + return c.formatJSON(secret) + case "table-whitespace": + return c.formatTable(secret, true) + case "table": + fallthrough + default: + return c.formatTable(secret, false) + } + + return 0 +} + +func (c *ReadCommand) formatJSON(s *api.Secret) int { + b, err := json.Marshal(s) if err != nil { c.Ui.Error(fmt.Sprintf( - "Error reading %s: %s", path, err)) + "Error formatting secret: %s", err)) return 1 } @@ -54,6 +74,30 @@ func (c *ReadCommand) Run(args []string) int { return 0 } +func (c *ReadCommand) formatTable(s *api.Secret, whitespace bool) int { + config := columnize.DefaultConfig() + config.Delim = "♨" + config.Glue = " | " + config.Prefix = "" + if whitespace { + config.Glue = "\t" + } + + input := make([]string, 0, 5) + input = append(input, fmt.Sprintf("Key %s Value", config.Delim)) + + if s.VaultId != "" { + input = append(input, fmt.Sprintf("vault_id %s %s", config.Delim, s.VaultId)) + } + + 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 +} + func (c *ReadCommand) Synopsis() string { return "Read data or secrets from Vault" } @@ -84,6 +128,12 @@ General Options: not recommended. This is especially not recommended for unsealing a vault. +Read Options: + + -format=table The format for output. By default it is a human + friendly table. This can also be table-whitespace, + json. + ` return strings.TrimSpace(helpText) }