diff --git a/cmd/config_test.go b/cmd/config_test.go index 8b252dff5..f5e029c40 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -82,7 +82,7 @@ func TestNewCommandWithConfigFile(t *testing.T) { }, { desc: "instance env overrides config file precedence", - args: []string{"--config-file", "testdata/config.json"}, + args: []string{"--config-file", "testdata/config-json.json"}, setup: func() { t.Setenv("CSQL_PROXY_INSTANCE_CONNECTION_NAME", "p:r:i") }, @@ -104,7 +104,7 @@ func TestNewCommandWithConfigFile(t *testing.T) { desc: "flag overrides config file precedence", args: []string{ "proj:region:inst", - "--config-file", "testdata/config.toml", + "--config-file", "testdata/config-toml.toml", "--debug", }, setup: func() {}, @@ -116,7 +116,7 @@ func TestNewCommandWithConfigFile(t *testing.T) { desc: "env overrides config file precedence", args: []string{ "proj:region:inst", - "--config-file", "testdata/config.toml", + "--config-file", "testdata/config-toml.toml", }, setup: func() { t.Setenv("CSQL_PROXY_DEBUG", "false") @@ -125,6 +125,20 @@ func TestNewCommandWithConfigFile(t *testing.T) { assert(t, false, c.conf.Debug) }, }, + { + desc: "specific config file extension prioritization", + args: []string{ + "proj:region:inst", + "--config-file", "testdata/priority.toml", + }, + setup: func() {}, + assert: func(t *testing.T, c *Command) { + // priority.toml has max-connections = 5555 and port = 5555 + // priority.json has max-connections = 6666 and port = 6666 + assert(t, uint64(5555), c.conf.MaxConnections) + assert(t, 5555, c.conf.Port) + }, + }, } for _, tc := range tcs { diff --git a/cmd/root.go b/cmd/root.go index 4b0ab1a56..319ab675a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -666,10 +666,6 @@ func initViper(c *Command) (*viper.Viper, error) { v := viper.New() if c.conf.Filepath != "" { - // Setup Viper configuration file. Viper will attempt to load - // configuration from the specified file if it exists. Otherwise, Viper - // will source all configuration from flags and then environment - // variables. ext := filepath.Ext(c.conf.Filepath) badExtErr := newBadCommandError( @@ -685,11 +681,7 @@ func initViper(c *Command) (*viper.Viper, error) { return nil, badExtErr } - conf := filepath.Base(c.conf.Filepath) - noExt := strings.ReplaceAll(conf, ext, "") - // argument must be the name of config file without extension - v.SetConfigName(noExt) - v.AddConfigPath(filepath.Dir(c.conf.Filepath)) + v.SetConfigFile(c.conf.Filepath) // Attempt to load configuration from a file. If no file is found, // assume configuration is provided by flags or environment variables. diff --git a/cmd/testdata/priority.json b/cmd/testdata/priority.json new file mode 100644 index 000000000..705f52c65 --- /dev/null +++ b/cmd/testdata/priority.json @@ -0,0 +1,4 @@ +{ + "max-connections": 6666, + "port": 6666 +} diff --git a/cmd/testdata/priority.toml b/cmd/testdata/priority.toml new file mode 100644 index 000000000..7420ab7b0 --- /dev/null +++ b/cmd/testdata/priority.toml @@ -0,0 +1,2 @@ +max-connections = 5555 +port = 5555