diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cdd0769a3..94cccb6247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,7 @@ RPM packages, which has been left unchanged. 1. [20669](https://github.com/influxdata/influxdb/pull/20669): Remove blank lines from payloads sent by `influx write`. 1. [20657](https://github.com/influxdata/influxdb/pull/20657): Allow for creating users without initial passwords in `influx user create`. 1. [20679](https://github.com/influxdata/influxdb/pull/20679): Fix incorrect "bucket not found" errors when passing `--bucket-id` to `influx write`. +1. [20702](https://github.com/influxdata/influxdb/pull/20702): Fix loading config when `INFLUXD_CONFIG_PATH` points to a directory with `.` in its name. ## v2.0.3 [2020-12-14] diff --git a/kit/cli/viper.go b/kit/cli/viper.go index 15eb8c594c..7cbf54b24f 100644 --- a/kit/cli/viper.go +++ b/kit/cli/viper.go @@ -60,20 +60,6 @@ func NewCommand(v *viper.Viper, p *Program) *cobra.Command { // This normalizes "-" to an underscore in env names. v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) - if configPath := v.GetString("CONFIG_PATH"); configPath != "" { - switch strings.ToLower(path.Ext(configPath)) { - case ".json", ".toml", ".yaml", ".yml": - v.SetConfigFile(configPath) - case "": - v.AddConfigPath(configPath) - } - } else { - // defaults to looking in same directory as program running for - // a file with base `config` and extensions .json|.toml|.yaml|.yml - v.SetConfigName("config") - v.AddConfigPath(".") - } - // done before we bind flags to viper keys. // order of precedence (1 highest -> 3 lowest): // 1. flags @@ -88,8 +74,20 @@ func NewCommand(v *viper.Viper, p *Program) *cobra.Command { } func initializeConfig(v *viper.Viper) error { - err := v.ReadInConfig() - if err != nil && !os.IsNotExist(err) { + configPath := v.GetString("CONFIG_PATH") + if configPath == "" { + // Default to looking in the working directory of the running process. + configPath = "." + } + + switch strings.ToLower(path.Ext(configPath)) { + case ".json", ".toml", ".yaml", ".yml": + v.SetConfigFile(configPath) + default: + v.AddConfigPath(configPath) + } + + if err := v.ReadInConfig(); err != nil && !os.IsNotExist(err) { if _, ok := err.(viper.ConfigFileNotFoundError); !ok { return err } diff --git a/kit/cli/viper_test.go b/kit/cli/viper_test.go index aa58e75e3b..713cc2c2aa 100644 --- a/kit/cli/viper_test.go +++ b/kit/cli/viper_test.go @@ -7,6 +7,7 @@ import ( "math" "os" "path" + "path/filepath" "testing" "time" @@ -420,3 +421,96 @@ func Test_ConfigPrecedence(t *testing.T) { t.Run(tt.name, fn) } } + +func Test_ConfigPathDotDirectory(t *testing.T) { + testDir, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(testDir) + + tests := []struct { + name string + dir string + }{ + { + name: "dot at start", + dir: ".directory", + }, + { + name: "dot in middle", + dir: "config.d", + }, + { + name: "dot at end", + dir: "forgotmyextension.", + }, + } + + config := map[string]string{ + "foo": "bar", + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + configDir := filepath.Join(testDir, tc.dir) + require.NoError(t, os.Mkdir(configDir, 0700)) + + _, err = writeTomlConfig(configDir, config) + require.NoError(t, err) + defer setEnvVar("TEST_CONFIG_PATH", configDir)() + + var foo string + program := &Program{ + Name: "test", + Opts: []Opt{ + { + DestP: &foo, + Flag: "foo", + }, + }, + Run: func() error { return nil }, + } + + cmd := NewCommand(viper.New(), program) + cmd.SetArgs([]string{}) + require.NoError(t, cmd.Execute()) + + require.Equal(t, "bar", foo) + }) + } +} + +func Test_LoadConfigCwd(t *testing.T) { + testDir, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(testDir) + + pwd, err := os.Getwd() + require.NoError(t, err) + defer os.Chdir(pwd) + + require.NoError(t, os.Chdir(testDir)) + + config := map[string]string{ + "foo": "bar", + } + _, err = writeJsonConfig(testDir, config) + require.NoError(t, err) + + var foo string + program := &Program{ + Name: "test", + Opts: []Opt{ + { + DestP: &foo, + Flag: "foo", + }, + }, + Run: func() error { return nil }, + } + + cmd := NewCommand(viper.New(), program) + cmd.SetArgs([]string{}) + require.NoError(t, cmd.Execute()) + + require.Equal(t, "bar", foo) +}