-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
218 lines (173 loc) · 4.95 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package libconfd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/BurntSushi/toml"
)
type Config struct {
// The path to confd configs.
// If the confdir is rel path, must convert to abs path.
//
// abspath = filepath.Join(ConfigPath, Config.ConfDir)
//
ConfDir string `toml:"confdir" json:"confdir"`
// The backend polling interval in seconds. (10)
Interval int `toml:"interval" json:"interval"`
// Enable noop mode. Process all template resources; skip target update.
Noop bool `toml:"noop" json:"noop"`
// The string to prefix to keys. ("/")
Prefix string `toml:"prefix" json:"prefix"`
// sync without check_cmd and reload_cmd.
SyncOnly bool `toml:"sync_only" json:"sync_only"`
// level which confd should log messages
// DEBUG/INFO/WARN/ERROR/PANIC
LogLevel string `toml:"log_level" json:"log_level"`
// run once and exit
Onetime bool `toml:"onetime" json:"onetime"`
// enable watch support
Watch bool `toml:"watch" json:"watch"`
// keep staged files
KeepStageFile bool `toml:"keep_stage_file" json:"keep_stage_file"`
// PGP secret keyring (for use with crypt functions)
PGPPrivateKey string `toml:"pgp_private_key" json:"pgp_private_key"`
// ----------------------------------------------------
FuncMap template.FuncMap `toml:"-" json:"-"`
FuncMapUpdater func(m template.FuncMap, basefn *TemplateFunc) `toml:"-" json:"-"`
HookAbsKeyAdjuster func(absKey string) (realKey string) `toml:"-" json:"-"`
HookOnCheckCmdDone func(trName, cmd string, err error) `toml:"-" json:"-"`
HookOnReloadCmdDone func(trName, cmd string, err error) `toml:"-" json:"-"`
HookOnUpdateDone func(trName string, err error) `toml:"-" json:"-"`
}
const defaultConfigContent = `
# The path to confd configs.
# If the confdir is rel path, must convert to abs path.
#
# abspath = filepath.Join(ConfigPath, Config.ConfDir)
#
confdir = "confd"
# The backend polling interval in seconds. (10)
interval = 10
# Enable noop mode. Process all template resources; skip target update.
noop = false
# The string to prefix to keys. ("/")
prefix = "/"
# sync without check_cmd and reload_cmd.
sync-only = true
# level which confd should log messages ("DEBUG")
log-level = "DEBUG"
# run once and exit
onetime = true
# enable watch support
watch = false
# the TOML backend file to watch for changes
file = "./confd/backend-file.toml"
# keep staged files
keep-stage-file = false
# PGP secret keyring (for use with crypt functions)
pgp-private-key = ""
`
func newDefaultConfig() (p *Config) {
p = new(Config)
_, err := toml.Decode(defaultConfigContent, p)
if err != nil {
GetLogger().Panic(err)
}
if !filepath.IsAbs(p.ConfDir) {
absdir, err := filepath.Abs(".")
if err != nil {
GetLogger().Panic(err)
}
p.ConfDir = filepath.Clean(filepath.Join(absdir, p.ConfDir))
}
return
}
func MustLoadConfig(path string) *Config {
p, err := LoadConfig(path)
if err != nil {
GetLogger().Fatal(err)
}
return p
}
func LoadConfig(path string) (p *Config, err error) {
p = new(Config)
_, err = toml.DecodeFile(path, p)
if err != nil {
return nil, err
}
if !filepath.IsAbs(p.ConfDir) {
absdir, err := filepath.Abs(filepath.Dir(path))
if err != nil {
return nil, err
}
p.ConfDir = filepath.Clean(filepath.Join(absdir, p.ConfDir))
}
return p, nil
}
func LoadConfigFromJsonString(s string) (p *Config, err error) {
p = new(Config)
if err := json.Unmarshal([]byte(s), p); err != nil {
return nil, err
}
if !filepath.IsAbs(p.ConfDir) {
err = fmt.Errorf("libconfd: ConfDir is not abs path: %s", p.ConfDir)
return
}
return p, nil
}
func (p *Config) Valid() error {
if !filepath.IsAbs(p.ConfDir) {
return fmt.Errorf("ConfDir is not abs path: %s", p.ConfDir)
}
if !dirExists(p.ConfDir) {
return fmt.Errorf("ConfDir not exists: %s", p.ConfDir)
}
if p.Interval < 0 {
return fmt.Errorf("invalid Interval: %d", p.Interval)
}
if p.LogLevel != "" && !newLogLevel(p.LogLevel).Valid() {
return fmt.Errorf("invalid LogLevel: %s", p.LogLevel)
}
return nil
}
func (p *Config) Save(name string) error {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(p); err != nil {
return err
}
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(buf.String()); err != nil {
return err
}
return nil
}
func (p *Config) Clone() *Config {
q := *p
// clone map
if p.FuncMap != nil {
q.FuncMap = make(template.FuncMap)
for k, v := range p.FuncMap {
q.FuncMap[k] = v
}
}
return &q
}
func (p *Config) GetConfigDir() string {
return filepath.Join(p.ConfDir, "conf.d")
}
func (p *Config) GetTemplateDir() string {
return filepath.Join(p.ConfDir, "templates")
}
func (p *Config) GetDefaultTemplateOutputDir() string {
return filepath.Join(p.ConfDir, "templates_output")
}