diff --git a/command/meta.go b/command/meta.go index 04e7dec428..3b10e805a0 100644 --- a/command/meta.go +++ b/command/meta.go @@ -22,6 +22,12 @@ const ( // Vault command inherits. type Meta struct { 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 @@ -31,6 +37,15 @@ type Meta struct { func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet { 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. // 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 diff --git a/command/meta_test.go b/command/meta_test.go new file mode 100644 index 0000000000..6670bf79fd --- /dev/null +++ b/command/meta_test.go @@ -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) + } + } +}