-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
54 lines (43 loc) · 967 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
48
49
50
51
52
53
54
package main
import (
"regexp"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
type regex struct {
regexp.Regexp
}
type profile struct {
RegEx []regex
Public bool
}
type serverConfig struct {
Addr string
LogLevel log.Level
}
// Config represents configuration for the application
type Config struct {
URL string
Profiles map[string]profile
Server serverConfig
}
func (r *regex) UnmarshalText(text []byte) error {
tmpRe, err := regexp.Compile("(?i)" + string(text))
r.Regexp = *tmpRe
return err
}
// ParseConfig reads config from path and returns a Config struct
func ParseConfig(path string) (Config, error) {
var tmpConfig Config
if _, err := toml.DecodeFile(path, &tmpConfig); err != nil {
return tmpConfig, err
}
// defaults
if tmpConfig.Server.Addr == "" {
tmpConfig.Server.Addr = ":8080"
}
if tmpConfig.Server.LogLevel == 0 {
tmpConfig.Server.LogLevel = log.InfoLevel
}
return tmpConfig, nil
}