-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
137 lines (113 loc) · 3.02 KB
/
main.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
//go:generate packr2
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/exec"
"time"
"github.com/chrislewisdev/prettyplan-cli/parse"
"github.com/chrislewisdev/prettyplan-cli/render"
"github.com/gobuffalo/packr/v2"
"github.com/pkg/browser"
)
func main() {
openFileFlag := flag.Bool("open", false, "To open the HTML report once generated")
fileNameFlag := flag.String("filename", "prettyplan-output.html", "Specify a filename other than prettyplan-output.html")
stdinFlag := flag.Bool("stdin", false, "Whether to read the plan from stdin instead of running terraform")
flag.Parse()
assets := getAssets()
var plan *planInfo
var err error
if plan, err = getPlan(*stdinFlag); err != nil {
fmt.Println(err.Error())
return
}
writeReport(*fileNameFlag, assets, plan)
if *openFileFlag {
browser.OpenFile(*fileNameFlag)
}
}
func panicIfError(err error) {
if err != nil {
panic(err)
}
}
type templateAssets struct {
Styles template.CSS
Scripts template.JS
Template *template.Template
}
func getAssets() *templateAssets {
templates := packr.New("Templates", "./templates")
styles, err := templates.FindString("style.css")
panicIfError(err)
scripts, err := templates.FindString("scripts.js")
panicIfError(err)
html, err := templates.FindString("prettyplan.html")
panicIfError(err)
templateFunctions := template.FuncMap{
"prettify": render.Prettify,
}
reportTemplate, err := template.New("prettyplan.html").Funcs(templateFunctions).Parse(html)
panicIfError(err)
return &templateAssets{
Styles: template.CSS(styles),
Scripts: template.JS(scripts),
Template: reportTemplate,
}
}
type planInfo struct {
Raw string
Parsed parse.Plan
}
func getRawPlan() ([]byte, error) {
rawPlan, err := exec.Command("terraform", "plan", "-no-color").Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("terraform plan failed! Output:\n %v", string(exitError.Stderr))
}
return nil, fmt.Errorf("terraform plan failed! Output:\n %v", err.Error())
}
return rawPlan, nil
}
func getPlan(stdinFlag bool) (*planInfo, error) {
var rawPlan []byte
var err error
if stdinFlag {
fmt.Println("Reading plan from stdin...")
rawPlan, err = ioutil.ReadAll(os.Stdin)
} else {
fmt.Println("Running terraform to get plan...")
rawPlan, err = getRawPlan()
}
if err != nil {
return nil, err
}
plan := parse.Parse(string(rawPlan))
return &planInfo{Raw: string(rawPlan), Parsed: plan}, nil
}
type reportInfo struct {
Version string
Plan parse.Plan
RawPlan string
Styles template.CSS
Scripts template.JS
ReportTime string
}
func writeReport(fileName string, assets *templateAssets, plan *planInfo) {
outputFile, err := os.Create(fileName)
panicIfError(err)
err = assets.Template.Execute(outputFile, reportInfo{
Version: "v1.1",
Plan: plan.Parsed,
RawPlan: plan.Raw,
Styles: assets.Styles,
Scripts: assets.Scripts,
ReportTime: time.Now().Format(time.ANSIC),
})
panicIfError(err)
outputFile.Close()
}