config: fix loading commands args from env vars

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2023-06-25 21:31:57 +02:00
parent 4440c49174
commit d1e4ee7bc8
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
2 changed files with 11 additions and 1 deletions

View file

@ -1961,6 +1961,11 @@ func getCommandConfigsFromEnv(idx int) {
cfg.Env = env cfg.Env = env
} }
args, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__ARGS", idx))
if ok {
cfg.Args = args
}
if cfg.Path != "" { if cfg.Path != "" {
if len(globalConf.CommandConfig.Commands) > idx { if len(globalConf.CommandConfig.Commands) > idx {
globalConf.CommandConfig.Commands[idx] = cfg globalConf.CommandConfig.Commands[idx] = cfg

View file

@ -924,13 +924,17 @@ func TestCommandsFromEnv(t *testing.T) {
os.Setenv("SFTPGO_COMMAND__COMMANDS__1__PATH", "cmd2") os.Setenv("SFTPGO_COMMAND__COMMANDS__1__PATH", "cmd2")
os.Setenv("SFTPGO_COMMAND__COMMANDS__1__TIMEOUT", "20") os.Setenv("SFTPGO_COMMAND__COMMANDS__1__TIMEOUT", "20")
os.Setenv("SFTPGO_COMMAND__COMMANDS__1__ENV", "e=f") os.Setenv("SFTPGO_COMMAND__COMMANDS__1__ENV", "e=f")
os.Setenv("SFTPGO_COMMAND__COMMANDS__1__ARGS", "arg1, arg2")
t.Cleanup(func() { t.Cleanup(func() {
os.Unsetenv("SFTPGO_COMMAND__TIMEOUT") os.Unsetenv("SFTPGO_COMMAND__TIMEOUT")
os.Unsetenv("SFTPGO_COMMAND__ENV") os.Unsetenv("SFTPGO_COMMAND__ENV")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__0__PATH") os.Unsetenv("SFTPGO_COMMAND__COMMANDS__0__PATH")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__0__TIMEOUT") os.Unsetenv("SFTPGO_COMMAND__COMMANDS__0__TIMEOUT")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__0__ENV") os.Unsetenv("SFTPGO_COMMAND__COMMANDS__1__PATH")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__1__TIMEOUT")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__1__ENV")
os.Unsetenv("SFTPGO_COMMAND__COMMANDS__1__ARGS")
}) })
err = config.LoadConfig(configDir, confName) err = config.LoadConfig(configDir, confName)
@ -945,6 +949,7 @@ func TestCommandsFromEnv(t *testing.T) {
require.Equal(t, "cmd2", commandConfig.Commands[1].Path) require.Equal(t, "cmd2", commandConfig.Commands[1].Path)
require.Equal(t, 20, commandConfig.Commands[1].Timeout) require.Equal(t, 20, commandConfig.Commands[1].Timeout)
require.Equal(t, []string{"e=f"}, commandConfig.Commands[1].Env) require.Equal(t, []string{"e=f"}, commandConfig.Commands[1].Env)
require.Equal(t, []string{"arg1", "arg2"}, commandConfig.Commands[1].Args)
err = os.Remove(configFilePath) err = os.Remove(configFilePath)
assert.NoError(t, err) assert.NoError(t, err)