fix(kit/cli): fix loading config from directories with '.' in the name (#20702)
parent
b01db56738
commit
1ad8f8d23d
|
@ -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. [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. [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. [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]
|
## v2.0.3 [2020-12-14]
|
||||||
|
|
||||||
|
|
|
@ -60,20 +60,6 @@ func NewCommand(v *viper.Viper, p *Program) *cobra.Command {
|
||||||
// This normalizes "-" to an underscore in env names.
|
// This normalizes "-" to an underscore in env names.
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
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.
|
// done before we bind flags to viper keys.
|
||||||
// order of precedence (1 highest -> 3 lowest):
|
// order of precedence (1 highest -> 3 lowest):
|
||||||
// 1. flags
|
// 1. flags
|
||||||
|
@ -88,8 +74,20 @@ func NewCommand(v *viper.Viper, p *Program) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeConfig(v *viper.Viper) error {
|
func initializeConfig(v *viper.Viper) error {
|
||||||
err := v.ReadInConfig()
|
configPath := v.GetString("CONFIG_PATH")
|
||||||
if err != nil && !os.IsNotExist(err) {
|
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 {
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -420,3 +421,96 @@ func Test_ConfigPrecedence(t *testing.T) {
|
||||||
t.Run(tt.name, fn)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue