@@ -25,11 +25,14 @@ var (
25
25
)
26
26
27
27
const (
28
- stateDesc string = "Optional; the state JSON output by 'terraform show -json', read from stdin if omitted"
28
+ stateDesc string = "Optional; the state JSON output by 'terraform show -json'. Read from stdin if omitted"
29
29
stateFileDesc string = "Optional; the path to a local file containing 'terraform show -json' output"
30
- headingDesc string = "Optional; the heading text for use in the printed markdown"
30
+ headingDesc string = "Optional; the heading text for use in the printed output."
31
+ outputDesc string = "Optional; the output format. Supported values: md, html."
31
32
versionDesc string = "Print the current version and exit"
32
33
defaultHeading string = "Outputs"
34
+ defaultOutput string = "md"
35
+ sensitive string = "sensitive; redacted"
33
36
)
34
37
35
38
type data struct {
@@ -39,12 +42,25 @@ type data struct {
39
42
40
43
func value (output tfjson.StateOutput ) string {
41
44
if output .Sensitive {
42
- return " sensitive; redacted"
45
+ return sensitive
43
46
}
44
47
45
48
return fmt .Sprintf ("%v" , output .Value )
46
49
}
47
50
51
+ func prettyPrintValue (output tfjson.StateOutput ) template.HTML {
52
+ if output .Sensitive {
53
+ return template .HTML (sensitive )
54
+ }
55
+
56
+ pretty , err := json .MarshalIndent (output .Value , "" , " " )
57
+ if err != nil {
58
+ exit (err )
59
+ }
60
+
61
+ return template .HTML (string (pretty ))
62
+ }
63
+
48
64
func dataType (output tfjson.StateOutput ) string {
49
65
return fmt .Sprintf ("%T" , output .Value )
50
66
}
@@ -53,6 +69,7 @@ func main() {
53
69
stateJSON := flag .String ("state" , "" , stateDesc )
54
70
stateFile := flag .String ("state-file" , "" , stateFileDesc )
55
71
heading := flag .String ("heading" , defaultHeading , headingDesc )
72
+ output := flag .String ("output" , defaultOutput , outputDesc )
56
73
flag .Parse ()
57
74
58
75
args := flag .Args ()
@@ -84,10 +101,16 @@ func main() {
84
101
exit (err )
85
102
}
86
103
87
- t , err := template .New ("markdown.tmpl" ).Funcs (template.FuncMap {
88
- "value" : value ,
89
- "dataType" : dataType ,
90
- }).ParseFS (templates , "templates/markdown.tmpl" )
104
+ tmpl , err := getTemplatePath (* output )
105
+ if err != nil {
106
+ exit (err )
107
+ }
108
+
109
+ t , err := template .New (strings .Split (tmpl , "/" )[1 ]).Funcs (template.FuncMap {
110
+ "value" : value ,
111
+ "dataType" : dataType ,
112
+ "prettyPrint" : prettyPrintValue ,
113
+ }).ParseFS (templates , tmpl )
91
114
if err != nil {
92
115
exit (err )
93
116
}
@@ -106,6 +129,17 @@ func main() {
106
129
}
107
130
}
108
131
132
+ func getTemplatePath (output string ) (string , error ) {
133
+ switch output {
134
+ case "html" :
135
+ return "templates/html.tmpl" , nil
136
+ case "md" :
137
+ return "templates/markdown.tmpl" , nil
138
+ default :
139
+ return "" , fmt .Errorf ("'%s' is not a supported output format. Supported formats: 'md' (default), 'html'" , output )
140
+ }
141
+ }
142
+
109
143
func exit (err error ) {
110
144
fmt .Fprintf (os .Stderr , "%s" , err .Error ())
111
145
os .Exit (1 )
0 commit comments