-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
47 lines (41 loc) · 911 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"io"
"log"
"os"
"gopkg.in/yaml.v2"
)
// Config stores runtime configuration options for sea-snow.
type Config struct {
Server struct {
Port string `yaml:"port"`
Host string `yaml:"host"`
} `yaml:"server"`
WeatherAPIKey string `yaml:"weather_api_key"`
}
// ReadConf attempts to read a `config.yml` file in the working directory.
// If it cannot do so, it returns a default configuration.
func ReadConf() Config {
path := "config.yml"
f, err := os.Open(path)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
cfg, err := ParseConf(f)
if err != nil {
log.Fatalf("read config: %v", err)
}
return cfg
}
// ParseConf parses the YAML configuration data in r.
func ParseConf(r io.Reader) (Config, error) {
var cfg Config
decoder := yaml.NewDecoder(r)
err := decoder.Decode(&cfg)
if err != nil {
err = fmt.Errorf("decode: %w", err)
}
return cfg, err
}