command/help

This commit is contained in:
Mitchell Hashimoto 2015-04-02 22:42:05 -07:00
parent 3ad31ea6e2
commit 8e39a1e7d8
5 changed files with 119 additions and 4 deletions

View File

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

78
command/help.go Normal file
View File

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

31
command/help_test.go Normal file
View File

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

View File

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

View File

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