forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_config.go
69 lines (55 loc) · 1.23 KB
/
dump_config.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
package common
import (
"fmt"
"github.com/mitchellh/reflectwalk"
"reflect"
"strings"
)
type walker struct {
depth int
say func(string)
}
func newDumpConfig(say func(string)) *walker {
return &walker{
depth: 0,
say: say,
}
}
func (s *walker) Enter(l reflectwalk.Location) error {
s.depth += 1
return nil
}
func (s *walker) Exit(l reflectwalk.Location) error {
s.depth -= 1
return nil
}
func (s *walker) Struct(v reflect.Value) error {
return nil
}
func (s *walker) StructField(f reflect.StructField, v reflect.Value) error {
if !s.shouldDump(v) {
return nil
}
switch v.Kind() {
case reflect.String:
s.say(fmt.Sprintf("%s=%s", f.Name, s.formatValue(f.Name, v.String())))
}
return nil
}
func (s *walker) shouldDump(v reflect.Value) bool {
return s.depth == 2 && v.IsValid() && v.CanInterface()
}
func (s *walker) formatValue(name, value string) string {
if s.isMaskable(name) {
return strings.Repeat("*", len(value))
}
return value
}
func (s *walker) isMaskable(name string) bool {
up := strings.ToUpper(name)
return strings.Contains(up, "SECRET") || strings.Contains(up, "PASSWORD")
}
func DumpConfig(config interface{}, say func(string)) {
walker := newDumpConfig(say)
reflectwalk.Walk(config, walker)
}