-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
176 lines (159 loc) · 3.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"errors"
"log"
"os"
"path/filepath"
"github.com/natefinch/lumberjack"
"gopkg.in/yaml.v2"
)
const (
BACKUP_EXT = ".bak"
EXT_YAML = ".yaml"
EXT_JSON = ".json"
USE_CONFIG_YML = true
DATA_EXT = ".data"
LOG_EXT = ".log"
)
var (
ErrTokenUndefined = errors.New("token is empty")
ErrMasterUndefined = errors.New("master id is not set")
)
func RealPath(path string) string {
r, _ := filepath.Abs(filepath.FromSlash(path))
return r
}
func TrimLastExt(fname string) string {
name := filepath.Base(fname)
ext := filepath.Ext(name)
lfname := len(fname)
lname := len(name)
lext := len(ext)
if lext == 0 || lext == lname {
return fname
}
return fname[0 : lfname-lext]
}
func ExeName() string {
return RealPath(os.Args[0])
}
func AppDir() string {
return filepath.Dir(RealPath(ExeName()))
}
func AppBaseFileName() string {
return TrimLastExt(ExeName())
}
func GetConfigName() string {
pfx := AppBaseFileName() + "-config"
if USE_CONFIG_YML {
return pfx + EXT_YAML
} else {
return pfx + EXT_JSON
}
}
func GetLogName() string {
return AppBaseFileName() + LOG_EXT
}
func SetupLogger() {
log.SetOutput(&lumberjack.Logger{
Filename: GetLogName(),
MaxSize: 10, // megabytes
MaxBackups: 10,
MaxAge: 28, //days
})
}
func CheckDatadir() {
if config.Data_Dir != "" {
if err := os.MkdirAll(config.Data_Dir, 0755); err != nil {
log.Fatalf("mkDirAll(%s) got error: %s", config.Data_Dir, err)
}
}
}
func LoadConfig() error {
cfname := GetConfigName()
f, err := os.Open(cfname)
if err != nil {
return err
}
defer f.Close()
if USE_CONFIG_YML {
err = yaml.NewDecoder(f).Decode(&config)
} else {
err = json.NewDecoder(f).Decode(&config)
}
if err != nil {
return err
}
config.Host, err = os.Hostname()
if err != nil {
log.Printf("os.Hostname() error: %s", err)
config.Host = "unknown"
}
if config.Data_Dir == "" {
config.Data_Dir = AppDir() + "/data"
CheckDatadir()
SaveConfig()
}
return nil
}
func MakeBackup(fname string) {
if _, err := os.Stat(fname); os.IsNotExist(err) {
return
}
// make backup
fname_backup := fname + BACKUP_EXT
if _, err := os.Stat(fname_backup); !os.IsNotExist(err) {
err = os.Remove(fname_backup)
if err != nil {
log.Fatalf("Remove(%s) failed: %s", fname_backup, err)
}
}
if err := os.Rename(fname, fname_backup); err != nil {
log.Fatalf("Rename(%s, %s) failed: %s", fname, fname_backup, err)
}
}
func SaveConfig() error {
cfname := GetConfigName()
MakeBackup(cfname)
f, err := os.Create(cfname)
defer f.Close()
if err != nil {
return err
}
if USE_CONFIG_YML {
err = yaml.NewEncoder(f).Encode(config)
} else {
err = json.NewEncoder(f).Encode(config)
}
return err
}
func ValidateConfig() error {
if config.Token == "" {
return ErrTokenUndefined
}
if config.Master == 0 {
return ErrMasterUndefined
}
dirty := false
if config.Exec.Interval == 0 {
config.Exec.Interval = DEFAULT_SEND_INTERVAL
dirty = true
}
if config.Exec.PageSize == 0 {
config.Exec.PageSize = DEFAULT_SEND_PAGE_SIZE
dirty = true
}
if config.Exec.Timeout == 0 {
config.Exec.Timeout = DEFAULT_EXEC_TIMEOUT
dirty = true
}
if config.Exec.MaxPages == 0 {
config.Exec.MaxPages = DEFAULT_SEND_MAX_PAGES
dirty = true
}
if dirty {
SaveConfig()
}
return nil
}