-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinteractive.go
582 lines (504 loc) · 17.2 KB
/
interactive.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package engine
import (
"fmt"
"strings"
"time"
"github.com/Azure/InnovationEngine/internal/az"
"github.com/Azure/InnovationEngine/internal/engine/environments"
"github.com/Azure/InnovationEngine/internal/lib"
"github.com/Azure/InnovationEngine/internal/logging"
"github.com/Azure/InnovationEngine/internal/parsers"
"github.com/Azure/InnovationEngine/internal/patterns"
"github.com/Azure/InnovationEngine/internal/ui"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)
type InteractiveModeCommands struct {
execute key.Binding
quit key.Binding
previous key.Binding
next key.Binding
}
// State for the codeblock in interactive mode. Used to keep track of the
// state of each codeblock.
type CodeBlockState struct {
CodeBlock parsers.CodeBlock
CodeBlockNumber int
Error error
StdErr string
StdOut string
StepName string
StepNumber int
Success bool
}
type interactiveModeComponents struct {
paginator paginator.Model
stepViewport viewport.Model
outputViewport viewport.Model
azureCLIViewport viewport.Model
}
type InteractiveModeModel struct {
azureStatus environments.AzureDeploymentStatus
codeBlockState map[int]CodeBlockState
commands InteractiveModeCommands
currentCodeBlock int
env map[string]string
environment string
executingCommand bool
height int
help help.Model
resourceGroupName string
scenarioTitle string
width int
scenarioCompleted bool
components interactiveModeComponents
ready bool
commandLines []string
}
// Initialize the intractive mode model
func (model InteractiveModeModel) Init() tea.Cmd {
environments.ReportAzureStatus(model.azureStatus, model.environment)
return tea.Batch(clearScreen(), tea.Tick(time.Millisecond*10, func(t time.Time) tea.Msg {
return tea.KeyMsg{Type: tea.KeyCtrlL} // This is to force a repaint
}))
}
// Initializes the viewports for the interactive mode model.
func initializeComponents(model InteractiveModeModel, width, height int) interactiveModeComponents {
// paginator setup
p := paginator.New()
p.TotalPages = len(model.codeBlockState)
p.Type = paginator.Dots
// Dots
p.ActiveDot = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).
Render("•")
p.InactiveDot = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).
Render("•")
p.KeyMap.PrevPage = model.commands.previous
p.KeyMap.NextPage = model.commands.next
stepViewport := viewport.New(width, 4)
outputViewport := viewport.New(width, 2)
azureCLIViewport := viewport.New(width, height)
components := interactiveModeComponents{
paginator: p,
stepViewport: stepViewport,
outputViewport: outputViewport,
azureCLIViewport: azureCLIViewport,
}
components.updateViewportHeight(height)
return components
}
// Handle user input for interactive mode.
func handleUserInput(
model InteractiveModeModel,
message tea.KeyMsg,
) (InteractiveModeModel, []tea.Cmd) {
var commands []tea.Cmd
switch {
case key.Matches(message, model.commands.execute):
if model.executingCommand {
logging.GlobalLogger.Info("Command is already executing, ignoring execute command")
break
}
// Prevent the user from executing a command if the previous command has
// not been executed successfully or executed at all.
previousCodeBlock := model.currentCodeBlock - 1
if previousCodeBlock >= 0 {
previousCodeBlockState := model.codeBlockState[previousCodeBlock]
if !previousCodeBlockState.Success {
logging.GlobalLogger.Info(
"Previous command has not been executed successfully, ignoring execute command",
)
break
}
}
// Prevent the user from executing a command if the current command has
// already been executed successfully.
codeBlockState := model.codeBlockState[model.currentCodeBlock]
if codeBlockState.Success {
logging.GlobalLogger.Info(
"Command has already been executed successfully, ignoring execute command",
)
break
}
codeBlock := codeBlockState.CodeBlock
model.executingCommand = true
// If we're on the last step and the command is an SSH command, we need
// to report the status before executing the command. This is needed for
// one click deployments and does not affect the normal execution flow.
if model.currentCodeBlock == len(model.codeBlockState)-1 &&
patterns.SshCommand.MatchString(codeBlock.Content) {
model.azureStatus.Status = "Succeeded"
environments.AttachResourceURIsToAzureStatus(
&model.azureStatus,
model.resourceGroupName,
model.environment,
)
commands = append(commands, tea.Sequence(
updateAzureStatus(model),
func() tea.Msg {
return ExecuteCodeBlockSync(codeBlock, lib.CopyMap(model.env))
}))
} else {
commands = append(commands, ExecuteCodeBlockAsync(
codeBlock,
lib.CopyMap(model.env),
))
}
case key.Matches(message, model.commands.previous):
if model.executingCommand {
logging.GlobalLogger.Info("Command is already executing, ignoring execute command")
break
}
if model.currentCodeBlock > 0 {
model.currentCodeBlock--
}
case key.Matches(message, model.commands.next):
if model.executingCommand {
logging.GlobalLogger.Info("Command is already executing, ignoring execute command")
break
}
if model.currentCodeBlock < len(model.codeBlockState)-1 {
model.currentCodeBlock++
}
case key.Matches(message, model.commands.quit):
commands = append(commands, tea.Quit)
}
return model, commands
}
func (components *interactiveModeComponents) updateViewportHeight(terminalHeight int) {
stepViewportPercent := 0.4
outputViewportPercent := 0.2
stepViewportHeight := int(float64(terminalHeight) * stepViewportPercent)
outputViewportHeight := int(float64(terminalHeight) * outputViewportPercent)
if stepViewportHeight < 4 {
stepViewportHeight = 4
}
if outputViewportHeight < 2 {
outputViewportHeight = 2
}
components.stepViewport.Height = stepViewportHeight
components.outputViewport.Height = outputViewportHeight
components.azureCLIViewport.Height = terminalHeight - 1
}
// Updates the intractive mode model
func (model InteractiveModeModel) Update(message tea.Msg) (tea.Model, tea.Cmd) {
var commands []tea.Cmd
switch message := message.(type) {
case tea.WindowSizeMsg:
model.width = message.Width
model.height = message.Height
logging.GlobalLogger.Debugf("Window size changed to: %d x %d", message.Width, message.Height)
if !model.ready {
model.components = initializeComponents(model, message.Width, message.Height)
model.ready = true
} else {
model.components.stepViewport.Width = message.Width
model.components.outputViewport.Width = message.Width
model.components.azureCLIViewport.Width = message.Width
model.components.updateViewportHeight(message.Height)
}
case tea.KeyMsg:
model, commands = handleUserInput(model, message)
case SuccessfulCommandMessage:
// Handle successful command executions
model.executingCommand = false
step := model.currentCodeBlock
// Update the state of the codeblock which finished executing.
codeBlockState := model.codeBlockState[step]
codeBlockState.StdOut = message.StdOut
codeBlockState.StdErr = message.StdErr
codeBlockState.Success = true
model.codeBlockState[step] = codeBlockState
logging.GlobalLogger.Infof("Finished executing:\n %s", codeBlockState.CodeBlock.Content)
// Extract the resource group name from the command output if
// it's not already set.
if model.resourceGroupName == "" && patterns.AzCommand.MatchString(codeBlockState.CodeBlock.Content) {
logging.GlobalLogger.Debugf("Attempting to extract resource group name from command output")
tmpResourceGroup := az.FindResourceGroupName(codeBlockState.StdOut)
if tmpResourceGroup != "" {
logging.GlobalLogger.Infof("Found resource group named: %s", tmpResourceGroup)
model.resourceGroupName = tmpResourceGroup
}
}
model.commandLines = append(model.commandLines, codeBlockState.StdOut)
// Increment the codeblock and update the viewport content.
model.currentCodeBlock++
if model.currentCodeBlock < len(model.codeBlockState) {
nextCommand := model.codeBlockState[model.currentCodeBlock].CodeBlock.Content
nextLanguage := model.codeBlockState[model.currentCodeBlock].CodeBlock.Language
model.commandLines = append(model.commandLines, ui.CommandPrompt(nextLanguage)+nextCommand)
}
// Only increment the step for azure if the step name has changed.
nextCodeBlockState := model.codeBlockState[model.currentCodeBlock]
if codeBlockState.StepName != nextCodeBlockState.StepName {
logging.GlobalLogger.Debugf("Step name has changed, incrementing step for Azure")
model.azureStatus.CurrentStep++
} else {
logging.GlobalLogger.Debugf("Step name has not changed, not incrementing step for Azure")
}
// If the scenario has been completed, we need to update the azure
// status and quit the program.
if model.currentCodeBlock == len(model.codeBlockState) {
model.scenarioCompleted = true
model.azureStatus.Status = "Succeeded"
environments.AttachResourceURIsToAzureStatus(
&model.azureStatus,
model.resourceGroupName,
model.environment,
)
commands = append(
commands,
tea.Sequence(
updateAzureStatus(model),
tea.Quit,
),
)
} else {
commands = append(commands, updateAzureStatus(model))
}
case FailedCommandMessage:
// Handle failed command executions
// Update the state of the codeblock which finished executing.
step := model.currentCodeBlock
codeBlockState := model.codeBlockState[step]
codeBlockState.StdOut = message.StdOut
codeBlockState.StdErr = message.StdErr
codeBlockState.Success = false
model.codeBlockState[step] = codeBlockState
model.commandLines = append(model.commandLines, codeBlockState.StdErr)
// Report the error
model.executingCommand = false
model.azureStatus.SetError(message.Error)
environments.AttachResourceURIsToAzureStatus(
&model.azureStatus,
model.resourceGroupName,
model.environment,
)
commands = append(commands, tea.Sequence(updateAzureStatus(model), tea.Quit))
case AzureStatusUpdatedMessage:
// After the status has been updated, we force a window resize to
// render over the status update. For some reason, clearing the screen
// manually seems to cause the text produced by View() to not render
// properly if we don't trigger a window size event.
commands = append(commands,
tea.Sequence(
tea.ClearScreen,
func() tea.Msg {
return tea.WindowSizeMsg{
Width: model.width,
Height: model.height,
}
},
),
)
}
// Update viewport content
block := model.codeBlockState[model.currentCodeBlock]
renderedStepSection := fmt.Sprintf(
"%s\n\n%s",
block.CodeBlock.Description,
block.CodeBlock.Content,
)
// TODO(vmarcella): We shoulkd figure out a way to not have to recreate
// the renderer every time we update the view.
renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(model.width-4),
)
if err == nil {
var glamourizedSection string
glamourizedSection, err = renderer.Render(
fmt.Sprintf(
"%s\n```%s\n%s```",
block.CodeBlock.Description,
block.CodeBlock.Language,
block.CodeBlock.Content,
),
)
if err != nil {
logging.GlobalLogger.Errorf(
"Error rendering codeblock: %s, using non rendered codeblock instead",
err,
)
} else {
renderedStepSection = glamourizedSection
}
} else {
logging.GlobalLogger.Errorf(
"Error creating glamour renderer: %s, using non rendered codeblock instead",
err,
)
}
model.components.stepViewport.SetContent(
renderedStepSection,
)
if block.Success {
model.components.outputViewport.SetContent(block.StdOut)
} else {
model.components.outputViewport.SetContent(block.StdErr)
}
model.components.azureCLIViewport.SetContent(strings.Join(model.commandLines, "\n"))
// Update all the viewports and append resulting commands.
var command tea.Cmd
model.components.paginator.Page = model.currentCodeBlock
model.components.stepViewport, command = model.components.stepViewport.Update(message)
commands = append(commands, command)
model.components.outputViewport, command = model.components.outputViewport.Update(message)
commands = append(commands, command)
model.components.azureCLIViewport, command = model.components.azureCLIViewport.Update(message)
commands = append(commands, command)
return model, tea.Batch(commands...)
}
// Shows the commands that the user can use to interact with the interactive
// mode model.
func (model InteractiveModeModel) helpView() string {
if model.environment == "azure" {
return ""
}
keyBindingGroups := [][]key.Binding{
// Command related bindings
{
model.commands.execute,
model.commands.previous,
model.commands.next,
},
// Scenario related bindings
{
model.commands.quit,
},
}
return model.help.FullHelpView(keyBindingGroups)
}
// Renders the interactive mode model.
func (model InteractiveModeModel) View() string {
// When running in the portal, we only want to show the Azure CLI viewport
// which mimics a command line interface during execution.
if model.environment == "azure" {
return model.components.azureCLIViewport.View()
}
scenarioTitle := ui.ScenarioTitleStyle.Width(model.width).
Align(lipgloss.Center).
Render(model.scenarioTitle)
border := lipgloss.NewStyle().
Width(model.components.stepViewport.Width - 2).
Border(lipgloss.NormalBorder())
stepTitle := ui.StepTitleStyle.Render(
fmt.Sprintf(
"Step %d - %s",
model.currentCodeBlock+1,
model.codeBlockState[model.currentCodeBlock].StepName,
),
)
stepView := border.Render(model.components.stepViewport.View())
stepSection := fmt.Sprintf("%s\n%s\n\n", stepTitle, stepView)
outputTitle := ui.StepTitleStyle.Render("Output")
outputView := border.Render(model.components.outputViewport.View())
outputSection := fmt.Sprintf("%s\n%s\n\n", outputTitle, outputView)
paginator := lipgloss.NewStyle().
Width(model.width).
Align(lipgloss.Center).
Render(model.components.paginator.View())
var executing string
if model.executingCommand {
executing = "Executing command..."
} else {
executing = ""
}
// TODO(vmarcella): Format this to be more readable.
return ((scenarioTitle + "\n") +
(paginator + "\n\n") +
(stepSection) +
(outputSection) +
(model.helpView())) +
("\n" + executing)
}
// TODO: Ideally we won't need a global program variable. We should
// refactor this in the future such that each tea program is localized to the
// function that creates it and ExecuteCodeBlockSync doesn't mutate the global
// program variable.
var program *tea.Program = nil
// Create a new interactive mode model.
func NewInteractiveModeModel(
title string,
engine *Engine,
steps []Step,
env map[string]string,
) (InteractiveModeModel, error) {
// TODO: In the future we should just set the current step for the azure status
// to one as the default.
azureStatus := environments.NewAzureDeploymentStatus()
azureStatus.CurrentStep = 1
totalCodeBlocks := 0
codeBlockState := make(map[int]CodeBlockState)
err := az.SetSubscription(engine.Configuration.Subscription)
if err != nil {
logging.GlobalLogger.Errorf("Invalid Config: Failed to set subscription: %s", err)
azureStatus.SetError(err)
environments.ReportAzureStatus(azureStatus, engine.Configuration.Environment)
return InteractiveModeModel{}, err
}
for stepNumber, step := range steps {
azureCodeBlocks := []environments.AzureCodeBlock{}
for blockNumber, block := range step.CodeBlocks {
azureCodeBlocks = append(azureCodeBlocks, environments.AzureCodeBlock{
Command: block.Content,
Description: block.Description,
})
codeBlockState[totalCodeBlocks] = CodeBlockState{
StepName: step.Name,
CodeBlock: block,
StepNumber: stepNumber,
CodeBlockNumber: blockNumber,
StdOut: "",
StdErr: "",
Error: nil,
Success: false,
}
totalCodeBlocks += 1
}
azureStatus.AddStep(fmt.Sprintf("%d. %s", stepNumber+1, step.Name), azureCodeBlocks)
}
language := codeBlockState[0].CodeBlock.Language
commandLines := []string{
ui.CommandPrompt(language) + codeBlockState[0].CodeBlock.Content,
}
return InteractiveModeModel{
scenarioTitle: title,
commands: InteractiveModeCommands{
execute: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "Execute the current command."),
),
quit: key.NewBinding(
key.WithKeys("q"),
key.WithHelp("q", "Quit the scenario."),
),
previous: key.NewBinding(
key.WithKeys("left"),
key.WithHelp("←", "Go to the previous command."),
),
next: key.NewBinding(
key.WithKeys("right"),
key.WithHelp("→", "Go to the next command."),
),
},
env: env,
resourceGroupName: "",
azureStatus: azureStatus,
codeBlockState: codeBlockState,
executingCommand: false,
currentCodeBlock: 0,
help: help.New(),
environment: engine.Configuration.Environment,
scenarioCompleted: false,
ready: false,
commandLines: commandLines,
}, nil
}