-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.go
80 lines (74 loc) · 1.75 KB
/
helper.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
package main
import (
"fmt"
"os"
"strings"
logging "github.com/op/go-logging"
)
func trimAndReplace(s string) string {
s = strings.Trim(s, " ")
old := s
s = strings.Replace(s, " ", "_", -1)
s = strings.Replace(s, "-", "_", -1)
new := s
if new != old {
fmt.Printf("WARN: parsed invalid config '%v' to valid '%v'. Change this in your config file!\n", old, new)
}
return s
}
func trimAndReplaceRef(s *string) {
*s = trimAndReplace(*s)
// *s = strings.Trim(*s, " ")
// *s = strings.Replace(*s, " ", "_", -1)
}
func getKeyValue(input string, sep string) (string, string) {
s := strings.Split(input, sep)
key := trimAndReplace(s[0])
val := trimAndReplace(s[1])
return key, val
}
func getKeysFromArray(input []string, sep string) []string {
var output []string
for _, kv := range input {
k, _ := getKeyValue(kv, sep)
output = append(output, k)
}
return output
}
func getValuesFromArray(input []string, sep string) []string {
var output []string
for _, kv := range input {
_, v := getKeyValue(kv, sep)
output = append(output, v)
}
return output
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func getLogLevel(input string) logging.Level {
if input == "" {
return logging.NOTICE
}
if strings.EqualFold(input, "debug") {
return logging.DEBUG
} else if strings.EqualFold(input, "info") {
return logging.INFO
} else if strings.EqualFold(input, "notice") {
return logging.NOTICE
} else if strings.EqualFold(input, "warn") {
return logging.WARNING
} else if strings.EqualFold(input, "error") {
return logging.ERROR
} else if strings.EqualFold(input, "critical") {
return logging.CRITICAL
}
return logging.NOTICE
}