From 2bf78209bc59c18d6a06050f1c7383e97aa551f5 Mon Sep 17 00:00:00 2001 From: fanyx Date: Tue, 6 Apr 2021 17:11:10 +0200 Subject: [PATCH] possible to read config.yaml --- main.go | 14 ++++---------- pkg/config/config.go | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index ce9de48..e57a8fe 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,12 @@ package main import ( - "fmt" - "log" - - . "git.fanyx.xyz/fanyx/goufy" + "git.fanyx.xyz/fanyx/goufy/pkg/config" ) func main() { - config, err := readConfig() - if err != nil { - log.Fatal(err) - } - - fmt.Printf("#%v", config) + var c config.AppConfig + c.ReadConfig() + // fmt.Printf("#%v", c) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 0bdcc7f..f0fd027 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,26 +2,34 @@ package config import ( "io/ioutil" + "log" yaml "gopkg.in/yaml.v3" ) const CONFIGFILE = "config.yaml" -type Config struct { - host string `yaml:"host"` - port int `yaml:"port"` +const DEFAULTCONFIG = ` +host: "127.0.0.1" +port: 5000 +` + +type TrackmaniaConfig struct { + Host string + Port int } -func (c *Config) readConfig() (*Config, error) { +type AppConfig struct { + Trackmania TrackmaniaConfig +} + +func (c *AppConfig) ReadConfig() { yamlFile, err := ioutil.ReadFile(CONFIGFILE) if err != nil { - return nil, err + log.Fatal(err) } err = yaml.Unmarshal([]byte(yamlFile), c) if err != nil { - return nil, err + log.Fatal(err) } - - return c, nil }