-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
127 lines (105 loc) · 2.68 KB
/
base.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
package drone
import (
"strings"
"time"
"github.com/goexl/gox"
"github.com/goexl/gox/field"
"github.com/goexl/simaqian"
)
var _ Config = (*Base)(nil)
// Base 插件基础
type Base struct {
simaqian.Logger
// 是否启用默认配置
Defaults bool `default:"${PLUGIN_DEFAULTS=${DEFAULTS=true}}"`
// 是否显示详细信息
Verbose bool `default:"${PLUGIN_VERBOSE=${VERBOSE=false}}"`
// 是否显示调试信息
Debug bool `default:"${PLUGIN_DEBUG=${DEBUG=false}}"`
// 是否在出错时打印输出
Pwe bool `default:"${PLUGIN_PWE=${PWE=true}}"`
// 是否重试
Retry bool `default:"${PLUGIN_RETRY=${RETRY=true}}"`
// 重试次数
Counts int `default:"${PLUGIN_COUNTS=${COUNTS=5}}"`
// 重试间隔
Backoff time.Duration `default:"${PLUGIN_BACKOFF=${BACKOFF=5s}}"`
}
func (b *Base) Parse(to map[string]string, configs ...string) {
for _, config := range configs {
b.parse(config, b.put(to))
}
}
func (b *Base) Parses(to map[string][]string, configs ...string) {
for _, config := range configs {
b.parse(config, b.puts(to))
}
}
func (b *Base) Setup() (unset bool, err error) {
unset = true
err = nil
return
}
func (b *Base) Fields() gox.Fields {
return gox.Fields{
field.Bool(`defaults`, b.Defaults),
field.Bool(`verbose`, b.Verbose),
field.Bool(`debug`, b.Debug),
field.Bool(`retry`, b.Retry),
field.Int(`counts`, b.Counts),
field.Duration(`backoff`, b.Backoff),
}
}
func (b *Base) BaseConfig() *Base {
return b
}
func (b *Base) parse(original string, put func(configs []string)) {
var _configs []string
defer func() {
put(_configs)
}()
if _configs = strings.Split(original, "@"); 2 <= len(_configs) {
return
}
if _configs = strings.Split(original, "=>"); 2 <= len(_configs) {
return
}
if _configs = strings.Split(original, "->"); 2 <= len(_configs) {
return
}
if _configs = strings.Split(original, " "); 2 <= len(_configs) {
return
}
}
func (b *Base) puts(cache map[string][]string) func(configs []string) {
return func(configs []string) {
if nil != configs && 2 <= len(configs) {
value := strings.TrimSpace(configs[1])
if `` == value {
return
}
cache[strings.TrimSpace(configs[0])] = b.splits(value, `,`, `|`, `||`)
}
}
}
func (b *Base) put(cache map[string]string) func(configs []string) {
return func(configs []string) {
if nil != configs && 2 <= len(configs) {
value := strings.TrimSpace(configs[1])
if `` == value {
return
}
cache[strings.TrimSpace(configs[0])] = value
}
}
}
func (b *Base) splits(config string, seps ...string) (configs []string) {
configs = []string{config}
for _, sep := range seps {
if strings.Contains(config, sep) {
configs = strings.Split(config, sep)
break
}
}
return
}