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
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)
}

View File

@ -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
}