From 32e640c8d072fe820398557f572e8b3d5509bf08 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Mar 2015 23:34:32 -0800 Subject: [PATCH] command/auth --- command/auth.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ command/meta.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ commands.go | 30 +++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 command/auth.go create mode 100644 command/meta.go diff --git a/command/auth.go b/command/auth.go new file mode 100644 index 000000000..50ccfe6d8 --- /dev/null +++ b/command/auth.go @@ -0,0 +1,47 @@ +package command + +import ( + "strings" +) + +// AuthCommand is a Command that handles authentication. +type AuthCommand struct { + Meta +} + +func (c *AuthCommand) Run(args []string) int { + var method string + flags := c.Meta.FlagSet("auth", FlagSetDefault) + flags.StringVar(&method, "method", "", "method") + flags.Usage = func() { c.Ui.Error(c.Help()) } + if err := flags.Parse(args); err != nil { + return 1 + } + + return 0 +} + +func (c *AuthCommand) Synopsis() string { + return "Prints information about how to authenticate with Vault" +} + +func (c *AuthCommand) Help() string { + helpText := ` +Usage: vault auth [options] + + Outputs instructions for authenticating with vault. + + Vault authentication is always done via environmental variables. The + specific environmental variables and the meaning for each environmental + variable varies depending on the auth mechanism that Vault is using. + This command outputs the mechanism vault is using along with documentation + for how to authenticate. + +Options: + + -method=name Outputs help for the authentication method with the given + name for the remote server. If this authentication method + is not available, exit with code 1. +` + return strings.TrimSpace(helpText) +} diff --git a/command/meta.go b/command/meta.go new file mode 100644 index 000000000..04e7dec42 --- /dev/null +++ b/command/meta.go @@ -0,0 +1,48 @@ +package command + +import ( + "bufio" + "flag" + "io" + + "github.com/mitchellh/cli" +) + +// FlagSetFlags is an enum to define what flags are present in the +// default FlagSet returned by Meta.FlagSet. +type FlagSetFlags uint + +const ( + FlagSetNone FlagSetFlags = 0 + FlagSetServer FlagSetFlags = 1 << iota + FlagSetDefault = FlagSetServer +) + +// Meta contains the meta-options and functionality that nearly every +// Vault command inherits. +type Meta struct { + Ui cli.Ui +} + +// FlagSet returns a FlagSet with the common flags that every +// command implements. The exact behavior of FlagSet can be configured +// using the flags as the second parameter, for example to disable +// server settings on the commands that don't talk to a server. +func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet { + f := flag.NewFlagSet(n, flag.ContinueOnError) + + // Create an io.Writer that writes to our Ui properly for errors. + // This is kind of a hack, but it does the job. Basically: create + // a pipe, use a scanner to break it into lines, and output each line + // to the UI. Do this forever. + errR, errW := io.Pipe() + errScanner := bufio.NewScanner(errR) + go func() { + for errScanner.Scan() { + m.Ui.Error(errScanner.Text()) + } + }() + f.SetOutput(errW) + + return f +} diff --git a/commands.go b/commands.go index 84c2e2e49..0c0a91496 100644 --- a/commands.go +++ b/commands.go @@ -11,9 +11,37 @@ import ( var Commands map[string]cli.CommandFactory func init() { - ui := &cli.BasicUi{Writer: os.Stdout} + ui := &cli.BasicUi{ + Writer: os.Stdout, + ErrorWriter: os.Stderr, + } + meta := command.Meta{Ui: ui} Commands = map[string]cli.CommandFactory{ + "auth": func() (cli.Command, error) { + return &command.AuthCommand{ + Meta: meta, + }, nil + }, + + /* + "get": func() (cli.Command, error) { + return nil, nil + }, + + "put": func() (cli.Command, error) { + return nil, nil + }, + + "seal": func() (cli.Command, error) { + return nil, nil + }, + + "unseal": func() (cli.Command, error) { + return nil, nil + }, + */ + "version": func() (cli.Command, error) { ver := Version rel := VersionPrerelease