forked from unprofession-al/solaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
225 lines (195 loc) · 5.29 KB
/
cli.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/russross/blackfriday"
"github.com/spf13/cobra"
)
type App struct {
cfg struct {
// root
rootBase string
rootIgnorePatterns []string
rootDebug bool
// graph
graphDetailed bool
// json
jsonCompact bool
// plan
planRoots []string
planJSON bool
planRenderManual bool
planTemplate string
}
// entry point
Execute func() error
}
func NewApp() *App {
a := &App{}
// root
rootCmd := &cobra.Command{
Use: "solaris",
Short: "handle dependencies between multiple terraform workspaces",
}
rootCmd.PersistentFlags().BoolVar(&a.cfg.rootDebug, "debug", false, "write debug output to STDERR")
rootCmd.PersistentFlags().StringVarP(&a.cfg.rootBase, "base", "b", ".", "the base directory")
rootCmd.PersistentFlags().StringSliceVarP(&a.cfg.rootIgnorePatterns, "ignore", "i", []string{`\.terraform`, "modules"}, "ignore subdirectories that match the given patterns")
a.Execute = rootCmd.Execute
// graph
graphCmd := &cobra.Command{
Use: "graph",
Short: "generate dot output of terraform workspace dependencies",
Run: a.graphCmd,
}
graphCmd.PersistentFlags().BoolVar(&a.cfg.graphDetailed, "d", false, "draw a detailed graph")
rootCmd.AddCommand(graphCmd)
// lint
lintCmd := &cobra.Command{
Use: "lint",
Short: "lint terraform workspace dependencies",
Run: a.lintCmd,
}
rootCmd.AddCommand(lintCmd)
// json
jsonCmd := &cobra.Command{
Use: "json",
Short: "print a json representation of terraform workspace dependencies",
Run: a.jsonCmd,
}
jsonCmd.PersistentFlags().BoolVar(&a.cfg.jsonCompact, "c", false, "print compact JSON")
rootCmd.AddCommand(jsonCmd)
// plan
planCmd := &cobra.Command{
Use: "plan",
Short: "print execution order of terraform workspaces",
Run: a.planCmd,
}
planCmd.PersistentFlags().StringSliceVar(&a.cfg.planRoots, "r", []string{}, "plan only to execute these workspaces and workspaces depending on them")
planCmd.PersistentFlags().BoolVar(&a.cfg.planJSON, "j", false, "print as JSON")
planCmd.PersistentFlags().BoolVar(&a.cfg.planRenderManual, "m", false, "Render Pre-/Post manuals (this requires `terraform` to be installed)")
planCmd.PersistentFlags().StringVar(&a.cfg.planTemplate, "t", "", "Path to template")
rootCmd.AddCommand(planCmd)
// version
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version info",
Run: a.versionCmd,
}
rootCmd.AddCommand(versionCmd)
return a
}
func (a *App) debug(out string) {
if a.cfg.rootDebug {
fmt.Fprint(os.Stderr, out)
}
}
func (a *App) graphCmd(cmd *cobra.Command, args []string) {
workspaces, err := GetWorkspaces(a.cfg.rootBase, a.cfg.rootIgnorePatterns)
if err != nil {
log.Fatal(err)
}
if a.cfg.graphDetailed {
graph := RenderWorkspacesDetailed(workspaces)
fmt.Println(graph.String())
} else {
graph := RenderWorkspaces(workspaces)
fmt.Println(graph.String())
}
fmt.Printf("\n/*\n Use 'solaris ... | fdp -Tsvg > out.svg' or\n similar to generate a vector visualization\n*/\n")
}
func (a *App) lintCmd(cmd *cobra.Command, args []string) {
workspaces, err := GetWorkspaces(a.cfg.rootBase, a.cfg.rootIgnorePatterns)
if err != nil {
log.Fatal(err)
}
errs := Lint(workspaces)
for k, v := range errs {
fmt.Printf("%s:\n", k)
for _, e := range v {
fmt.Printf(" %s\n", e)
}
}
}
func (a *App) jsonCmd(cmd *cobra.Command, args []string) {
workspaces, err := GetWorkspaces(a.cfg.rootBase, a.cfg.rootIgnorePatterns)
if err != nil {
log.Fatal(err)
}
var out []byte
if a.cfg.jsonCompact {
out, err = json.Marshal(workspaces)
} else {
out, err = json.MarshalIndent(workspaces, "", " ")
}
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
}
func (a *App) planCmd(cmd *cobra.Command, args []string) {
workspaces, err := GetWorkspaces(a.cfg.rootBase, a.cfg.rootIgnorePatterns)
if err != nil {
log.Fatal(err)
}
wsarray := []*Workspace{}
for _, ws := range workspaces {
wsarray = append(wsarray, ws)
}
plan, err := BuildExecutionPlan(wsarray, a.cfg.planRoots, a.debug)
if err != nil {
log.Fatal(err)
}
for tier, workspaces := range plan {
for i, ws := range workspaces {
if ws.PreManual != "" {
manual := ""
if a.cfg.planRenderManual {
manual, err = ws.PreManual.render(ws.Inputs)
if err != nil {
log.Fatal(err)
}
} else {
manual = string(ws.PreManual)
}
x := blackfriday.Run([]byte(manual))
plan[tier][i].PreManualRendered = string(x)
}
if ws.PostManual != "" {
manual := ""
if a.cfg.planRenderManual {
manual, err = ws.PostManual.render(ws.Inputs)
if err != nil {
log.Fatal(err)
}
} else {
manual = string(ws.PostManual)
}
x := blackfriday.Run([]byte(manual))
plan[tier][i].PostManualRendered = string(x)
}
}
}
if a.cfg.planJSON {
out, err := json.MarshalIndent(plan, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
} else {
if a.cfg.planTemplate == "" {
log.Fatal("template was not specified")
}
content, err := ioutil.ReadFile(a.cfg.planTemplate)
if err != nil {
log.Fatal(err)
}
out := RenderExecutionPlan(plan, string(content))
fmt.Println(out)
}
}
func (a *App) versionCmd(cmd *cobra.Command, args []string) {
fmt.Println(versionInfo())
}