forked from childe/gohangout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parser.go
52 lines (43 loc) · 1.24 KB
/
config_parser.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
package main
import (
"errors"
"regexp"
"strings"
"github.com/golang/glog"
yaml "gopkg.in/yaml.v2"
)
type Config map[string]interface{}
type Parser interface {
parse(filename string) (map[string]interface{}, error)
}
func parseConfig(filename string) (map[string]interface{}, error) {
lowerFilename := strings.ToLower(filename)
if strings.HasSuffix(lowerFilename, ".yaml") || strings.HasSuffix(lowerFilename, ".yml") {
yp := &YamlParser{}
return yp.parse(filename)
}
return nil, errors.New("unknown config format. config filename should ends with yaml|yml")
}
// remove sensitive info before output
func removeSensitiveInfo(config map[string]interface{}) string {
re := regexp.MustCompile(`(.*password:\s+)(.*)`)
re2 := regexp.MustCompile(`(http(s)?://\w+:)\w+`)
b, err := yaml.Marshal(config)
if err != nil {
glog.Errorf("marshal config error: %s", err)
return ""
}
output := make([]string, 0, 0)
for _, l := range strings.Split(string(b), "\n") {
if re.MatchString(l) {
output = append(output, re.ReplaceAllString(l, "${1}xxxxxx"))
continue
}
if re2.MatchString(l) {
output = append(output, re2.ReplaceAllString(l, "${1}xxxxxx"))
continue
}
output = append(output, l)
}
return strings.Join(output, "\n")
}