Skip to content

Commit

Permalink
refactor(config): import proto cfg from contributor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 20, 2024
1 parent 734c169 commit 76efd71
Show file tree
Hide file tree
Showing 29 changed files with 331 additions and 297 deletions.
21 changes: 11 additions & 10 deletions cmd/cli/commands/config/config_contributoor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package config

import (
"runtime"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
)

// Available run modes.
var runModes = []string{
sidecar.RunMethodDocker,
sidecar.RunMethodSystemd,
sidecar.RunMethodBinary,
var runModes = []config.RunMethod{
config.RunMethod_RUN_METHOD_DOCKER,
config.RunMethod_RUN_METHOD_SYSTEMD,
config.RunMethod_RUN_METHOD_BINARY,
}

// ContributoorSettingsPage is a page that allows the user to configure core contributoor settings.
Expand Down Expand Up @@ -105,10 +106,10 @@ func (p *ContributoorSettingsPage) initPage() {
runModeLabels := make([]string, len(runModes))

for i, mode := range runModes {
if mode == sidecar.RunMethodSystemd {
if mode == config.RunMethod_RUN_METHOD_SYSTEMD {
runModeLabels[i] = getServiceManagerLabel()
} else {
runModeLabels[i] = mode
runModeLabels[i] = strings.ToLower(mode.DisplayName())
}
}

Expand All @@ -118,9 +119,9 @@ func (p *ContributoorSettingsPage) initPage() {
})

form.AddDropDown("Run Mode", runModeLabels, currentRunModeIndex, func(option string, index int) {
if option == sidecar.RunMethodDocker {
if option == config.RunMethod_RUN_METHOD_DOCKER.String() {
p.description.SetText("Run using Docker containers (recommended)")
} else if runModes[index] == sidecar.RunMethodSystemd {
} else if runModes[index] == config.RunMethod_RUN_METHOD_SYSTEMD {
p.description.SetText(getServiceManagerDescription())
} else {
p.description.SetText("Run directly as a binary on your system")
Expand Down Expand Up @@ -214,7 +215,7 @@ func validateAndUpdateContributoor(p *ContributoorSettingsPage) {
runModeIndex, _ := runMode.GetCurrentOption()
runModeText := runModes[runModeIndex]

if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.LogLevel = logLevelText
cfg.RunMethod = runModeText
}); err != nil {
Expand Down
22 changes: 14 additions & 8 deletions cmd/cli/commands/config/config_network.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package config

import (
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor-installer/internal/validate"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -64,8 +64,8 @@ func (p *NetworkConfigPage) initPage() {
networkDescriptions := make(map[string]string)

for i, network := range tui.AvailableNetworks {
networks[i] = network.Value
networkDescriptions[network.Value] = network.Description
networks[i] = network.Value.String()
networkDescriptions[network.Value.String()] = network.Description
}

// Add our form fields.
Expand All @@ -74,7 +74,7 @@ func (p *NetworkConfigPage) initPage() {
currentNetworkIndex := 0

for i, network := range networks {
if network == currentNetwork {
if network == currentNetwork.String() {
currentNetworkIndex = i

break
Expand Down Expand Up @@ -169,15 +169,21 @@ func (p *NetworkConfigPage) initPage() {
}

func validateAndUpdateNetwork(p *NetworkConfigPage) {
if err := validate.ValidateBeaconNodeAddress(p.display.sidecarCfg.Get().BeaconNodeAddress); err != nil {
networkDropdown, _ := p.form.GetFormItem(0).(*tview.DropDown)
beaconInput, _ := p.form.GetFormItem(1).(*tview.InputField)

_, networkName := networkDropdown.GetCurrentOption()
beaconAddress := beaconInput.GetText()

if err := validate.ValidateBeaconNodeAddress(beaconAddress); err != nil {
p.openErrorModal(err)

return
}

if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
cfg.NetworkName = p.display.sidecarCfg.Get().NetworkName
cfg.BeaconNodeAddress = p.display.sidecarCfg.Get().BeaconNodeAddress
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.NetworkName = config.NetworkName(config.NetworkName_value[networkName])
cfg.BeaconNodeAddress = beaconAddress
}); err != nil {
p.openErrorModal(err)

Expand Down
6 changes: 3 additions & 3 deletions cmd/cli/commands/config/config_output_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor-installer/internal/validate"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -312,7 +312,7 @@ func validateAndUpdateOutputServer(p *OutputServerConfigPage) {
}

// Update config with validated values.
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Address = serverAddress
if username != "" && password != "" {
cfg.OutputServer.Credentials = validate.EncodeCredentials(username, password)
Expand Down Expand Up @@ -340,7 +340,7 @@ func (p *OutputServerConfigPage) openErrorModal(err error) {
}

// Update getCredentialsFromConfig to use the validation package.
func getCredentialsFromConfig(cfg *sidecar.Config) (username, password string) {
func getCredentialsFromConfig(cfg *config.Config) (username, password string) {
username, password, err := validate.DecodeCredentials(cfg.OutputServer.Credentials)
if err != nil {
return "", ""
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/commands/install/page_20_network.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package install

import (
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -91,7 +91,7 @@ func (p *NetworkConfigPage) initPage() {

// Add button to our form.
form.AddButton(label, func() {
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.NetworkName = tui.AvailableNetworks[index].Value
}); err != nil {
p.openErrorModal(err)
Expand Down
10 changes: 5 additions & 5 deletions cmd/cli/commands/install/page_20_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package install
import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/sidecar/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +15,7 @@ import (
// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestNetworkConfigPage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *sidecar.Config) *InstallDisplay {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *config.Config) *InstallDisplay {
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(cfg).AnyTimes()
mockConfig.EXPECT().Update(gomock.Any()).Return(nil).AnyTimes()
Expand All @@ -32,7 +32,7 @@ func TestNetworkConfigPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)
Expand All @@ -52,7 +52,7 @@ func TestNetworkConfigPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)
Expand All @@ -70,7 +70,7 @@ func TestNetworkConfigPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/commands/install/page_30_beacon_node.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package install

import (
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor-installer/internal/validate"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -133,7 +133,7 @@ func validateAndUpdate(p *BeaconNodePage, input *tview.InputField) {
return
}

if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.BeaconNodeAddress = input.GetText()
}); err != nil {
p.openErrorModal(err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/cli/commands/install/page_30_beacon_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package install
import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/sidecar/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +15,7 @@ import (
// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestBeaconNodePage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *sidecar.Config) *InstallDisplay {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *config.Config) *InstallDisplay {
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(cfg).AnyTimes()

Expand All @@ -34,7 +34,7 @@ func TestBeaconNodePage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{
mockDisplay := setupMockDisplay(ctrl, &config.Config{
BeaconNodeAddress: "http://localhost:5052",
})

Expand All @@ -61,7 +61,7 @@ func TestBeaconNodePage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{
mockDisplay := setupMockDisplay(ctrl, &config.Config{
BeaconNodeAddress: "http://localhost:5052",
})

Expand Down
16 changes: 8 additions & 8 deletions cmd/cli/commands/install/page_40_output_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strings"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor-installer/internal/validate"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -69,8 +69,8 @@ func (p *OutputServerPage) initPage() {

// Get current selection from config
if p.display.sidecarCfg.Get().OutputServer == nil {
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
cfg.OutputServer = &sidecar.OutputServerConfig{}
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer = &config.OutputServer{}
}); err != nil {
p.openErrorModal(err)

Expand Down Expand Up @@ -120,7 +120,7 @@ func (p *OutputServerPage) initPage() {

if wasEthPandaOps != isEthPandaOps {
// Server type changed, clear credentials
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Credentials = ""
}); err != nil {
p.openErrorModal(err)
Expand All @@ -137,7 +137,7 @@ func (p *OutputServerPage) initPage() {
existingAddress = ""
}

if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Address = existingAddress
}); err != nil {
p.openErrorModal(err)
Expand All @@ -146,7 +146,7 @@ func (p *OutputServerPage) initPage() {
}

input := form.AddInputField("Server Address", existingAddress, 40, nil, func(address string) {
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Address = address
}); err != nil {
p.openErrorModal(err)
Expand All @@ -157,7 +157,7 @@ func (p *OutputServerPage) initPage() {
input.SetBackgroundColor(tui.ColorFormBackground)
} else {
// Only update config when explicitly selecting a standard server.
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Address = selectedValue
}); err != nil {
p.openErrorModal(err)
Expand Down Expand Up @@ -203,7 +203,7 @@ func (p *OutputServerPage) initPage() {
}

// Update config with validated address
if err := p.display.sidecarCfg.Update(func(cfg *sidecar.Config) {
if err := p.display.sidecarCfg.Update(func(cfg *config.Config) {
cfg.OutputServer.Address = address
}); err != nil {
p.openErrorModal(err)
Expand Down
12 changes: 6 additions & 6 deletions cmd/cli/commands/install/page_40_output_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package install
import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/sidecar/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/ethpandaops/contributoor/pkg/config/v1"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -15,9 +15,9 @@ import (
// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestOutputServerPage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *sidecar.Config) *InstallDisplay {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *config.Config) *InstallDisplay {
if cfg.OutputServer == nil {
cfg.OutputServer = &sidecar.OutputServerConfig{}
cfg.OutputServer = &config.OutputServer{}
}

mockConfig := mock.NewMockConfigManager(ctrl)
Expand All @@ -39,7 +39,7 @@ func TestOutputServerPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewOutputServerPage(mockDisplay)
Expand All @@ -64,7 +64,7 @@ func TestOutputServerPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewOutputServerPage(mockDisplay)
Expand All @@ -78,7 +78,7 @@ func TestOutputServerPage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &sidecar.Config{})
mockDisplay := setupMockDisplay(ctrl, &config.Config{})

// Create the page.
page := NewOutputServerPage(mockDisplay)
Expand Down
Loading

0 comments on commit 76efd71

Please sign in to comment.