refuse to start if the config file is invalid

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2023-12-10 16:50:15 +01:00
parent c71f0426ae
commit d8b040e57c
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
2 changed files with 4 additions and 4 deletions

View file

@ -734,9 +734,9 @@ func LoadConfig(configDir, configFile string) error {
if errors.As(err, &viper.ConfigFileNotFoundError{}) { if errors.As(err, &viper.ConfigFileNotFoundError{}) {
logger.Debug(logSender, "", "no configuration file found") logger.Debug(logSender, "", "no configuration file found")
} else { } else {
// should we return the error and not start here?
logger.Warn(logSender, "", "error loading configuration file: %v", err) logger.Warn(logSender, "", "error loading configuration file: %v", err)
logger.WarnToConsole("error loading configuration file: %v", err) logger.WarnToConsole("error loading configuration file: %v", err)
return err
} }
} }
checkOverrideDefaultSettings() checkOverrideDefaultSettings()

View file

@ -68,11 +68,11 @@ func TestLoadConfigTest(t *testing.T) {
confName := tempConfigName + ".json" //nolint:goconst confName := tempConfigName + ".json" //nolint:goconst
configFilePath := filepath.Join(configDir, confName) configFilePath := filepath.Join(configDir, confName)
err = config.LoadConfig(configDir, confName) err = config.LoadConfig(configDir, confName)
assert.NoError(t, err) assert.Error(t, err)
err = os.WriteFile(configFilePath, []byte("{invalid json}"), os.ModePerm) err = os.WriteFile(configFilePath, []byte("{invalid json}"), os.ModePerm)
assert.NoError(t, err) assert.NoError(t, err)
err = config.LoadConfig(configDir, confName) err = config.LoadConfig(configDir, confName)
assert.NoError(t, err) assert.Error(t, err)
err = os.WriteFile(configFilePath, []byte(`{"sftpd": {"max_auth_tries": "a"}}`), os.ModePerm) err = os.WriteFile(configFilePath, []byte(`{"sftpd": {"max_auth_tries": "a"}}`), os.ModePerm)
assert.NoError(t, err) assert.NoError(t, err)
err = config.LoadConfig(configDir, confName) err = config.LoadConfig(configDir, confName)
@ -1587,7 +1587,7 @@ func TestConfigFromEnv(t *testing.T) {
os.Unsetenv("SFTPGO_HTTPD__SETUP__INSTALLATION_CODE") os.Unsetenv("SFTPGO_HTTPD__SETUP__INSTALLATION_CODE")
os.Unsetenv("SFTPGO_ACME__HTTP01_CHALLENGE_PORT") os.Unsetenv("SFTPGO_ACME__HTTP01_CHALLENGE_PORT")
}) })
err := config.LoadConfig(".", "invalid config") err := config.LoadConfig(configDir, "")
assert.NoError(t, err) assert.NoError(t, err)
sftpdConfig := config.GetSFTPDConfig() sftpdConfig := config.GetSFTPDConfig()
assert.Equal(t, "127.0.0.1", sftpdConfig.Bindings[0].Address) assert.Equal(t, "127.0.0.1", sftpdConfig.Bindings[0].Address)