-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
154 lines (134 loc) · 3.38 KB
/
util.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
package springboot
import (
"bufio"
"bytes"
"fmt"
"github.com/golang/mock/gomock"
"gopkg.in/yaml.v3"
"reflect"
"regexp"
"strings"
)
func New[T any]() T {
var t T
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Pointer {
return reflect.New(typ.Elem()).Interface().(T)
}
return reflect.New(typ).Elem().Interface().(T)
}
func ParseYaml(content string) (map[string]interface{}, error) {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(content), m)
if err != nil {
return nil, err
}
return m, nil
}
func ParseProperties(content string) map[string]string {
s := bufio.NewScanner(bytes.NewBufferString(content))
var m = make(map[string]string)
for s.Scan() {
line := s.Text()
idx := strings.Index(line, "=")
if idx >= 0 {
key := strings.TrimSpace(line[:idx])
value := strings.TrimSpace(line[idx+1:])
m[key] = value
} else {
m[line] = ""
}
}
return m
}
func flatten(m map[string]interface{}) map[string]interface{} {
var result = make(map[string]interface{})
var f func(parent string, m map[string]interface{}, result map[string]interface{})
f = func(parent string, m map[string]interface{}, result map[string]interface{}) {
for k, v := range m {
var key string
if len(parent) > 0 {
key = strings.Join([]string{parent, k}, ".")
} else {
key = k
}
switch v.(type) {
case map[string]interface{}:
f(key, v.(map[string]interface{}), result)
default:
result[key] = v
}
}
}
f("", m, result)
return result
}
func GetConfigFromYaml[T any](key, content string) (T, bool) {
var zero T
cfg, err := ParseYaml(content)
if err != nil {
return zero, false
}
cfg = flatten(cfg)
if find, ok := cfg[key].(T); ok {
return find, true
}
return zero, false
}
func GetConfigFromProperties(key, content string) (string, bool) {
cfg := ParseProperties(content)
if find, ok := cfg[key]; ok {
return find, len(find) > 0
}
return "", false
}
func CleanOutput(raw string) string {
var value = raw
value = strings.TrimSpace(value)
value = strings.TrimSuffix(value, "\r\n")
value = strings.TrimSuffix(value, "\n")
value = strings.ReplaceAll(value, "\"", "")
value = strings.ReplaceAll(value, "'", "")
return value
}
func Contains[T ~string](slices []T, find T) bool {
for _, t := range slices {
if t == find {
return true
}
}
return false
}
type tryFunc[In any, Out any] func(in In) (Out, bool)
type tryFuncs[In any, Out any] []tryFunc[In, Out]
func (ts tryFuncs[In, Out]) try(in In) (Out, bool) {
var zero Out
for _, f := range ts {
if value, ok := f(in); ok {
return value, true
}
}
return zero, false
}
type cmdMatcher struct {
cmd string
}
func (c cmdMatcher) Matches(x interface{}) bool {
s := x.(string)
pattern := regexp.QuoteMeta(c.cmd)
pattern = strings.ReplaceAll(pattern, "%%d", "^^d") // a dirty trick, we need to replace %% to ^^ to keep it as-is
pattern = strings.ReplaceAll(pattern, "%d", "[0-9]+")
pattern = strings.ReplaceAll(pattern, "%\\[1\\]d", "[0-9]+")
pattern = strings.ReplaceAll(pattern, "%f", "[0-9\\.]+")
pattern = strings.ReplaceAll(pattern, "%s", "[0-9a-zA-Z\\-_\\./]+")
pattern = strings.ReplaceAll(pattern, "^^d", "%d")
r := regexp.MustCompile(pattern)
find := r.FindString(s)
return strings.Compare(s, find) == 0
}
func (c cmdMatcher) String() string {
return fmt.Sprintf("has cmd: %s", c.cmd)
}
func CmdMatcher(cmd string) gomock.Matcher {
return &cmdMatcher{cmd: cmd}
}