command/auth

This commit is contained in:
Mitchell Hashimoto 2015-03-03 23:34:32 -08:00
parent fdc3368ac0
commit 32e640c8d0
3 changed files with 124 additions and 1 deletions

47
command/auth.go Normal file
View File

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

48
command/meta.go Normal file
View File

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

View File

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