forked from chrislewisdev/prettyplan-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (96 loc) · 2.56 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
//go:generate packr2
package main
import (
"flag"
"fmt"
"html/template"
"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")
flag.Parse()
assets := getAssets()
var plan *planInfo
var err error
if plan, err = getPlan(); 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 getPlan() (*planInfo, 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())
}
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()
}