Skip to content

Commit f951767

Browse files
authored
fix: use specific config file for loading (#2573) (#2580)
Ensures that Viper loads the specific configuration file provided via the --config-file flag by using SetConfigFile instead of SetConfigName and AddConfigPath. This prevents filename ambiguity when multiple files with the same base name but different extensions exist in the same directory. Similar to AlloyDB Auth Proxy PR #914.
1 parent 4242010 commit f951767

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

cmd/config_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestNewCommandWithConfigFile(t *testing.T) {
8282
},
8383
{
8484
desc: "instance env overrides config file precedence",
85-
args: []string{"--config-file", "testdata/config.json"},
85+
args: []string{"--config-file", "testdata/config-json.json"},
8686
setup: func() {
8787
t.Setenv("CSQL_PROXY_INSTANCE_CONNECTION_NAME", "p:r:i")
8888
},
@@ -104,7 +104,7 @@ func TestNewCommandWithConfigFile(t *testing.T) {
104104
desc: "flag overrides config file precedence",
105105
args: []string{
106106
"proj:region:inst",
107-
"--config-file", "testdata/config.toml",
107+
"--config-file", "testdata/config-toml.toml",
108108
"--debug",
109109
},
110110
setup: func() {},
@@ -116,7 +116,7 @@ func TestNewCommandWithConfigFile(t *testing.T) {
116116
desc: "env overrides config file precedence",
117117
args: []string{
118118
"proj:region:inst",
119-
"--config-file", "testdata/config.toml",
119+
"--config-file", "testdata/config-toml.toml",
120120
},
121121
setup: func() {
122122
t.Setenv("CSQL_PROXY_DEBUG", "false")
@@ -125,6 +125,20 @@ func TestNewCommandWithConfigFile(t *testing.T) {
125125
assert(t, false, c.conf.Debug)
126126
},
127127
},
128+
{
129+
desc: "specific config file extension prioritization",
130+
args: []string{
131+
"proj:region:inst",
132+
"--config-file", "testdata/priority.toml",
133+
},
134+
setup: func() {},
135+
assert: func(t *testing.T, c *Command) {
136+
// priority.toml has max-connections = 5555 and port = 5555
137+
// priority.json has max-connections = 6666 and port = 6666
138+
assert(t, uint64(5555), c.conf.MaxConnections)
139+
assert(t, 5555, c.conf.Port)
140+
},
141+
},
128142
}
129143

130144
for _, tc := range tcs {

cmd/root.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,6 @@ func initViper(c *Command) (*viper.Viper, error) {
666666
v := viper.New()
667667

668668
if c.conf.Filepath != "" {
669-
// Setup Viper configuration file. Viper will attempt to load
670-
// configuration from the specified file if it exists. Otherwise, Viper
671-
// will source all configuration from flags and then environment
672-
// variables.
673669
ext := filepath.Ext(c.conf.Filepath)
674670

675671
badExtErr := newBadCommandError(
@@ -685,11 +681,7 @@ func initViper(c *Command) (*viper.Viper, error) {
685681
return nil, badExtErr
686682
}
687683

688-
conf := filepath.Base(c.conf.Filepath)
689-
noExt := strings.ReplaceAll(conf, ext, "")
690-
// argument must be the name of config file without extension
691-
v.SetConfigName(noExt)
692-
v.AddConfigPath(filepath.Dir(c.conf.Filepath))
684+
v.SetConfigFile(c.conf.Filepath)
693685

694686
// Attempt to load configuration from a file. If no file is found,
695687
// assume configuration is provided by flags or environment variables.

cmd/testdata/priority.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"max-connections": 6666,
3+
"port": 6666
4+
}

cmd/testdata/priority.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max-connections = 5555
2+
port = 5555

0 commit comments

Comments
 (0)