possible to read config.yaml

This commit is contained in:
fanyx 2021-04-06 17:11:10 +02:00
parent 6340f90a41
commit 2bf78209bc
2 changed files with 20 additions and 18 deletions

14
main.go
View File

@ -1,18 +1,12 @@
package main package main
import ( import (
"fmt" "git.fanyx.xyz/fanyx/goufy/pkg/config"
"log"
. "git.fanyx.xyz/fanyx/goufy"
) )
func main() { func main() {
config, err := readConfig() var c config.AppConfig
if err != nil { c.ReadConfig()
log.Fatal(err)
}
fmt.Printf("#%v", config)
// fmt.Printf("#%v", c)
} }

View File

@ -2,26 +2,34 @@ package config
import ( import (
"io/ioutil" "io/ioutil"
"log"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
const CONFIGFILE = "config.yaml" const CONFIGFILE = "config.yaml"
type Config struct { const DEFAULTCONFIG = `
host string `yaml:"host"` host: "127.0.0.1"
port int `yaml:"port"` 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) yamlFile, err := ioutil.ReadFile(CONFIGFILE)
if err != nil { if err != nil {
return nil, err log.Fatal(err)
} }
err = yaml.Unmarshal([]byte(yamlFile), c) err = yaml.Unmarshal([]byte(yamlFile), c)
if err != nil { if err != nil {
return nil, err log.Fatal(err)
} }
return c, nil
} }