command/meta: server options

This commit is contained in:
Mitchell Hashimoto 2015-03-03 23:49:37 -08:00
parent 32e640c8d0
commit 38bfea80cc
2 changed files with 56 additions and 0 deletions

View File

@ -22,6 +22,12 @@ const (
// Vault command inherits. // Vault command inherits.
type Meta struct { type Meta struct {
Ui cli.Ui Ui cli.Ui
// These are set by the command line flags.
flagAddress string
flagCACert string
flagCAPath string
flagInsecure bool
} }
// FlagSet returns a FlagSet with the common flags that every // FlagSet returns a FlagSet with the common flags that every
@ -31,6 +37,15 @@ type Meta struct {
func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet { func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
f := flag.NewFlagSet(n, flag.ContinueOnError) f := flag.NewFlagSet(n, flag.ContinueOnError)
// FlagSetServer tells us to enable the settings for selecting
// the server information.
if fs&FlagSetServer != 0 {
f.StringVar(&m.flagAddress, "address", "", "")
f.StringVar(&m.flagCACert, "ca-cert", "", "")
f.StringVar(&m.flagCAPath, "ca-path", "", "")
f.BoolVar(&m.flagInsecure, "insecure", false, "")
}
// Create an io.Writer that writes to our Ui properly for errors. // 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 // 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 // a pipe, use a scanner to break it into lines, and output each line

41
command/meta_test.go Normal file
View File

@ -0,0 +1,41 @@
package command
import (
"flag"
"reflect"
"sort"
"testing"
)
func TestFlagSet(t *testing.T) {
cases := []struct {
Flags FlagSetFlags
Expected []string
}{
{
FlagSetNone,
[]string{},
},
{
FlagSetServer,
[]string{"address", "ca-cert", "ca-path", "insecure"},
},
}
for i, tc := range cases {
var m Meta
fs := m.FlagSet("foo", tc.Flags)
actual := make([]string, 0, 0)
fs.VisitAll(func(f *flag.Flag) {
actual = append(actual, f.Name)
})
sort.Strings(actual)
sort.Strings(tc.Expected)
if !reflect.DeepEqual(actual, tc.Expected) {
t.Fatalf("%d: flags: %#v\n\nExpected: %#v\nGot: %#v",
i, tc.Flags, tc.Expected, actual)
}
}
}