fix: config failing in tests (#625)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2024-07-09 10:47:44 -03:00 committed by GitHub
parent f44e5b5f5c
commit 1133e5047b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 7 deletions

View file

@ -53,12 +53,12 @@ func ensureConfigFile() error {
if configFile == "" {
configFile = viper.GetViper().ConfigFileUsed()
if err := os.MkdirAll(filepath.Dir(configFile), 0o755); err != nil {
return fmt.Errorf("Could not write config file: %w", err)
return fmt.Errorf("could not write configuration file: %w", err)
}
}
if ext := path.Ext(configFile); ext != ".yaml" && ext != ".yml" {
return fmt.Errorf("'%s' is not a supported config type: use '%s' or '%s'", ext, ".yaml", ".yml")
return fmt.Errorf("'%s' is not a supported configuration type: use '%s' or '%s'", ext, ".yaml", ".yml")
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {

13
main.go
View file

@ -400,12 +400,12 @@ func tryLoadConfigFromDefaultPlaces() {
os.Exit(1)
}
if c := os.Getenv("CHARM_CONFIG_HOME"); c != "" {
viper.AddConfigPath(c)
if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
dirs = append([]string{filepath.Join(c, "glow")}, dirs...)
}
if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
viper.AddConfigPath(filepath.Join(c, "glow"))
if c := os.Getenv("GLOW_CONFIG_HOME"); c != "" {
dirs = append([]string{c}, dirs...)
}
for _, v := range dirs {
@ -428,8 +428,11 @@ func tryLoadConfigFromDefaultPlaces() {
return
}
if viper.ConfigFileUsed() == "" {
configFile = filepath.Join(dirs[0], "glow.yml")
}
if err := ensureConfigFile(); err != nil {
fmt.Println("Could not create default configuration.")
log.Error("Could not create default configuration", "error", err)
os.Exit(1)
}
}