Skip to content

Commit

Permalink
Add taskModels for planning
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS-ovm committed May 2, 2024
1 parent f815dd3 commit 1e6ca7e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/tea_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (m cmdModel) Init() tea.Cmd {
waitForCancellation(m.ctx, m.cancel),
m.tasks["00_oi"].Init(),
m.tasks["01_token"].Init(),
m.tasks["02_config"].Init(),
m.cmd.Init(),
)
}
Expand Down
38 changes: 27 additions & 11 deletions cmd/terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ type tfPlanModel struct {
oi OvermindInstance

args []string
planTask taskModel
planHeader string
processingTask taskModel
processingHeader string

runTfPlan bool
Expand All @@ -246,19 +248,17 @@ func NewTfPlanModel(args []string) tea.Model {
// -out needs to go last to override whatever the user specified on the command line
args = append(args, "-out", "overmind.plan")

planHeader := `# Planning Changes
Running ` + "`" + `terraform %v` + "`\n"
planHeader := `Running ` + "`" + `terraform %v` + "`\n"
planHeader = fmt.Sprintf(planHeader, strings.Join(args, " "))

processingHeader := `# Processing Planned Changes
Processing plan from ` + "`" + `terraform %v` + "`\n"
processingHeader := `Processing plan from ` + "`" + `terraform %v` + "`\n"
processingHeader = fmt.Sprintf(processingHeader, strings.Join(args, " "))

return tfPlanModel{
args: args,
planTask: NewTaskModel("Planning Changes"),
planHeader: planHeader,
processingTask: NewTaskModel("Processing Planned Changes"),
processingHeader: processingHeader,

processing: make(chan tea.Msg, 10), // provide a small buffer for sending updates, so we don't block the processing
Expand All @@ -268,7 +268,10 @@ Processing plan from ` + "`" + `terraform %v` + "`\n"
}

func (m tfPlanModel) Init() tea.Cmd {
return nil
return tea.Batch(
m.planTask.Init(),
m.processingTask.Init(),
)
}

func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -281,6 +284,7 @@ func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case sourcesInitialisedMsg:
m.runTfPlan = true
m.planTask.status = taskStatusRunning
// defer the actual command to give the view a chance to show the header
return m, func() tea.Msg { return triggerTfPlanMsg{} }
case triggerTfPlanMsg:
Expand All @@ -303,7 +307,9 @@ func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
})
case tfPlanFinishedMsg:
m.tfPlanFinished = true
m.planTask.status = taskStatusDone

m.processingTask.status = taskStatusRunning
m.processingModel.state = "executed terraform plan"

return m, tea.Batch(
Expand All @@ -316,6 +322,7 @@ func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.waitForProcessingActivity
case processingFinishedActivityMsg:
m.processingModel.state = "finished"
m.processingTask.status = taskStatusDone
m.progress = append(m.progress, msg.text)
return m, m.waitForProcessingActivity
case changeUpdatedMsg:
Expand All @@ -341,23 +348,32 @@ func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case fatalError:
m.fatalError = msg.err.Error()
return m, tea.Quit
default:
var planCmd, processingCmd tea.Cmd
m.planTask, planCmd = m.planTask.Update(msg)
m.processingTask, processingCmd = m.processingTask.Update(msg)
return m, tea.Batch(planCmd, processingCmd)
}

return m, nil
}

func (m tfPlanModel) View() string {
bits := []string{}
bits := []string{
m.planTask.View(),
}

if m.runTfPlan && !m.tfPlanFinished {
bits = append(bits, markdownToString(m.planHeader))
} else if m.tfPlanFinished {
}

bits = append(bits, m.processingTask.View())

if m.tfPlanFinished {
bits = append(bits, markdownToString(m.processingHeader))
bits = append(bits, m.processingModel.View())
}

// bits = append(bits, m.progress...)

if m.changeUrl != "" {
bits = append(bits, fmt.Sprintf("\nCheck the blast radius graph and risks at:\n%v\n\n", m.changeUrl))
}
Expand Down

0 comments on commit 1e6ca7e

Please sign in to comment.