-
Notifications
You must be signed in to change notification settings - Fork 2
/
rubberneck.go
181 lines (153 loc) · 5.06 KB
/
rubberneck.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package rubberneck
import (
"fmt"
"reflect"
"regexp"
"strings"
)
var (
expr *regexp.Regexp
)
// AddLineFeed will direct the Printer to add line feeds at the end of each
// output. NoAddLineFeed will not. This is useful for controlling the display
// of output when functions exhibit different behavior. For example, fmt.Printf
// wants line feeds added, whereas logrus.Infof does not.
const (
AddLineFeed = iota
NoAddLineFeed = iota
)
func init() {
expr = regexp.MustCompile("^[a-z]")
}
// Conforms to the signature used by fmt.Printf and log.Printf among many
// functions available in other packages.
type PrinterFunc func(format string, v ...interface{})
// MaskFunc takes a config argument and returns a string that is used to mask
// config values. This is useful to redact passwords etc.
type MaskFunc func(argument string) *string
// MaskWithValueFunc is the same as MaskFunc but provides the original value to
// the masking function, which is expected to identify the type itself.
type MaskWithValueFunc func(argument string, value interface{}) *string
// Printer defines the signature of a function that can be used to display the
// configuration. This signature is used by fmt.Printf, log.Printf, various
// logging output levels from the logrus package, and others.
type Printer struct {
Show PrinterFunc
Mask MaskFunc
MaskWithValue MaskWithValueFunc
}
func addLineFeed(fn PrinterFunc) PrinterFunc {
return func(format string, v ...interface{}) {
format = format + "\n"
fn(format, v...)
}
}
// NewDefaultPrinter returns a Printer configured to write to stdout.
func NewDefaultPrinter() *Printer {
return &Printer{
Show: func(format string, v ...interface{}) {
fmt.Printf(format+"\n", v...)
},
}
}
// NewPrinter returns a Printer configured to use the supplied function to
// output to the supplied function.
func NewPrinter(fn PrinterFunc, lineFeed int) *Printer {
p := &Printer{Show: fn}
if lineFeed == AddLineFeed {
p.Show = addLineFeed(fn)
}
return p
}
// NewPrinterWithKeyMasking returns a Printer configured to use printer
// function `fn` for output and the masking function `mk` for redacting certain
// keys.
func NewPrinterWithKeyMasking(fn PrinterFunc, mk MaskFunc, lineFeed int) *Printer {
p := NewPrinter(fn, lineFeed)
p.Mask = mk
return p
}
// NewPrinterWithKeyValueMasking returns a Printer configured to use printer
// function `fn` for output and the value masking function `mk` for redacting
// certain keys.
func NewPrinterWithKeyValueMasking(fn PrinterFunc, mk MaskWithValueFunc, lineFeed int) *Printer {
p := NewPrinter(fn, lineFeed)
p.MaskWithValue = mk
return p
}
// Print attempts to pretty print the contents of each obj in a format suitable
// for displaying the configuration of an application on startup. It uses a
// default label of 'Settings' for the output.
func (p *Printer) Print(objs ...interface{}) {
// Add some protection against accidentally providing this method with a
// label.
if len(objs) > 0 {
switch objs[0].(type) {
case string:
p.Show(" *** Expected to print a struct, got: '%s' ***", objs[0])
return
}
}
p.PrintWithLabel("Settings", objs...)
}
// PrintWithLabel attempts to pretty print the contents of each obj in a format
// suitable for displaying the configuration of an application on startup. It
// takes a label argument which is a string to be printed into the title bar in
// the output.
func (p *Printer) PrintWithLabel(label string, objs ...interface{}) {
labelLen := 50
if len(label) > 50 {
labelLen = len(label) + 1
}
p.Show("%s %s", label, strings.Repeat("-", labelLen-len(label)-1))
for _, obj := range objs {
p.processOne(reflect.ValueOf(obj), 0)
}
p.Show("%s", strings.Repeat("-", labelLen))
}
func (p *Printer) processOne(value reflect.Value, indent int) {
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
t := value.Type()
for i := 0; i < value.NumField(); i++ {
name := t.Field(i).Name
// Other methods of detecting unexported fields seem unreliable or
// different between Go versions and Go compilers (gc vs gccgo)
if expr.MatchString(name) {
continue
}
field := value.Field(i)
if field.Kind() == reflect.Ptr {
field = field.Elem()
}
switch field.Kind() {
case reflect.Struct:
p.Show(" %s * %s:", strings.Repeat(" ", indent), name)
p.processOne(reflect.ValueOf(field.Interface()), indent+1)
default:
var val interface{}
// Elem() returns a Zero value when field is nil. So IsValid() and
// CanInterface() protect us from calling Interface()
// inappropriately.
if field.IsValid() && field.CanInterface() {
val = field.Interface()
}
if p.Mask != nil {
if maskedValue := p.Mask(name); maskedValue != nil {
val = *maskedValue
}
} else if p.MaskWithValue != nil {
if maskedValue := p.MaskWithValue(name, val); maskedValue != nil {
val = *maskedValue
}
}
p.Show(" %s * %s: %v", strings.Repeat(" ", indent), name, val)
}
}
}
// Print configures a default printer to output to stdout and then prints the
// object.
func Print(obj interface{}) {
NewDefaultPrinter().Print(obj)
}