make config flag global, and add global dry run flag

config flag can be used by all subcommands. Dry run flag stops subcommand
execution after parsing of config - nice for testing.
This commit is contained in:
Matt Jaffee 2017-03-16 11:23:59 -05:00
parent dcafca3141
commit e144259a19
3 changed files with 12 additions and 2 deletions

View file

@ -40,9 +40,21 @@ Build Time: ` + BuildTime + "\n",
if err != nil {
return err
}
// return "dry run" error if "dry-run" flag is set
if ret, err := cmd.Flags().GetBool("dry-run"); ret && err == nil {
if cmd.Parent() != nil {
return fmt.Errorf("dry run")
} else if err != nil {
return fmt.Errorf("problem getting dry-run flag: %v", err)
}
}
return nil
},
}
rc.PersistentFlags().Bool("dry-run", false, "stop before executing")
rc.PersistentFlags().StringP("config", "c", "", "Configuration file to read from.")
for _, subcomFn := range subcommandFns {
rc.AddCommand(subcomFn(stdin, stdout, stderr))
}

View file

@ -89,7 +89,6 @@ func (ct commandTest) setupCommand(t *testing.T) *cobra.Command {
// set up config file args/env
ct.env["PILOSA_CONFIG"] = cfgFile.Name()
ct.args = append(ct.args[:1], append([]string{"--config=" + cfgFile.Name()}, ct.args[1:]...)...)
// set up env
for name, val := range ct.env {

View file

@ -74,7 +74,6 @@ on the configured port.`,
}
flags := serveCmd.Flags()
flags.StringVarP(&Server.ConfigPath, "config", "c", "", "Configuration file to read from.")
flags.StringVarP(&Server.Config.DataDir, "data-dir", "d", "~/.pilosa", "Directory to store pilosa data files.")
flags.StringVarP(&Server.Config.Host, "bind", "", ":10101", "Default URI on which pilosa should listen.")
flags.IntVarP(&Server.Config.Cluster.ReplicaN, "cluster.replicas", "", 1, "Number hosts each piece of data should be stored on.")