From c9ac76e40bd2156d83fa2784107cf6f33ce502ad Mon Sep 17 00:00:00 2001 From: fanyx Date: Fri, 9 Apr 2021 00:26:11 +0200 Subject: [PATCH] changes to loading config defaults --- main.go | 2 +- pkg/config/config.go | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 8f3143e..63de945 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( func main() { // initiate config - var conf config.AppConfig + var conf = config.LoadDefaults() conf.ReadConfig() //fmt.Printf("%#v\n", conf) diff --git a/pkg/config/config.go b/pkg/config/config.go index 32b26e5..d875109 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -7,15 +7,9 @@ import ( yaml "gopkg.in/yaml.v3" ) -const CONFIGFILE = "config.yaml" - -const DEFAULTCONFIG = ` -trackmania: - host: "127.0.0.1" - port: 5000 -api: - port: 5111 -` +var ( + CONFIGFILE = "config.yaml" +) type TrackmaniaConfig struct { Host string @@ -32,11 +26,6 @@ type AppConfig struct { } func (c *AppConfig) ReadConfig() { - // read default config - err := yaml.Unmarshal([]byte(DEFAULTCONFIG), c) - if err != nil { - log.Fatal(err) - } log.Print("Initiated the default config") // open config.yaml yamlFile, err := ioutil.ReadFile(CONFIGFILE) @@ -51,3 +40,11 @@ func (c *AppConfig) ReadConfig() { } log.Print("Read config from yaml File") } + +func LoadDefaults() AppConfig { + var c AppConfig + c.Trackmania.Host = "127.0.0.1" + c.Trackmania.Port = 5000 + c.Api.Port = 5111 + return c +}