Don't panic! Propagate error

This commit is contained in:
daru
2021-10-17 13:49:56 +02:00
parent 1d7dcbe3ba
commit 290a9af7f1
3 changed files with 14 additions and 10 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/pelletier/go-toml/v2"
)
func loadConfFile(configFilePath string) Configuration {
func loadConfFile(configFilePath string) (Configuration, error) {
bytes, err := ioutil.ReadFile(configFilePath)
if err != nil {
@@ -22,23 +22,23 @@ func loadConfFile(configFilePath string) Configuration {
if err != nil {
color.Errorln(err.Error())
color.Errorln("Configuration file " + configFilePath + " could not be read as TOML file")
panic(err)
return defaultConfig, err
}
color.Infoln("Loaded configuration file " + configFilePath)
return cfg
return cfg, err
}
func createNewConfig(configFilePath string) Configuration {
func createNewConfig(configFilePath string) (Configuration, error) {
confBytes, err := toml.Marshal(defaultConfig)
if err != nil {
color.Errorln(err.Error())
color.Errorln("Default config struct is not TOML conform")
panic(err)
return defaultConfig, err
}
err = os.WriteFile(configFilePath, confBytes, 0644)
if err != nil {
color.Errorln(err.Error())
color.Warnln("Using default config without file")
}
return defaultConfig
return defaultConfig, err
}