Skip to content
Snippets Groups Projects

Use config package to handle all controller configurations

Merged Ghost User requested to merge istaester/provide-config-package into develop
Compare and Show latest version
5 files
+ 52
20
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 17
13
@@ -97,19 +97,6 @@ func initConfig() {
viper.AddConfigPath("/usr/local/etc/gosdn/")
viper.SetConfigType(configType)
viper.SetConfigName(configName)
// Viper will crash if you call 'WriteConfig()' and the file does
// not exists yet.
// Therefore we handle this case here.
// Inspired by //https://github.com/spf13/viper/issues/430#issuecomment-661945101
configPath := filepath.Join(configHome, configName+"."+configType)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err := ensureFileSystemStoreExists(configPath)
if err != nil {
panic(err)
}
}
}
viper.AutomaticEnv() // read in environment variables that match
@@ -117,6 +104,8 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debug("Using config file:", viper.ConfigFileUsed())
} else {
ensureViperConfigFileExists()
}
viper.SetDefault("socket", ":55055")
@@ -148,3 +137,18 @@ func ensureFileSystemStoreExists(pathToStore string) error {
return nil
}
func ensureViperConfigFileExists() {
// Viper will crash if you call 'WriteConfig()' and the file does
// not exists yet.
// Therefore we handle this case here.
// Inspired by //https://github.com/spf13/viper/issues/430#issuecomment-661945101
configPath := filepath.Join(configHome, configName+"."+configType)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err := ensureFileSystemStoreExists(configPath)
if err != nil {
panic(err)
}
}
}
Loading