Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand All @@ -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() {},
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
10 changes: 1 addition & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions cmd/testdata/priority.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"max-connections": 6666,
"port": 6666
}
2 changes: 2 additions & 0 deletions cmd/testdata/priority.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max-connections = 5555
port = 5555
Loading