Skip to content

Commit

Permalink
style: Add import statement for fmt in display.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent d77c5a7 commit e425b7e
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 23 deletions.
26 changes: 19 additions & 7 deletions cmd/cli/commands/install/display.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package install

import (
"fmt"

"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
Expand Down Expand Up @@ -77,7 +79,7 @@ func (d *InstallDisplay) Run() error {
"config_path": cfg.ContributoorDirectory,
"version": cfg.Version,
"run_method": cfg.RunMethod,
}).Info("Running installation wizard")
}).Debug("Running installation wizard")

return d.app.Run()
}
Expand Down Expand Up @@ -127,12 +129,22 @@ func (d *InstallDisplay) setPage(page *tui.Page) {

// OnComplete is called when the install wizard is complete.
func (d *InstallDisplay) OnComplete() error {
d.log.Infof("%sInstallation complete%s", tui.TerminalColorGreen, tui.TerminalColorReset)
d.log.Info("You can now manage contributoor using the following commands:")
d.log.Info(" contributoor start")
d.log.Info(" contributoor stop")
d.log.Info(" contributoor update")
d.log.Info(" contributoor config")
cfg := d.sidecarCfg.Get()

fmt.Printf("%sContributoor Status%s\n", tui.TerminalColorLightBlue, tui.TerminalColorReset)
fmt.Printf("%-20s: %s\n", "Version", cfg.Version)
fmt.Printf("%-20s: %s\n", "Run Method", cfg.RunMethod)
fmt.Printf("%-20s: %s\n", "Network", cfg.NetworkName)
fmt.Printf("%-20s: %s\n", "Beacon Node", cfg.BeaconNodeAddress)
fmt.Printf("%-20s: %s\n", "Config Path", d.sidecarCfg.GetConfigPath())

if cfg.OutputServer != nil {
fmt.Printf("%-20s: %s\n", "Output Server", cfg.OutputServer.Address)
}

fmt.Printf("\n%sInstallation complete%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)
fmt.Printf("You can now manage contributoor using the following command(s):\n")
fmt.Printf(" contributoor [start|stop|status|update|config]\n")

return nil
}
8 changes: 4 additions & 4 deletions cmd/cli/commands/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/ethpandaops/contributoor-installer/cmd/cli/options"
"github.com/ethpandaops/contributoor-installer/internal/installer"
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/sirupsen/logrus"
Expand All @@ -18,9 +17,10 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
Usage: "Start Contributoor",
UsageText: "contributoor start [options]",
Action: func(c *cli.Context) error {
log := opts.Logger()

installerCfg := installer.NewConfig()
var (
log = opts.Logger()
installerCfg = opts.InstallerConfig()
)

sidecarCfg, err := sidecar.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
Expand Down
117 changes: 117 additions & 0 deletions cmd/cli/commands/status/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package status

import (
"fmt"

"github.com/ethpandaops/contributoor-installer/cmd/cli/options"
"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
app.Commands = append(app.Commands, cli.Command{
Name: opts.Name(),
Aliases: opts.Aliases(),
Usage: "Show Contributoor status",
UsageText: "contributoor status [options]",
Action: func(c *cli.Context) error {
var (
log = opts.Logger()
installerCfg = opts.InstallerConfig()
)

sidecarCfg, err := sidecar.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
return fmt.Errorf("error loading config: %w", err)
}

dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarCfg, installerCfg)
if err != nil {
return fmt.Errorf("error creating docker sidecar service: %w", err)
}

binarySidecar, err := sidecar.NewBinarySidecar(log, sidecarCfg, installerCfg)
if err != nil {
return fmt.Errorf("error creating binary sidecar service: %w", err)
}

githubService, err := service.NewGitHubService(log, installerCfg)
if err != nil {
return fmt.Errorf("error creating github service: %w", err)
}

return showStatus(c, log, sidecarCfg, dockerSidecar, binarySidecar, githubService)
},
})
}

func showStatus(
c *cli.Context,
log *logrus.Logger,
config sidecar.ConfigManager,
docker sidecar.DockerSidecar,
binary sidecar.BinarySidecar,
github service.GitHubService,
) error {
var (
runner sidecar.SidecarRunner
cfg = config.Get()
)

// Determine which runner to use.
switch cfg.RunMethod {
case sidecar.RunMethodDocker:
runner = docker
case sidecar.RunMethodBinary:
runner = binary
default:
return fmt.Errorf("invalid sidecar run method: %s", cfg.RunMethod)
}

// Check if running.
running, err := runner.IsRunning()
if err != nil {
return fmt.Errorf("failed to check status: %w", err)
}

// Check if there's a newer version available.
var latestVersionLine string

latestVersion, err := github.GetLatestVersion()
if err == nil && cfg.Version != latestVersion {
latestVersionLine = fmt.Sprintf("%-20s: %s%s%s", "Latest Version", tui.TerminalColorYellow, latestVersion, tui.TerminalColorReset)
}

// Print status information.
fmt.Printf("%sContributoor Status%s\n", tui.TerminalColorLightBlue, tui.TerminalColorReset)
fmt.Printf("%-20s: %s\n", "Version", cfg.Version)

if latestVersionLine != "" {
fmt.Printf("%s\n", latestVersionLine)
}

fmt.Printf("%-20s: %s\n", "Run Method", cfg.RunMethod)
fmt.Printf("%-20s: %s\n", "Network", cfg.NetworkName)
fmt.Printf("%-20s: %s\n", "Beacon Node", cfg.BeaconNodeAddress)
fmt.Printf("%-20s: %s\n", "Config Path", config.GetConfigPath())

if cfg.OutputServer != nil {
fmt.Printf("%-20s: %s\n", "Output Server", cfg.OutputServer.Address)
}

// Print running status with color
statusColor := tui.TerminalColorRed
statusText := "Stopped"

if running {
statusColor = tui.TerminalColorGreen
statusText = "Running"
}

fmt.Printf("%-20s: %s%s%s\n", "Status", statusColor, statusText, tui.TerminalColorReset)

return nil
}
147 changes: 147 additions & 0 deletions cmd/cli/commands/status/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package status

import (
"fmt"
"testing"

servicemock "github.com/ethpandaops/contributoor-installer/internal/service/mock"
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/sidecar/mock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"go.uber.org/mock/gomock"
)

func TestShowStatus(t *testing.T) {
t.Run("shows status for running docker sidecar", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// Create mock config with docker setup
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(&sidecar.Config{
Version: "1.0.0",
RunMethod: sidecar.RunMethodDocker,
NetworkName: "mainnet",
BeaconNodeAddress: "http://localhost:5052",
OutputServer: &sidecar.OutputServerConfig{
Address: "https://output.server",
},
}).AnyTimes()
mockConfig.EXPECT().GetConfigPath().Return("/path/to/config.yaml")

// Create mock docker sidecar that's running
mockDocker := mock.NewMockDockerSidecar(ctrl)
mockDocker.EXPECT().IsRunning().Return(true, nil)

// Create mock binary sidecar (shouldn't be used)
mockBinary := mock.NewMockBinarySidecar(ctrl)

// Create mock GitHub service
mockGithub := servicemock.NewMockGitHubService(ctrl)
mockGithub.EXPECT().GetLatestVersion().Return("1.0.1", nil)

err := showStatus(
cli.NewContext(nil, nil, nil),
logrus.New(),
mockConfig,
mockDocker,
mockBinary,
mockGithub,
)

assert.NoError(t, err)
})

t.Run("shows status for stopped binary sidecar", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// Create mock config with binary setup
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(&sidecar.Config{
Version: "1.0.0",
RunMethod: sidecar.RunMethodBinary,
NetworkName: "mainnet",
BeaconNodeAddress: "http://localhost:5052",
}).AnyTimes()
mockConfig.EXPECT().GetConfigPath().Return("/path/to/config.yaml")

// Create mock docker sidecar (shouldn't be used)
mockDocker := mock.NewMockDockerSidecar(ctrl)

// Create mock binary sidecar that's stopped
mockBinary := mock.NewMockBinarySidecar(ctrl)
mockBinary.EXPECT().IsRunning().Return(false, nil)

// Create mock GitHub service with same version (shouldn't show update)
mockGithub := servicemock.NewMockGitHubService(ctrl)
mockGithub.EXPECT().GetLatestVersion().Return("1.0.0", nil)

err := showStatus(
cli.NewContext(nil, nil, nil),
logrus.New(),
mockConfig,
mockDocker,
mockBinary,
mockGithub,
)

assert.NoError(t, err)
})

