From 8e39a1e7d8c375ea65f68d87f2f31907129ee9e6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Apr 2015 22:42:05 -0700 Subject: [PATCH] command/help --- api/help.go | 3 +- command/help.go | 78 +++++++++++++++++++++++++++++++++++++++ command/help_test.go | 31 ++++++++++++++++ commands.go | 6 +++ logical/framework/path.go | 5 +-- 5 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 command/help.go create mode 100644 command/help_test.go diff --git a/api/help.go b/api/help.go index 9bce6b538a..b9ae100bc5 100644 --- a/api/help.go +++ b/api/help.go @@ -6,7 +6,8 @@ import ( // Help reads the help information for the given path. func (c *Client) Help(path string) (*Help, error) { - r := c.NewRequest("GET", fmt.Sprintf("/v1/%s?help=1", path)) + r := c.NewRequest("GET", fmt.Sprintf("/v1/%s", path)) + r.Params.Add("help", "1") resp, err := c.RawRequest(r) if err != nil { return nil, err diff --git a/command/help.go b/command/help.go new file mode 100644 index 0000000000..d3e9123374 --- /dev/null +++ b/command/help.go @@ -0,0 +1,78 @@ +package command + +import ( + "fmt" + "strings" +) + +// HelpCommand is a Command that lists the mounts. +type HelpCommand struct { + Meta +} + +func (c *HelpCommand) Run(args []string) int { + flags := c.Meta.FlagSet("help", FlagSetDefault) + flags.Usage = func() { c.Ui.Error(c.Help()) } + if err := flags.Parse(args); err != nil { + return 1 + } + + args = flags.Args() + if len(args) != 1 { + flags.Usage() + c.Ui.Error("\nhelp expects a single argument") + return 1 + } + + path := args[0] + + client, err := c.Client() + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error initializing client: %s", err)) + return 2 + } + + help, err := client.Help(path) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error reading help: %s", err)) + return 1 + } + + c.Ui.Output(help.Help) + return 0 +} + +func (c *HelpCommand) Synopsis() string { + return "Look up the help for a path" +} + +func (c *HelpCommand) Help() string { + helpText := ` +Usage: vault help [options] path + + Look up the help for a path. + + All endpoints in Vault from system paths, secret paths, and credential + providers provide built-in help. This command looks up and outputs that + help. + +General Options: + + -address=TODO The address of the Vault server. + + -ca-cert=path Path to a PEM encoded CA cert file to use to + verify the Vault server SSL certificate. + + -ca-path=path Path to a directory of PEM encoded CA cert files + to verify the Vault server SSL certificate. If both + -ca-cert and -ca-path are specified, -ca-path is used. + + -insecure Do not verify TLS certificate. This is highly + not recommended. This is especially not recommended + for unsealing a vault. + +` + return strings.TrimSpace(helpText) +} diff --git a/command/help_test.go b/command/help_test.go new file mode 100644 index 0000000000..c4facc0ca8 --- /dev/null +++ b/command/help_test.go @@ -0,0 +1,31 @@ +package command + +import ( + "testing" + + "github.com/hashicorp/vault/http" + "github.com/hashicorp/vault/vault" + "github.com/mitchellh/cli" +) + +func TestHelp(t *testing.T) { + core, _, token := vault.TestCoreUnsealed(t) + ln, addr := http.TestServer(t, core) + defer ln.Close() + + ui := new(cli.MockUi) + c := &HelpCommand{ + Meta: Meta{ + ClientToken: token, + 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()) + } +} diff --git a/commands.go b/commands.go index a578295b79..8bd6792774 100644 --- a/commands.go +++ b/commands.go @@ -25,6 +25,12 @@ func init() { meta := command.Meta{Ui: ui} Commands = map[string]cli.CommandFactory{ + "help": func() (cli.Command, error) { + return &command.HelpCommand{ + Meta: meta, + }, nil + }, + "auth": func() (cli.Command, error) { return &command.AuthCommand{ Meta: meta, diff --git a/logical/framework/path.go b/logical/framework/path.go index 478e0dc173..99e7d7cf6d 100644 --- a/logical/framework/path.go +++ b/logical/framework/path.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "sort" + "strings" "text/template" "github.com/hashicorp/vault/logical" @@ -96,7 +97,7 @@ func (p *Path) helpCallback( return nil, fmt.Errorf("error executing template: %s", err) } - return logical.HelpResponse(buf.String(), nil), nil + return logical.HelpResponse(strings.TrimSpace(buf.String()), nil), nil } type pathTemplateData struct { @@ -121,12 +122,10 @@ Matching Route: {{.RoutePattern}} {{.Synopsis}} ## Parameters - {{range .Fields}} ### {{.Key}} (type: {{.Type}}) {{.Description}} - {{end}} ## Description