some library updates and adds support for adblock lists
This commit is contained in:
@@ -2,63 +2,62 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"pihole-blocklist/bind/assets"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"github.com/goccy/go-yaml"
|
||||
|
||||
"gitlab.smoothnet.org/nhyatt/bind-response-policy-zone-creator/assets"
|
||||
"gitlab.smoothnet.org/nhyatt/bind-response-policy-zone-creator/internal/common"
|
||||
"gitlab.smoothnet.org/nhyatt/bind-response-policy-zone-creator/internal/log"
|
||||
)
|
||||
|
||||
func Init() Config {
|
||||
func Init() (Config, error) {
|
||||
cfg := New()
|
||||
|
||||
// parse config structure
|
||||
cfgInfo, err := getStructInfo(&cfg)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Unable to initialize program: %v", err))
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// get command line flags
|
||||
if err := cfg.parseFlags(cfgInfo); err != nil {
|
||||
panic(fmt.Sprintf("Unable to initialize program: %v", err))
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// set logging Level
|
||||
setLogLevel(&cfg)
|
||||
log.Init("text")
|
||||
log.SetNumericLevel(cfg.LogLevel)
|
||||
|
||||
// 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)
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// check config file
|
||||
if !FileExists(cfg.ConfigFileLocation) {
|
||||
if _, err := WriteFile(cfg.ConfigFileLocation, assets.Config); err != nil {
|
||||
cfg.Log.Error(err.Error())
|
||||
os.Exit(1)
|
||||
if !common.FileExists(cfg.ConfigFileLocation) {
|
||||
if _, err := common.WriteFile(cfg.ConfigFileLocation, assets.Config); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
cfg.Log.Error("Unable to locate configuration file, an example config file has been written", "path", cfg.ConfigFileLocation)
|
||||
os.Exit(1)
|
||||
return Config{}, fmt.Errorf("Unable to locate configuration file, an example config file has been written to %s", cfg.ConfigFileLocation)
|
||||
}
|
||||
|
||||
// read config
|
||||
cfData, err := ReadFile(cfg.ConfigFileLocation)
|
||||
cfData, err := common.ReadFile(cfg.ConfigFileLocation)
|
||||
if err != nil {
|
||||
cfg.Log.Error("Unable to read config file", "error", err)
|
||||
os.Exit(1)
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// print running config
|
||||
printRunningConfig(&cfg, cfgInfo)
|
||||
|
||||
// return configuration
|
||||
return cfg
|
||||
return cfg, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user