t.Run("handles github service error gracefully", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(&sidecar.Config{
Version: "1.0.0",
RunMethod: sidecar.RunMethodDocker,
NetworkName: "mainnet",
}).AnyTimes()
mockConfig.EXPECT().GetConfigPath().Return("/path/to/config.yaml")

mockDocker := mock.NewMockDockerSidecar(ctrl)
mockDocker.EXPECT().IsRunning().Return(true, nil)

// Create mock GitHub service that returns an error
mockGithub := servicemock.NewMockGitHubService(ctrl)
mockGithub.EXPECT().GetLatestVersion().Return("", fmt.Errorf("github error"))

err := showStatus(
cli.NewContext(nil, nil, nil),
logrus.New(),
mockConfig,
mockDocker,
mock.NewMockBinarySidecar(ctrl),
mockGithub,
)

assert.NoError(t, err) // Should still succeed even with GitHub error
})

t.Run("handles invalid run method", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// Create mock config with invalid run method
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(&sidecar.Config{
RunMethod: "invalid",
})

err := showStatus(
cli.NewContext(nil, nil, nil),
logrus.New(),
mockConfig,
nil,
nil,
nil,
)

assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid sidecar run method")
})
}
8 changes: 4 additions & 4 deletions cmd/cli/commands/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/ethpandaops/contributoor-installer/cmd/cli/options"
"github.com/ethpandaops/contributoor-installer/internal/installer"
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/sirupsen/logrus"
Expand All @@ -18,9 +17,10 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
Usage: "Stop Contributoor",
UsageText: "contributoor stop [options]",
Action: func(c *cli.Context) error {
log := opts.Logger()

installerCfg := installer.NewConfig()
var (
log = opts.Logger()
installerCfg = opts.InstallerConfig()
)

sidecarCfg, err := sidecar.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/cli/commands/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/ethpandaops/contributoor-installer/cmd/cli/options"
"github.com/ethpandaops/contributoor-installer/internal/installer"
"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/sidecar"
"github.com/ethpandaops/contributoor-installer/internal/tui"
Expand All @@ -26,9 +25,10 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
},
},
Action: func(c *cli.Context) error {
log := opts.Logger()

installerCfg := installer.NewConfig()
var (
log = opts.Logger()
installerCfg = opts.InstallerConfig()
)

sidecarCfg, err := sidecar.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
Expand Down
Loading

0 comments on commit e425b7e

Please sign in to comment.