Skip to content

Commit

Permalink
refactor: Refactor configuration and installation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent 42acacd commit c8b11f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
28 changes: 16 additions & 12 deletions cmd/cli/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

Expand All @@ -16,23 +17,26 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
Usage: "Configure Contributoor settings",
UsageText: "contributoor config",
Action: func(c *cli.Context) error {
return showConfig(c, opts)
log := opts.Logger()

configService, err := service.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
return fmt.Errorf("%serror loading config: %v%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

return configureContributoor(c, log, configService)
},
})
}

func showConfig(c *cli.Context, opts *options.CommandOpts) error {
log := opts.Logger()

configService, err := service.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
return fmt.Errorf("%sError loading config: %v%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

app := tview.NewApplication()
func configureContributoor(c *cli.Context, log *logrus.Logger, config service.ConfigManager) error {
var (
app = tview.NewApplication()
display = NewConfigDisplay(log, app, config)
)

if err := NewConfigDisplay(log, app, configService).Run(); err != nil {
return fmt.Errorf("%sDisplay error: %w%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
if err := display.Run(); err != nil {
return fmt.Errorf("%sdisplay error: %w%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

return nil
Expand Down
30 changes: 15 additions & 15 deletions cmd/cli/commands/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
Usage: "Install Contributoor",
UsageText: "contributoor install [options]",
Action: func(c *cli.Context) error {
return installContributoor(c, opts)
log := opts.Logger()

configService, err := service.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
return fmt.Errorf("%serror loading config: %v%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

return installContributoor(c, log, configService)
},
Flags: []cli.Flag{
cli.StringFlag{
Expand All @@ -36,28 +43,21 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) {
})
}

// installContributoor is the action for the install command.
func installContributoor(c *cli.Context, opts *options.CommandOpts) error {
log := opts.Logger()
log.SetLevel(logrus.DebugLevel)

configService, err := service.NewConfigService(log, c.GlobalString("config-path"))
if err != nil {
return fmt.Errorf("%serror loading config: %v%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

app := tview.NewApplication()
d := NewInstallDisplay(log, app, configService)
func installContributoor(c *cli.Context, log *logrus.Logger, config service.ConfigManager) error {
var (
app = tview.NewApplication()
display = NewInstallDisplay(log, app, config)
)

// Run the display.
if err := d.Run(); err != nil {
if err := display.Run(); err != nil {
log.Errorf("error running display: %v", err)

return fmt.Errorf("%sdisplay error: %w%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
}

// Handle completion.
if err := d.OnComplete(); err != nil {
if err := display.OnComplete(); err != nil {
log.Errorf("error completing installation: %v", err)

return fmt.Errorf("%scompletion error: %w%s", tui.TerminalColorRed, err, tui.TerminalColorReset)
Expand Down

0 comments on commit c8b11f4

Please sign in to comment.