Skip to content

Commit

Permalink
refactor: Improve error handling and code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent 76b485e commit 0ee627b
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 146 deletions.
30 changes: 18 additions & 12 deletions cmd/cli/commands/config/config_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,35 @@ func validateAndUpdate(p *NetworkConfigPage, input *tview.InputField) {

p.display.app.QueueUpdateDraw(func() {
if err != nil {
errorModal := tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
p.display.app.SetFocus(p.form)
},
)

p.display.app.SetRoot(errorModal, true)
p.openErrorModal(err)

return
}

p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if index != -1 {
cfg.NetworkName = networkOption
}

cfg.BeaconNodeAddress = text
})
}); err != nil {
p.openErrorModal(err)

return
}

p.display.setPage(p.display.homePage)
})
}()
}

func (p *NetworkConfigPage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
p.display.app.SetFocus(p.form)
},
), true)
}
59 changes: 39 additions & 20 deletions cmd/cli/commands/config/config_output_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,29 +295,31 @@ func validateAndUpdateOutputServer(p *OutputServerConfigPage) {

// Only set credentials if both username and password are provided
if usernameText != "" && passwordText != "" {
credentials := fmt.Sprintf("%s:%s", usernameText, passwordText)
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
credentials := base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:%s", usernameText, passwordText)),
)

if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = serverAddress
cfg.OutputServer.Credentials = base64.StdEncoding.EncodeToString([]byte(credentials))
})
cfg.OutputServer.Credentials = credentials
}); err != nil {
p.openErrorModal(err)

return
}
} else if usernameText == "" && passwordText == "" {
// Both empty - clear credentials
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = serverAddress
cfg.OutputServer.Credentials = ""
})
}); err != nil {
p.openErrorModal(err)

return
}
} else {
// One is empty but not both
errorModal := tui.CreateErrorModal(
p.display.app,
"Both username and password must be provided if using credentials",
func() {
p.display.app.SetRoot(p.display.frame, true)
p.display.app.SetFocus(p.form)
},
)

p.display.app.SetRoot(errorModal, true)
p.openErrorModal(fmt.Errorf("both username and password must be provided if using credentials"))

return
}
Expand Down Expand Up @@ -346,11 +348,18 @@ func validateAndUpdateOutputServer(p *OutputServerConfigPage) {
}

// Update credentials.
credentials := fmt.Sprintf("%s:%s", usernameText, passwordText)
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
credentials := base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:%s", usernameText, passwordText)),
)

if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = serverAddress
cfg.OutputServer.Credentials = base64.StdEncoding.EncodeToString([]byte(credentials))
})
cfg.OutputServer.Credentials = credentials
}); err != nil {
p.openErrorModal(err)

return
}
}

p.display.setPage(p.display.homePage)
Expand All @@ -374,3 +383,13 @@ func getCredentialsFromConfig(cfg *service.ContributoorConfig) (username, passwo

return parts[0], parts[1]
}

func (p *OutputServerConfigPage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
},
), true)
}
19 changes: 17 additions & 2 deletions cmd/cli/commands/install/page_20_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ func (p *NetworkConfigPage) initPage() {

// Add button to our form.
form.AddButton(label, func() {
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.NetworkName = tui.AvailableNetworks[index].Value
})
}); err != nil {
p.openErrorModal(err)

return
}

p.display.setPage(p.display.beaconPage.GetPage())
})

Expand Down Expand Up @@ -193,3 +198,13 @@ func (p *NetworkConfigPage) initPage() {

p.content = borderGrid
}

func (p *NetworkConfigPage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
},
), true)
}
18 changes: 16 additions & 2 deletions cmd/cli/commands/install/page_30_beacon_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ func validateAndUpdate(p *BeaconNodePage) {
}

// Update config if validation passes
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.BeaconNodeAddress = address
})
}); err != nil {
p.openErrorModal(err)

return
}

// Move to next page
p.display.setPage(p.display.outputPage.GetPage())
Expand Down Expand Up @@ -196,3 +200,13 @@ func validateBeaconNode(address string) error {

return nil
}

func (p *BeaconNodePage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
},
), true)
}
35 changes: 29 additions & 6 deletions cmd/cli/commands/install/page_40_output_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,35 @@ func (p *OutputServerPage) initPage() {
existingAddress := p.display.configService.Get().OutputServer.Address
if strings.Contains(existingAddress, "platform.ethpandaops.io") {
existingAddress = ""
p.display.configService.Update(func(cfg *service.ContributoorConfig) {

if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = existingAddress
})
}); err != nil {
p.openErrorModal(err)

return
}
}

input := form.AddInputField("Server Address", existingAddress, 40, nil, func(address string) {
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = address
})
}); err != nil {
p.openErrorModal(err)

return
}
})
input.SetBackgroundColor(tui.ColorFormBackground)
} else {
// Only update config when explicitly selecting a standard server.
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Address = selectedValue
})
}); err != nil {
p.openErrorModal(err)

return
}
}
})

Expand Down Expand Up @@ -221,3 +234,13 @@ func (p *OutputServerPage) initPage() {

p.content = borderGrid
}

func (p *OutputServerPage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
},
), true)
}
33 changes: 27 additions & 6 deletions cmd/cli/commands/install/page_50_output_server_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ func (p *OutputServerCredentialsPage) initPage() {

if p.username != "" && p.password != "" {
// Set credentials only when validated.
credentials := fmt.Sprintf("%s:%s", p.username, p.password)
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Credentials = base64.StdEncoding.EncodeToString([]byte(credentials))
})
credentials := base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:%s", p.username, p.password)),
)

if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
cfg.OutputServer.Credentials = credentials
}); err != nil {
p.openErrorModal(err)

return
}
}

p.display.setPage(p.display.finishedPage.GetPage())
Expand Down Expand Up @@ -183,15 +190,29 @@ func validateAndSaveCredentials(p *OutputServerCredentialsPage) {
}

// Update config with credentials.
p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if err := p.display.configService.Update(func(cfg *service.ContributoorConfig) {
if username != "" && password != "" {
credentials := fmt.Sprintf("%s:%s", username, password)
cfg.OutputServer.Credentials = base64.StdEncoding.EncodeToString([]byte(credentials))
} else {
cfg.OutputServer.Credentials = ""
}
})
}); err != nil {
p.openErrorModal(err)

return
}

// Installation complete.
p.display.app.Stop()
}

func (p *OutputServerCredentialsPage) openErrorModal(err error) {
p.display.app.SetRoot(tui.CreateErrorModal(
p.display.app,
err.Error(),
func() {
p.display.app.SetRoot(p.display.frame, true)
},
), true)
}
Loading

0 comments on commit 0ee627b

Please sign in to comment.