Skip to content

Commit

Permalink
Fixed up callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Jul 22, 2024
1 parent 2e25bc7 commit 28c33cd
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions internal/steps/space_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,26 @@ func (s SpaceExportStep) createNewProject(parent fyne.Window, attemptedLvsDelete
s.createProject.Disable()
s.result.SetText("πŸ”΅ Creating project. This can take a little while.")

message, err := s.Execute(func(title string, message string, callback func(bool)) {
s.Execute(func(title string, message string, callback func(bool)) {
dialog.NewConfirm(title, message, callback, parent).Show()
}, func(title string, err error) {
s.result.SetText(title)
s.logs.SetText(err.Error())
s.infinite.Hide()
}, func(message string) {
s.result.SetText(message)
s.next.Enable()
s.infinite.Hide()
}, false, false)

s.result.SetText(message)

if err != nil {
s.logs.SetText(err.Error())
}
}

func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handleError func(string, error), attemptedLvsDelete bool, attemptedAccountDelete bool) (string, error) {
func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handleError func(string, error), handleSuccess func(string), attemptedLvsDelete bool, attemptedAccountDelete bool) {
myclient, err := octoclient.CreateClient(s.State)

if err != nil {
return "πŸ”΄ Failed to create the client", err
handleError("πŸ”΄ Failed to create the client", err)
return
}

// Best effort at deleting existing project and project group
Expand All @@ -128,15 +129,15 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
if err := s.deleteProject(myclient, project); err != nil {
handleError("πŸ”΄ Failed to delete the resource", err)
} else if s.State.PromptForDelete {
s.Execute(prompt, handleError, attemptedLvsDelete, attemptedAccountDelete)
s.Execute(prompt, handleError, handleSuccess, attemptedLvsDelete, attemptedAccountDelete)
}
}
}

if s.State.PromptForDelete {
prompt("Project Group Exists", "The project "+spaceManagementProject+" already exists. Do you want to delete it? It is usually safe to delete this resource.", deleteProjectFunc)
// We can't go further until the group is deleted
return "", nil
return
} else {
deleteProjectFunc(true)
}
Expand All @@ -150,15 +151,15 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
if err := s.deleteProjectGroup(myclient, pggroup); err != nil {
handleError("πŸ”΄ Failed to delete the resource", err)
} else if s.State.PromptForDelete {
s.Execute(prompt, handleError, attemptedLvsDelete, attemptedAccountDelete)
s.Execute(prompt, handleError, handleSuccess, attemptedLvsDelete, attemptedAccountDelete)
}
}
}

if s.State.PromptForDelete {
prompt("Project Group Exists", "The project group Octoterra already exists. Do you want to delete it? It is usually safe to delete this resource.", deleteProgGroupFunc)
// We can't go further until the group is deleted
return "", nil
return
} else {
deleteProgGroupFunc(true)
}
Expand Down Expand Up @@ -234,15 +235,15 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
// Tolerate the inability to delete a LVS, because it might have been
// captured in a release or runbook snapshot, which becomes very hard
// to unwind.
s.Execute(prompt, handleError, true, attemptedAccountDelete)
s.Execute(prompt, handleError, handleSuccess, true, attemptedAccountDelete)
}
}
}

if s.State.PromptForDelete {
prompt("Library Variable Set Exists", "The library variable set Octoterra already exists. Do you want to unlink it from all the projects and delete it? It is usually safe to delete this resource.", deleteLvsFunc)
// We can't go further until the group is deleted
return "", nil
return
} else {
deleteLvsFunc(true)
}
Expand All @@ -257,15 +258,15 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
if err := s.deleteFeed(myclient, feed); err != nil {
handleError("πŸ”΄ Failed to delete the resource", err)
} else if s.State.PromptForDelete {
s.Execute(prompt, handleError, attemptedLvsDelete, attemptedAccountDelete)
s.Execute(prompt, handleError, handleSuccess, attemptedLvsDelete, attemptedAccountDelete)
}
}
}

if s.State.PromptForDelete {
prompt("Feed Exists", "The feed Octoterra Docker Feed already exists. Do you want to delete it? It is usually safe to delete this resource.", delteFeedFunc)
// We can't go further until the feed is deleted
return "", nil
return
} else {
delteFeedFunc(true)
}
Expand All @@ -281,15 +282,15 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
}

if s.State.PromptForDelete {
s.Execute(prompt, handleError, attemptedLvsDelete, true)
s.Execute(prompt, handleError, handleSuccess, attemptedLvsDelete, true)
}
}
}

if s.State.PromptForDelete {
prompt("Account Exists", "The account Octoterra AWS Account already exists. Do you want to delete it? It is usually safe to delete this resource.", deleteAccountFunc)
// We can't go further until the account is deleted
return "", nil
return
} else {
deleteAccountFunc(true)
}
Expand All @@ -299,32 +300,32 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
serializeSpaceTemplate, err, message := query.GetStepTemplateId(myclient, s.State, "Octopus - Serialize Space to Terraform")

if err != nil {
return message, err
handleError(message, err)
}

deploySpaceTemplateS3, err, message := query.GetStepTemplateId(myclient, s.State, "Octopus - Populate Octoterra Space (S3 Backend)")

if err != nil {
return message, err
handleError(message, err)
}

// Find space name
spaceName, err := query.GetSpaceName(myclient, s.State)

if err != nil {
return message, err
handleError(message, err)
}

// Save and apply the module
dir, err := ioutil.TempDir("", "octoterra")
if err != nil {
return "πŸ”΄ An error occurred while creating a temporary directory", err
handleError("πŸ”΄ An error occurred while creating a temporary directory", err)
}

filePath := filepath.Join(dir, "terraform.tf")

if err := os.WriteFile(filePath, []byte(module), 0644); err != nil {
return "πŸ”΄ An error occurred while writing the Terraform file", err
handleError("πŸ”΄ An error occurred while writing the Terraform file", err)
}

initCmd := exec.Command("terraform", "init", "-no-color")
Expand All @@ -335,7 +336,7 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
initCmd.Stderr = &initStderr

if err := initCmd.Run(); err != nil {
return "πŸ”΄ Terraform init failed.", errors.New(err.Error() + "\n" + initStdout.String() + initCmd.String())
handleError("πŸ”΄ Terraform init failed.", errors.New(err.Error()+"\n"+initStdout.String()+initCmd.String()))
}

applyCmd := exec.Command("terraform",
Expand All @@ -362,9 +363,9 @@ func (s SpaceExportStep) Execute(prompt func(string, string, func(bool)), handle
applyCmd.Stderr = &stderr

if err := applyCmd.Run(); err != nil {
return "πŸ”΄ Terraform apply failed", errors.New(stdout.String() + stderr.String())
handleError("πŸ”΄ Terraform apply failed", errors.New(stdout.String()+stderr.String()))
} else {
return "🟒 Terraform apply succeeded", nil
handleSuccess("🟒 Terraform apply succeeded")
}
}

Expand Down

0 comments on commit 28c33cd

Please sign in to comment.