-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport.go
52 lines (42 loc) · 1.35 KB
/
report.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 envstruct
import (
"fmt"
"io"
"os"
"reflect"
"strings"
"text/tabwriter"
)
var ReportWriter io.Writer = os.Stdout
// WriteReport will take a struct that is setup for envstruct and print
// out a report containing the struct field name, field type, environment
// variable for that field, whether or not the field is required and
// the value of that field. The report is written to `ReportWriter`
// which defaults to `os.StdOut`. Sensetive values that you would not
// want appearing in logs can be omitted with the `noreport` value in
// the `env` struct tag.
func WriteReport(t interface{}) error {
w := tabwriter.NewWriter(ReportWriter, 0, 8, 2, ' ', 0)
fmt.Fprintln(w, "FIELD NAME:\tTYPE:\tENV:\tREQUIRED:\tVALUE:")
val := reflect.ValueOf(t).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
tagProperties := extractSliceInputs(tag.Get("env"))
envVar := strings.ToUpper(tagProperties[indexEnvVar])
isRequired := tagPropertiesContains(tagProperties, tagRequired)
var displayedValue interface{} = valueField
if tagPropertiesContains(tagProperties, tagNoReport) {
displayedValue = "(OMITTED)"
}
fmt.Fprintln(w, fmt.Sprintf(
"%v\t%v\t%v\t%t\t%v",
typeField.Name,
valueField.Type(),
envVar,
isRequired,
displayedValue))
}
return w.Flush()
}