mirror of
https://github.com/MarekWojt/gertdns.git
synced 2025-12-13 20:29:51 +01:00
Don't panic! Propagate error
This commit is contained in:
@@ -16,6 +16,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Load(configFilePath string) {
|
func Load(configFilePath string) (*Configuration, error) {
|
||||||
Config = loadConfFile(configFilePath)
|
Config, err := loadConfFile(configFilePath)
|
||||||
|
return &Config, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfFile(configFilePath string) Configuration {
|
func loadConfFile(configFilePath string) (Configuration, error) {
|
||||||
bytes, err := ioutil.ReadFile(configFilePath)
|
bytes, err := ioutil.ReadFile(configFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -22,23 +22,23 @@ func loadConfFile(configFilePath string) Configuration {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
color.Errorln(err.Error())
|
color.Errorln(err.Error())
|
||||||
color.Errorln("Configuration file " + configFilePath + " could not be read as TOML file")
|
color.Errorln("Configuration file " + configFilePath + " could not be read as TOML file")
|
||||||
panic(err)
|
return defaultConfig, err
|
||||||
}
|
}
|
||||||
color.Infoln("Loaded configuration file " + configFilePath)
|
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)
|
confBytes, err := toml.Marshal(defaultConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Errorln(err.Error())
|
color.Errorln(err.Error())
|
||||||
color.Errorln("Default config struct is not TOML conform")
|
color.Errorln("Default config struct is not TOML conform")
|
||||||
panic(err)
|
return defaultConfig, err
|
||||||
}
|
}
|
||||||
err = os.WriteFile(configFilePath, confBytes, 0644)
|
err = os.WriteFile(configFilePath, confBytes, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Errorln(err.Error())
|
color.Errorln(err.Error())
|
||||||
color.Warnln("Using default config without file")
|
color.Warnln("Using default config without file")
|
||||||
}
|
}
|
||||||
return defaultConfig
|
return defaultConfig, err
|
||||||
}
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -15,9 +15,12 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config.Load(*configFile)
|
_, err := config.Load(*configFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load configuration: %s\n ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
err := dns.Run()
|
err = dns.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to start DNS server: %s\n ", err.Error())
|
log.Fatalf("Failed to start DNS server: %s\n ", err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user