-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
42 lines (32 loc) · 840 Bytes
/
yaml.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
package configutils
import (
"strings"
"unicode"
)
func YAMLRemoveRootKeys(b []byte, keys ...string) []byte {
return yamlFilterRootKeys(b, keys, false)
}
func YAMLKeepRootKeys(b []byte, keys ...string) []byte {
return yamlFilterRootKeys(b, keys, true)
}
func yamlFilterRootKeys(b []byte, keys []string, reverse bool) []byte {
lines := strings.Split(string(b), "\n")
isIgnoring := false
for i, line := range lines {
isEmpty := len(strings.TrimSpace(line)) == 0
isRoot := strings.TrimLeftFunc(line, unicode.IsSpace) == line && !isEmpty
if isRoot {
isIgnoring = false
for _, key := range keys {
if strings.HasPrefix(line, key+":") {
isIgnoring = true
break
}
}
}
if (isIgnoring && !reverse) || (!isIgnoring && reverse) {
lines[i] = ""
}
}
return []byte(strings.Join(lines, "\n"))
}