command/read

This commit is contained in:
Mitchell Hashimoto 2015-03-15 20:52:28 -07:00
parent c206755bdc
commit 9eb22bd3c0
2 changed files with 65 additions and 0 deletions

View File

@ -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
}

30
command/read_test.go Normal file
View File

@ -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())
}
}