-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
53 lines (42 loc) · 1.09 KB
/
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
48
49
50
51
52
53
package main
import (
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
"github.com/dominikschulz/vanity/server"
"github.com/go-kit/kit/log"
)
const defaultConfig = `
---
hosts:
`
// example.org -> go-import example.org/foo git https://github.com/example/foo
// example.org -> go-import example.org/bar hg https://code.google.com/p/bar
// Config holds the server configuration
type Config struct {
Hosts map[string]*server.Host `yaml:"hosts"`
}
func loadConfiguration(l log.Logger, cfgFile string) (Config, error) {
if l == nil {
l = log.NewNopLogger()
}
var err error
var buf []byte
if _, err := os.Stat(cfgFile); err == nil {
l.Log("level", "debug", "msg", "Loading config", "source", cfgFile)
buf, err = ioutil.ReadFile(cfgFile)
if err != nil {
l.Log("level", "error", "msg", "Could not load config", "source", cfgFile)
buf = []byte(defaultConfig)
}
} else {
l.Log("level", "error", "msg", "Loading default config due to error", "err", err)
buf = []byte(defaultConfig)
}
var cfg Config
err = yaml.Unmarshal(buf, &cfg)
if err != nil {
return Config{}, err
}
return cfg, nil
}