Skip to content

Commit

Permalink
Added error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Aug 21, 2024
1 parent 8e7ace0 commit 5da3bbe
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 10 deletions.
24 changes: 24 additions & 0 deletions internal/logutil/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package logutil

import (
"fmt"
"os"
)

// WriteTextToFile writes the given text to the specified file.
func WriteTextToFile(filePath, text string) error {
// Open the file for writing, create it if it doesn't exist
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

// Write the text to the file
_, err = file.WriteString(text)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}
10 changes: 10 additions & 0 deletions internal/steps/aws_terraform_state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package steps

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/state"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
"github.com/mcasperson/OctoterraWizard/internal/validators"
Expand Down Expand Up @@ -56,13 +58,21 @@ func (s AwsTerraformStateStep) GetContainer(parent fyne.Window) *fyne.Container

validationFailed := false
if err := validators.ValidateAWS(s.getState()); err != nil {
if err := logutil.WriteTextToFile("aws_terraform_state_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to validate the credentials. Please check the Access Key, Secret Key, S3 Bucket Name, and S3 Bucket Region.")
s.logs.SetText(err.Error())
s.logs.Show()
validationFailed = true
}

if err := validators.TestS3Bucket(s.getState()); err != nil {
if err := logutil.WriteTextToFile("aws_terraform_state_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to connect to the S3 bucket. Please check that the bucket exists and that the supplied credentials can access it.")
s.logs.SetText(err.Error())
s.logs.Show()
Expand Down
10 changes: 10 additions & 0 deletions internal/steps/azure_terraform_state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package steps

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/state"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
"github.com/mcasperson/OctoterraWizard/internal/validators"
Expand Down Expand Up @@ -64,6 +66,10 @@ func (s AzureTerraformStateStep) GetContainer(parent fyne.Window) *fyne.Containe
exists, err := validators.AzureContainerExists(newState.AzureTenantId, newState.AzureApplicationId, newState.AzurePassword, newState.AzureStorageAccountName, newState.AzureContainerName)

if err != nil {
if err := logutil.WriteTextToFile("azure_terraform_state_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to validate the credentials. Please check the credentials and storage account details.")
s.logs.SetText(err.Error())
s.logs.Show()
Expand All @@ -76,6 +82,10 @@ func (s AzureTerraformStateStep) GetContainer(parent fyne.Window) *fyne.Containe
rgExists, err := validators.AzureResourceGroupExists(newState.AzureTenantId, newState.AzureApplicationId, newState.AzureSubscriptionId, newState.AzurePassword, newState.AzureResourceGroupName)

if err != nil {
if err := logutil.WriteTextToFile("azure_terraform_state_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to validate the credentials. Please check the credentials and storage account details.")
s.logs.SetText(err.Error())
s.logs.Show()
Expand Down
8 changes: 7 additions & 1 deletion internal/steps/octopus_destination_details.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package steps

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/state"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
"github.com/mcasperson/OctoterraWizard/internal/validators"
Expand Down Expand Up @@ -45,7 +47,11 @@ func (s OctopusDestinationDetails) GetContainer(parent fyne.Window) *fyne.Contai
defer s.previous.Enable()

validationFailed := false
if !validators.ValidateDestinationCreds(s.getState()) {
if err := validators.ValidateDestinationCreds(s.getState()); err != nil {
if err := logutil.WriteTextToFile("octopus_destination_details_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to connect to the Octopus server. Please check the URL, API key, and Space ID.")
validationFailed = true
}
Expand Down
8 changes: 7 additions & 1 deletion internal/steps/octopus_details.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package steps

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/state"
"github.com/mcasperson/OctoterraWizard/internal/validators"
"github.com/mcasperson/OctoterraWizard/internal/wizard"
Expand Down Expand Up @@ -44,7 +46,11 @@ func (s OctopusDetails) GetContainer(parent fyne.Window) *fyne.Container {
defer s.previous.Enable()

validationFailed := false
if !validators.ValidateSourceCreds(s.getState()) {
if err := validators.ValidateSourceCreds(s.getState()); err != nil {
if err := logutil.WriteTextToFile("octopus_details_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText("🔴 Unable to connect to the Octopus server. Please check the URL, API key, and Space ID.")
validationFailed = true
}
Expand Down
5 changes: 5 additions & 0 deletions internal/steps/project_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbooks"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/octoclient"
"github.com/mcasperson/OctoterraWizard/internal/query"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
Expand Down Expand Up @@ -109,6 +110,10 @@ func (s ProjectExportStep) createNewProject(parent fyne.Window) {
s.Execute(func(title string, message string, callback func(bool)) {
dialog.NewConfirm(title, message, callback, parent).Show()
}, func(message string, err error) {
if err := logutil.WriteTextToFile("project_export_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText(message)
s.logs.SetText(err.Error())
s.logs.Show()
Expand Down
5 changes: 5 additions & 0 deletions internal/steps/space_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projectgroups"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/octoclient"
"github.com/mcasperson/OctoterraWizard/internal/query"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
Expand Down Expand Up @@ -124,6 +125,10 @@ func (s SpaceExportStep) createNewProject(parent fyne.Window) {
s.Execute(func(title string, message string, callback func(bool)) {
dialog.NewConfirm(title, message, callback, parent).Show()
}, func(title string, err error) {
if err := logutil.WriteTextToFile("space_export_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

s.result.SetText(title)
s.logs.Show()
s.logs.SetText(err.Error())
Expand Down
6 changes: 6 additions & 0 deletions internal/steps/spread_variables.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package steps

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/spreadvariables"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
"github.com/mcasperson/OctoterraWizard/internal/wizard"
Expand Down Expand Up @@ -86,6 +88,10 @@ func (s SpreadVariablesStep) GetContainer(parent fyne.Window) *fyne.Container {
defer previous.Enable()
defer infinite.Hide()
if err := s.Execute(); err != nil {
if err := logutil.WriteTextToFile("spread_variables_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

result.SetText("🔴 An error was raised while attempting to spread the variables. Unfortunately, this means the wizard can not continue.\n " + err.Error())
} else {
result.SetText("🟢 Sensitive variables have been spread.")
Expand Down
5 changes: 5 additions & 0 deletions internal/steps/start_project_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fyne.io/fyne/v2/widget"
projects2 "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/mcasperson/OctoterraWizard/internal/infrastructure"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/octoclient"
"github.com/mcasperson/OctoterraWizard/internal/octoerrors"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
Expand Down Expand Up @@ -91,6 +92,10 @@ func (s StartProjectExportStep) GetContainer(parent fyne.Window) *fyne.Container
if err := s.Execute(func(message string) {
result.SetText(message)
}); err != nil {
if err := logutil.WriteTextToFile("start_project_export_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

result.SetText(fmt.Sprintf("🔴 Failed to publish and run the runbooks. The failed tasks are shown below. You can review the task details in the Octopus console to find more information."))
s.logs.SetText(err.Error())
s.logs.Show()
Expand Down
5 changes: 5 additions & 0 deletions internal/steps/start_space_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/mcasperson/OctoterraWizard/internal/infrastructure"
"github.com/mcasperson/OctoterraWizard/internal/logutil"
"github.com/mcasperson/OctoterraWizard/internal/strutil"
"github.com/mcasperson/OctoterraWizard/internal/wizard"
"net/url"
Expand Down Expand Up @@ -85,6 +86,10 @@ func (s StartSpaceExportStep) GetContainer(parent fyne.Window) *fyne.Container {
if err := s.Execute(func(message string) {
result.SetText(message)
}); err != nil {
if err := logutil.WriteTextToFile("start_space_export_error.txt", err.Error()); err != nil {
fmt.Println("Failed to write error to file")
}

result.SetText(fmt.Sprintf("🔴 Failed to publish and run the runbooks"))
s.logs.Show()
s.logs.SetText(err.Error())
Expand Down
16 changes: 8 additions & 8 deletions internal/validators/octopus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ import (
"github.com/mcasperson/OctoterraWizard/internal/state"
)

func ValidateSourceCreds(state state.State) bool {
func ValidateSourceCreds(state state.State) error {
if myclient, err := octoclient.CreateClient(state); err != nil {
return false
return err
} else {
if _, err := spaces.GetByID(myclient, myclient.GetSpaceID()); err != nil {
return false
return err
}
}

return true
return nil
}

func ValidateDestinationCreds(state state.State) bool {
func ValidateDestinationCreds(state state.State) error {
if myclient, err := octoclient.CreateDestinationClient(state); err != nil {
return false
return err
} else {
if _, err := spaces.GetByID(myclient, myclient.GetSpaceID()); err != nil {
return false
return err
}
}

return true
return nil
}

0 comments on commit 5da3bbe

Please sign in to comment.