-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: Add import statement for
fmt
in display.go
- Loading branch information
Showing
9 changed files
with
335 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.