-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
381 lines (322 loc) · 9.86 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/erikgeiser/promptkit/confirmation"
tsize "github.com/kopoli/go-terminal-size"
)
var (
versionFlag bool
version string
WinSize tsize.Size
SearchPath string
Debug bool
ValidateOnRefresh bool = true
)
type State int
const (
tableView State = iota
outputView
confirmationView
)
type MainModel struct {
err error
confirmation *confirmation.Model
task func(*MainModel) tea.Cmd
help help.Model
message string
keys KeyMap
projects []Project
output OutputModel
spinner spinner.Model
table TableModel
progress progress.Model
percent float64
state State
working bool
refreshing bool
}
type Project struct {
LastModified time.Time
Name string
Path string
LastAction TerraformCommand
Output string
Valid string
PlanChanges TerraformChanges
}
type (
UpdateValidateMsg Project
UpdatePlanMsg Project
UpdateApplyMsg Project
UpdatesFinishedMsg string
RefreshFinishedMsg []Project
ErrMsg struct{ err error }
)
func (e ErrMsg) Error() string {
return e.err.Error()
}
func initialModel() MainModel {
s := spinner.New()
s.Spinner = spinner.MiniDot
table := createProjectsTable()
output := OutputModel{width: WinSize.Width, height: WinSize.Height}
output.createViewport()
main := MainModel{
state: tableView,
table: table,
output: output,
confirmation: createConfirmation(),
keys: mainKeys,
help: help.New(),
spinner: s,
progress: progress.New(
progress.WithGradient("#737c73", "#8992a7"),
progress.WithWidth(WinSize.Width),
),
working: false,
refreshing: false,
}
return main
}
func (m MainModel) Init() tea.Cmd {
return tea.Batch(tea.SetWindowTitle("tarragon"), m.spinner.Tick, refreshProjects)
}
func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
project, _ := m.table.model.HighlightedRow().Data[columnProject].(Project)
highlightedProject := matchProjectInMemory(project.Path, &m.projects)
switch msg := msg.(type) {
case ErrMsg:
m.err = msg
fmt.Printf("Error: %v\n", msg)
cmds = append(cmds, tea.Quit)
case tea.KeyMsg:
if key.Matches(msg, m.keys.ToggleOutput) {
if m.state == outputView {
m.state = tableView
} else {
m.output.setTitle(project.Name, project.LastAction)
m.output.viewport.SetContent(project.Output + strings.Repeat("\n", 4))
m.state = outputView
}
}
}
switch m.state {
case tableView:
m.table.updateFooter()
m.table.model, cmd = m.table.model.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
if m.working {
m.spinner, cmd = m.spinner.Update(msg)
}
cmds = append(cmds, cmd)
case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
cmds = append(cmds, cmd)
case RefreshFinishedMsg:
m.projects = msg
m.working = false
m.table.updateData(&m.projects)
if ValidateOnRefresh {
m.refreshing = true
m.working = true
m.message = "Terraform Validate: all projects"
var batchArgs []tea.Cmd
batchArgs = append(batchArgs, m.spinner.Tick)
for i := range len(m.projects) {
batchArgs = append(batchArgs, runValidate(&m.projects[i]))
}
cmds = append(cmds, tea.Sequence(tea.Batch(batchArgs...), updatesFinished))
}
case UpdateValidateMsg:
m.message = fmt.Sprintf("Validated %s", msg.Name)
m.table.updateData(&m.projects)
if m.refreshing {
m.percent += float64(1) / float64(m.table.model.TotalRows())
} else {
m.percent += float64(1) / float64(len(m.table.model.SelectedRows()))
}
case UpdatePlanMsg:
m.message = fmt.Sprintf("Updated %s", msg.Name)
m.table.updateData(&m.projects)
m.percent += float64(1) / float64(len(m.table.model.SelectedRows()))
case UpdateApplyMsg:
m.message = fmt.Sprintf("Applied %s", msg.Name)
m.table.updateData(&m.projects)
m.percent += float64(1) / float64(len(m.table.model.SelectedRows()))
case UpdatesFinishedMsg:
m.working = false
m.refreshing = false
m.message = string(msg)
m.percent = 0.0
case tea.KeyMsg:
if !m.table.model.GetIsFilterInputFocused() {
switch {
case key.Matches(msg, m.keys.Help):
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, m.keys.Quit):
cmds = append(cmds, tea.Quit)
case key.Matches(msg, m.keys.Refresh):
m.working = true
cmds = append(cmds, m.spinner.Tick, refreshProjects)
case key.Matches(msg, m.keys.ValidateHighlighted):
m.working = true
m.message = fmt.Sprintf("Terraform Validate: %s", project.Name)
cmds = append(cmds, m.spinner.Tick, tea.Sequence(runValidate(highlightedProject), updatesFinished))
case key.Matches(msg, m.keys.ValidateSelected):
m.working = true
m.message = "Terraform Validate: selected projects"
var batchArgs []tea.Cmd
batchArgs = append(batchArgs, m.spinner.Tick)
for _, row := range m.table.model.SelectedRows() {
project := matchProjectInMemory(row.Data[columnProject].(Project).Path, &m.projects)
batchArgs = append(batchArgs, runValidate(project))
}
cmds = append(cmds, tea.Sequence(tea.Batch(batchArgs...), updatesFinished))
case key.Matches(msg, m.keys.PlanHighlighted):
m.working = true
m.message = fmt.Sprintf("Terraform Plan: %s", project.Name)
cmds = append(cmds, m.spinner.Tick, tea.Sequence(runPlan(highlightedProject), updatesFinished))
case key.Matches(msg, m.keys.PlanSelected):
m.working = true
m.message = "Terraform Plan: selected projects"
var batchArgs []tea.Cmd
batchArgs = append(batchArgs, m.spinner.Tick)
for _, row := range m.table.model.SelectedRows() {
project := matchProjectInMemory(row.Data[columnProject].(Project).Path, &m.projects)
batchArgs = append(batchArgs, runPlan(project))
}
cmds = append(cmds, tea.Sequence(tea.Batch(batchArgs...), updatesFinished))
case key.Matches(msg, m.keys.ApplyHighlighted):
m.task = func(m *MainModel) tea.Cmd {
m.message = fmt.Sprintf("Terraform Apply: %s", project.Name)
return tea.Sequence(runApply(highlightedProject), updatesFinished)
}
m.state = confirmationView
case key.Matches(msg, m.keys.ApplySelected):
m.task = func(m *MainModel) tea.Cmd {
m.message = "Terraform Apply: selected projects"
var batchArgs []tea.Cmd
for _, row := range m.table.model.SelectedRows() {
project := matchProjectInMemory(row.Data[columnProject].(Project).Path, &m.projects)
batchArgs = append(batchArgs, runApply(project))
}
return tea.Sequence(tea.Batch(batchArgs...), updatesFinished)
}
m.state = confirmationView
case key.Matches(msg, m.keys.SelectAll):
rows := m.table.model.GetVisibleRows()
for i, row := range rows {
rows[i] = row.Selected(true)
}
case key.Matches(msg, m.keys.DeselectAll):
m.table.model.WithAllRowsDeselected()
}
}
}
case confirmationView:
msg, _ := msg.(tea.KeyMsg)
switch {
case key.Matches(msg, m.keys.Cancel):
m.state = tableView
m.working = false
case key.Matches(msg, m.keys.No):
m.state = tableView
m.working = false
case key.Matches(msg, m.keys.Yes):
cmds = append(cmds, m.spinner.Tick, m.task(&m))
m.state = tableView
m.working = true
default:
_, cmd := m.confirmation.Update(msg)
cmds = append(cmds, cmd)
}
case outputView:
m.output.viewport, cmd = m.output.viewport.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m MainModel) renderProgress() string {
working := ""
progress := ""
if m.working {
working = fmt.Sprintf(" %s %s...", m.spinner.View(), m.message)
if len(m.table.model.SelectedRows()) > 1 || m.refreshing {
progress = m.progress.ViewAs(m.percent)
}
}
return working + "\n" + progress
}
func (m MainModel) View() string {
var output string
switch m.state {
case tableView:
table := m.table.renderTable()
progress := m.renderProgress()
helpView := m.help.View(m.keys)
contentHeight := lipgloss.Height(table) + lipgloss.Height(progress)
paddingHeight := WinSize.Height - contentHeight - lipgloss.Height(helpView)
output = table + progress + strings.Repeat("\n", max(paddingHeight, 0)) + helpView
case confirmationView:
table := m.table.renderTable()
progress := m.renderProgress()
confirm := m.confirmation.View()
contentHeight := lipgloss.Height(table) + lipgloss.Height(progress)
paddingHeight := WinSize.Height - contentHeight - lipgloss.Height(confirm)
output = table + progress + strings.Repeat("\n", max(paddingHeight, 0)) + confirm
case outputView:
output = m.output.renderOutput()
}
return output
}
func main() {
WinSize, _ = tsize.GetSize()
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Uh oh, there was an error: %v\n", err)
os.Exit(1)
}
flag.BoolVar(&versionFlag, "version", false, "Show version number")
flag.BoolVar(&Debug, "debug", false, "Enable logging to file (debug.log)")
flag.StringVar(&SearchPath, "path", cwd, "Path to search for Terraform projects")
flag.Parse()
if versionFlag {
fmt.Println(version)
os.Exit(0)
}
if Debug {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
}
p := tea.NewProgram(
initialModel(),
tea.WithAltScreen(),
)
_, runErr := p.Run()
if runErr != nil {
fmt.Printf("Uh oh, there was an error: %v\n", err)
os.Exit(1)
}
}