-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
124 lines (107 loc) · 2.34 KB
/
conf.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
package tigerbalm
import (
"flag"
"fmt"
"io/ioutil"
"time"
"github.com/jumboframes/tigerbalm/frame/capal/tblog"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"gopkg.in/yaml.v2"
)
var (
Conf *Config
RotateLog *rotatelogs.RotateLogs
h bool
file string
defaultFile string = "./tigerbalm.yaml"
)
type Config struct {
Web struct {
Addr string `yaml:"addr"`
} `yaml:"web"`
Kafka struct {
Enable bool `yaml:"enable"`
Brokers []string `yaml:"brokers"`
Consumer struct { //dumped from sarama
Group struct {
Session struct {
Timeout time.Duration `yaml:"timeout"`
} `yaml:"session`
Heartbeat struct {
Interval time.Duration `yaml:"interval"`
} `yaml:"heartbeat"`
} `yaml:"group"`
Offsets struct {
Initial int64 `yaml:"initial"`
} `yaml:"offsets"`
} `yaml:"consumer"`
} `yaml:"kafka"`
Plugin struct {
Path string `yaml:"path"`
WatchPath bool `yaml:"watch_path"`
Log struct {
Enable bool `yaml:"enable"`
Path string `yaml:"path"`
Level string `yaml:"level"`
MaxSize int64 `yaml:"maxsize"`
MaxRolls uint `yaml:"maxrolls"`
} `yaml:"log"`
} `yaml:"plugin"`
Log struct {
Level string `yaml:"level"`
File string `yaml:"file"`
MaxSize int64 `yaml:"maxsize"`
MaxRolls uint `yaml:"maxrolls"`
} `yaml:"log"`
Env []struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
} `yaml:"env"`
}
func Init() error {
time.LoadLocation("Asia/Shanghai")
err := initCmd()
if err != nil {
return err
}
err = initConf()
if err != nil {
return err
}
err = initLog()
return err
}
func initCmd() error {
flag.StringVar(&file, "f", defaultFile, "configuration file")
flag.BoolVar(&h, "h", false, "help")
flag.Parse()
if h {
flag.Usage()
return fmt.Errorf("invalid usage for command line")
}
return nil
}
func initConf() error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
Conf = &Config{}
err = yaml.Unmarshal([]byte(data), Conf)
return err
}
func initLog() error {
level, err := tblog.ParseLevel(Conf.Log.Level)
if err != nil {
return err
}
tblog.SetLevel(level)
RotateLog, err = rotatelogs.New(Conf.Log.File,
rotatelogs.WithRotationCount(Conf.Log.MaxRolls),
rotatelogs.WithRotationSize(Conf.Log.MaxSize))
if err != nil {
return err
}
tblog.SetOutput(RotateLog)
return nil
}