forked from openpitrix/libconfd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
87 lines (73 loc) · 1.65 KB
/
options.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
// 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 (
"text/template"
)
type Options func(*Config)
func (p *Config) applyOptions(opts ...Options) *Config {
for _, fn := range opts {
fn(p)
}
return p
}
func WithOnetimeMode() Options {
return func(opt *Config) {
opt.Onetime = true
}
}
func WithIntervalMode() Options {
return func(opt *Config) {
opt.Onetime = false
opt.Watch = false
}
}
func WithInterval(interval int) Options {
return func(opt *Config) {
opt.Interval = interval
}
}
func WithWatchMode() Options {
return func(opt *Config) {
opt.Onetime = false
opt.Watch = true
}
}
func WithFuncMap(maps ...template.FuncMap) Options {
return func(opt *Config) {
if opt.FuncMap == nil {
opt.FuncMap = make(template.FuncMap)
}
for _, m := range maps {
for k, fn := range m {
opt.FuncMap[k] = fn
}
}
}
}
func WithAbsKeyAdjuster(fn func(absKey string) (realKey string)) Options {
return func(opt *Config) {
opt.HookAbsKeyAdjuster = fn
}
}
func WithFuncMapUpdater(fn func(m template.FuncMap, basefn *TemplateFunc)) Options {
return func(opt *Config) {
opt.FuncMapUpdater = fn
}
}
func WithHookOnCheckCmdError(fn func(trName, cmd string, err error)) Options {
return func(opt *Config) {
opt.HookOnCheckCmdError = fn
}
}
func WithHookOnReloadCmdError(fn func(trName, cmd string, err error)) Options {
return func(opt *Config) {
opt.HookOnReloadCmdError = fn
}
}
func WithHookOnError(fn func(trName string, err error)) Options {
return func(opt *Config) {
opt.HookOnError = fn
}
}