Skip to content

Commit

Permalink
add fallback for config location
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Apr 21, 2021
1 parent 5379b87 commit 9affac7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/meroxa/global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func readConfig() (*viper.Viper, error) {
}
configDir = filepath.Join(configDir, "meroxa")

// create subdirectory if it doesn't exist, otherwise viper will complain
err = os.MkdirAll(configDir, 0755)
if err != nil {
return nil, fmt.Errorf("could not create meroxa config directory: %w", err)
}

cfg.AddConfigPath(configDir)
cfg.SetConfigName("config")
cfg.SetConfigType("env")
Expand All @@ -41,6 +47,31 @@ func readConfig() (*viper.Viper, error) {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("could not read config: %w", err)
}

// TODO remove this code once we migrate acceptance tests to use new location
// No config found, fallback to old config file location in $HOME
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not get home directory: %w", err)
}
cfg.AddConfigPath(homeDir)
cfg.SetConfigName("meroxa")
cfg.SetConfigType("env")

if err := cfg.ReadInConfig(); err != nil {
// It's okay if there isn't a config file
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("could not read config: %w", err)
}
}
cfg.SetConfigName("config") // revert config name
if err == nil {
// we read the config in the home folder, let's write it to the new location
err = cfg.SafeWriteConfig()
if err != nil {
return nil, err
}
}
}

// When we bind flags to environment variables expect that the
Expand Down

0 comments on commit 9affac7

Please sign in to comment.