From ef3b9610a17f4d87d5cb03bfe9d69f1d416e1ce3 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 3 May 2017 10:37:25 +0200 Subject: [PATCH] Fixed up the --home flag, ebuchman check this out --- cli/setup.go | 8 ++++++-- cli/setup_test.go | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cli/setup.go b/cli/setup.go index 8120449c..b6f00658 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 fb8e5655..47170fe2 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 {