multiple updates
This commit is contained in:
64
internal/config/initialize.go
Normal file
64
internal/config/initialize.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"pihole-blocklist/bind/assets"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func Init() Config {
|
||||
cfg := New()
|
||||
|
||||
cfgInfo, err := getStructInfo(&cfg)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Unable to initialize program: %v", err))
|
||||
}
|
||||
|
||||
// get command line flags
|
||||
if err := cfg.parseFlags(cfgInfo); err != nil {
|
||||
panic(fmt.Sprintf("Unable to initialize program: %v", err))
|
||||
}
|
||||
|
||||
// set logging Level
|
||||
setLogLevel(&cfg)
|
||||
|
||||
// set timezone & time format
|
||||
cfg.TZUTC, _ = time.LoadLocation("UTC")
|
||||
cfg.TZLocal, err = time.LoadLocation(cfg.TimeZoneLocal)
|
||||
if err != nil {
|
||||
cfg.Log.Error("Unable to parse timezone string", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// check config file
|
||||
if !FileExists(cfg.ConfigFileLocation) {
|
||||
if _, err := WriteFile(cfg.ConfigFileLocation, assets.Config); err != nil {
|
||||
cfg.Log.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
cfg.Log.Error("Unable to locate configuration file, an example config file has been written", "path", cfg.ConfigFileLocation)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// read config
|
||||
cfData, err := ReadFile(cfg.ConfigFileLocation)
|
||||
if err != nil {
|
||||
cfg.Log.Error("Unable to read config file", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// unmarshal config file
|
||||
if err := yaml.Unmarshal(cfData, &cfg.ConfigFile); err != nil {
|
||||
cfg.Log.Error("Unable to read config file contents", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// print running config
|
||||
printRunningConfig(&cfg, cfgInfo)
|
||||
|
||||
// return configuration
|
||||
return cfg
|
||||
}
|
Reference in New Issue
Block a user