open-vault/command/auth_help.go

126 lines
2.7 KiB
Go
Raw Normal View History

2017-09-05 03:59:45 +00:00
package command
import (
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
var (
_ cli.Command = (*AuthHelpCommand)(nil)
_ cli.CommandAutocomplete = (*AuthHelpCommand)(nil)
)
2017-09-05 03:59:45 +00:00
type AuthHelpCommand struct {
*BaseCommand
2017-09-08 01:56:39 +00:00
Handlers map[string]LoginHandler
2017-09-05 03:59:45 +00:00
}
func (c *AuthHelpCommand) Synopsis() string {
2017-09-08 01:56:39 +00:00
return "Prints usage for an auth method"
2017-09-05 03:59:45 +00:00
}
func (c *AuthHelpCommand) Help() string {
helpText := `
2017-09-08 01:56:39 +00:00
Usage: vault auth help [options] TYPE | PATH
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
Prints usage and help for an auth method.
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
- If given a TYPE, this command prints the default help for the
auth method of that type.
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
- If given a PATH, this command prints the help output for the
auth method enabled at that path. This path must already
exist.
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
Get usage instructions for the userpass auth method:
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
$ vault auth help userpass
2017-09-05 03:59:45 +00:00
2017-09-08 01:56:39 +00:00
Print usage for the auth method enabled at my-method/:
$ vault auth help my-method/
Each auth method produces its own help output.
2017-09-05 03:59:45 +00:00
` + c.Flags().Help()
return strings.TrimSpace(helpText)
}
func (c *AuthHelpCommand) Flags() *FlagSets {
return c.flagSet(FlagSetHTTP)
}
func (c *AuthHelpCommand) AutocompleteArgs() complete.Predictor {
handlers := make([]string, 0, len(c.Handlers))
for k := range c.Handlers {
handlers = append(handlers, k)
}
return complete.PredictSet(handlers...)
}
func (c *AuthHelpCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *AuthHelpCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
args = f.Args()
switch {
case len(args) < 1:
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1, got %d)", len(args)))
return 1
case len(args) > 1:
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
return 1
}
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
// Start with the assumption that we have an auth type, not a path.
authType := strings.TrimSpace(args[0])
authHandler, ok := c.Handlers[authType]
if !ok {
// There was no auth type by that name, see if it's a mount
auths, err := client.Sys().ListAuth()
if err != nil {
2017-09-08 01:56:39 +00:00
c.UI.Error(fmt.Sprintf("Error listing auth methods: %s", err))
2017-09-05 03:59:45 +00:00
return 2
}
authPath := ensureTrailingSlash(sanitizePath(args[0]))
auth, ok := auths[authPath]
if !ok {
c.UI.Warn(fmt.Sprintf(
"No auth method available on the server at %q", authPath))
2017-09-05 03:59:45 +00:00
return 1
}
authHandler, ok = c.Handlers[auth.Type]
if !ok {
c.UI.Warn(wrapAtLength(fmt.Sprintf(
"No method-specific CLI handler available for auth method %q",
authType)))
2017-09-05 03:59:45 +00:00
return 2
}
}
c.UI.Output(authHandler.Help())
return 0
}