From 9eb22bd3c0ccabdda3d590aba7490accedb8dd00 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 15 Mar 2015 20:52:28 -0700 Subject: [PATCH] command/read --- command/read.go | 35 +++++++++++++++++++++++++++++++++++ command/read_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 command/read_test.go diff --git a/command/read.go b/command/read.go index 64800e530..31e5ea03f 100644 --- a/command/read.go +++ b/command/read.go @@ -1,6 +1,9 @@ package command import ( + "bytes" + "encoding/json" + "fmt" "strings" ) @@ -16,6 +19,38 @@ func (c *ReadCommand) Run(args []string) int { return 1 } + args = flags.Args() + if len(args) != 1 { + c.Ui.Error("read expects one argument") + flags.Usage() + return 1 + } + path := args[0] + + client, err := c.Client() + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error initializing client: %s", err)) + return 2 + } + + secret, err := client.Logical().Read(path) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error reading %s: %s", path, err)) + return 1 + } + + b, err := json.Marshal(secret) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error reading %s: %s", path, err)) + return 1 + } + + var out bytes.Buffer + json.Indent(&out, b, "", "\t") + c.Ui.Output(out.String()) return 0 } diff --git a/command/read_test.go b/command/read_test.go new file mode 100644 index 000000000..f1f3d22ce --- /dev/null +++ b/command/read_test.go @@ -0,0 +1,30 @@ +package command + +import ( + "testing" + + "github.com/hashicorp/vault/http" + "github.com/hashicorp/vault/vault" + "github.com/mitchellh/cli" +) + +func TestRead(t *testing.T) { + core, _ := vault.TestCoreUnsealed(t) + ln, addr := http.TestServer(t, core) + defer ln.Close() + + ui := new(cli.MockUi) + c := &ReadCommand{ + Meta: Meta{ + Ui: ui, + }, + } + + args := []string{ + "-address", addr, + "sys/mounts", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } +}