Skip to content

Commit

Permalink
refactor: Improve error handling for input field types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent 2db7191 commit e4a00a2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cmd/cli/commands/config/config_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (p *NetworkConfigPage) initPage() {
// Add a save button and ensure we validate the input.
saveButton := tview.NewButton(tui.ButtonSaveSettings)
saveButton.SetSelectedFunc(func() {
validateAndUpdate(p, form.GetFormItem(1).(*tview.InputField))
beaconNodeAddress, _ := form.GetFormItem(1).(*tview.InputField)
validateAndUpdate(p, beaconNodeAddress)
})
saveButton.SetBackgroundColorActivated(tui.ColorButtonActivated)
saveButton.SetLabelColorActivated(tui.ColorButtonText)
Expand Down
38 changes: 30 additions & 8 deletions cmd/cli/commands/config/config_output_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/service"
Expand Down Expand Up @@ -256,26 +257,47 @@ func validateAndUpdateOutputServer(p *OutputServerConfigPage) {

if isCustom {
// For custom servers, get address from input field.
address := p.form.GetFormItem(formStart).(*tview.InputField).GetText()
serverAddress = address
formStart++
var address string

// Only validate the address if it's a custom server, otherwise its a
// pre-defined pandaops server.
if err := validate.ValidateOutputServerAddress(serverAddress); err != nil {
if item := p.form.GetFormItem(formStart); item != nil {
if inputField, ok := item.(*tview.InputField); ok {
address = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid address field type"))

return
}
}

if err := validate.ValidateOutputServerAddress(address); err != nil {
p.openErrorModal(err)

return
}

serverAddress = address
formStart++
}

// Get credentials from form.
if formItem := p.form.GetFormItem(formStart); formItem != nil {
username = formItem.(*tview.InputField).GetText()
if inputField, ok := formItem.(*tview.InputField); ok {
username = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid username field type"))

return
}
}

if formItem := p.form.GetFormItem(formStart + 1); formItem != nil {
password = formItem.(*tview.InputField).GetText()
if inputField, ok := formItem.(*tview.InputField); ok {
password = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid password field type"))

return
}
}

// Validate credentials. These are optional for custom servers.
Expand Down
9 changes: 8 additions & 1 deletion cmd/cli/commands/install/page_40_output_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package install

import (
"fmt"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/service"
Expand Down Expand Up @@ -183,7 +184,13 @@ func (p *OutputServerPage) initPage() {

if isCustom {
if input := form.GetFormItemByLabel("Server Address"); input != nil {
address = input.(*tview.InputField).GetText()
if inputField, ok := input.(*tview.InputField); ok {
address = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid input field type"))

return
}
}

if err := validate.ValidateOutputServerAddress(address); err != nil {
Expand Down
24 changes: 22 additions & 2 deletions cmd/cli/commands/install/page_50_output_server_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package install

import (
"encoding/base64"
"fmt"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/service"
Expand Down Expand Up @@ -133,8 +134,27 @@ func (p *OutputServerCredentialsPage) initPage() {
}

func validateAndSaveCredentials(p *OutputServerCredentialsPage) {
username := p.form.GetFormItem(0).(*tview.InputField).GetText()
password := p.form.GetFormItem(1).(*tview.InputField).GetText()
var username, password string

if item := p.form.GetFormItem(0); item != nil {
if inputField, ok := item.(*tview.InputField); ok {
username = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid username field type"))

return
}
}

if item := p.form.GetFormItem(1); item != nil {
if inputField, ok := item.(*tview.InputField); ok {
password = inputField.GetText()
} else {
p.openErrorModal(fmt.Errorf("invalid password field type"))

return
}
}

currentAddress := p.display.configService.Get().OutputServer.Address
isEthPandaOps := validate.IsEthPandaOpsServer(currentAddress)
Expand Down

0 comments on commit e4a00a2

Please sign in to comment.