diff --git a/cli/setup.go b/cli/setup.go index 8120449c7..b6f006583 100644 --- a/cli/setup.go +++ b/cli/setup.go @@ -24,7 +24,8 @@ const ( func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot string) func() { cobra.OnInitialize(func() { initEnv(envPrefix) }) cmd.PersistentFlags().StringP(RootFlag, "r", defautRoot, "DEPRECATED. Use --home") - cmd.PersistentFlags().StringP(HomeFlag, "h", defautRoot, "root directory for config and data") + // -h is already reserved for --help as part of the cobra framework + cmd.PersistentFlags().String(HomeFlag, "", "root directory for config and data") cmd.PersistentPreRunE = concatCobraCmdFuncs(bindFlagsLoadViper, cmd.PersistentPreRunE) return func() { execute(cmd) } } @@ -104,7 +105,10 @@ func bindFlagsLoadViper(cmd *cobra.Command, args []string) error { // rootDir is command line flag, env variable, or default $HOME/.tlc // NOTE: we support both --root and --home for now, but eventually only --home rootDir := viper.GetString(HomeFlag) - if !viper.IsSet(HomeFlag) && viper.IsSet(RootFlag) { + // @ebuchman: viper.IsSet doesn't do what you think... + // Even a default of "" on the pflag marks it as set, + // simply by fact of having a pflag. + if rootDir == "" { rootDir = viper.GetString(RootFlag) } viper.SetConfigName("config") // name of config file (without extension) diff --git a/cli/setup_test.go b/cli/setup_test.go index fb8e5655c..47170fe21 100644 --- a/cli/setup_test.go +++ b/cli/setup_test.go @@ -103,6 +103,9 @@ func TestSetupConfig(t *testing.T) { {nil, map[string]string{"RD_BOO": "bang"}, "bang"}, {nil, map[string]string{"RD_ROOT": conf1}, cval1}, {nil, map[string]string{"RDROOT": conf2}, cval2}, + {nil, map[string]string{"RDHOME": conf1}, cval1}, + // and when both are set??? HOME wins every time! + {[]string{"--root", conf1}, map[string]string{"RDHOME": conf2}, cval2}, } for idx, tc := range cases {