-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
125 lines (118 loc) · 2.86 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2017 Orange
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
)
func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("%s: unknown fields: %s", ctx, strings.Join(keys, ", "))
}
return nil
}
func Load(s string) (*Config, error) {
cfg := &Config{}
err := yaml.Unmarshal([]byte(s), cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func LoadFile(filename string) (*Config, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg, err := Load(string(content))
if err != nil {
return nil, err
}
return cfg, nil
}
type Config struct {
KairosUrl string `yaml:"kairos_url"`
SkipInsecure bool `yaml:"skip_insecure"`
ListenAddr string `yaml:"listen_addr"`
LogLevel string `yaml:"log_level"`
LogJson bool `yaml:"log_json"`
NoColor bool `yaml:"no_color"`
Workers int `yaml:"workers"`
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Config
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.KairosUrl == "" {
return fmt.Errorf("Config: kairos_url must be set")
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
if c.ListenAddr == "" {
c.ListenAddr = "0.0.0.0:" + port
}
if len(strings.Split(c.ListenAddr, ":")) < 2 {
c.ListenAddr += ":" + port
}
if c.Workers <= 0 {
c.Workers = 5
}
err := checkOverflow(c.XXX, "Config")
if err != nil {
return err
}
c.loadLogConfig()
return nil
}
func (c Config) loadLogConfig() {
if c.LogJson {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{
DisableColors: c.NoColor,
})
}
if c.LogLevel == "" {
return
}
switch strings.ToUpper(c.LogLevel) {
case "ERROR":
log.SetLevel(log.ErrorLevel)
return
case "WARN":
log.SetLevel(log.WarnLevel)
return
case "DEBUG":
log.SetLevel(log.DebugLevel)
return
case "PANIC":
log.SetLevel(log.PanicLevel)
return
case "FATAL":
log.SetLevel(log.FatalLevel)
return
}
}