Skip to content

Commit

Permalink
chore: update console output messages in start, stop, and update funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent e425b7e commit 95de29d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
6 changes: 4 additions & 2 deletions cmd/cli/commands/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func startContributoor(
cfg = config.Get()
)

log.WithField("version", cfg.Version).Info("Starting Contributoor")
fmt.Printf("%sStarting Contributoor%s\n", tui.TerminalColorLightBlue, tui.TerminalColorReset)

// Start the sidecar via whatever method the user has configured (docker or binary).
switch cfg.RunMethod {
Expand All @@ -76,7 +76,9 @@ func startContributoor(

// If the sidecar is already running, we can just return.
if running {
return fmt.Errorf("%sContributoor is already running. Use 'contributoor stop' first if you want to restart it%s", tui.TerminalColorRed, tui.TerminalColorReset)
fmt.Printf("%sContributoor is already running. Use 'contributoor stop' first if you want to restart it%s\n", tui.TerminalColorYellow, tui.TerminalColorReset)

return nil
}

if err := runner.Start(); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/cli/commands/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func TestStartContributoor(t *testing.T) {
}).Times(1)
d.EXPECT().IsRunning().Return(true, nil)
},
expectedError: "Contributoor is already running",
},
{
name: "docker - start fails",
Expand Down Expand Up @@ -80,7 +79,6 @@ func TestStartContributoor(t *testing.T) {
}).Times(1)
b.EXPECT().IsRunning().Return(true, nil)
},
expectedError: "Contributoor is already running",
},
{
name: "invalid sidecar run method",
Expand Down
6 changes: 4 additions & 2 deletions cmd/cli/commands/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func stopContributoor(
cfg = config.Get()
)

log.WithField("version", cfg.Version).Info("Stopping Contributoor")
fmt.Printf("%sStopping Contributoor%s\n", tui.TerminalColorLightBlue, tui.TerminalColorReset)

// Stop the sidecar via whatever method the user has configured (docker or binary).
switch cfg.RunMethod {
Expand All @@ -76,7 +76,9 @@ func stopContributoor(

// If the service is not running, we can just return.
if !running {
return fmt.Errorf("%sContributoor is not running. Use 'contributoor start' to start it%s", tui.TerminalColorRed, tui.TerminalColorReset)
fmt.Printf("%sContributoor is not running. Use 'contributoor start' to start it%s\n", tui.TerminalColorYellow, tui.TerminalColorReset)

return nil
}

if err := runner.Stop(); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/cli/commands/stop/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func TestStopContributoor(t *testing.T) {
}).Times(1)
d.EXPECT().IsRunning().Return(false, nil)
},
expectedError: "Contributoor is not running",
},
{
name: "docker - stop fails",
Expand Down Expand Up @@ -80,7 +79,6 @@ func TestStopContributoor(t *testing.T) {
}).Times(1)
b.EXPECT().IsRunning().Return(false, nil)
},
expectedError: "Contributoor is not running",
},
{
name: "invalid sidecar run method",
Expand Down
46 changes: 25 additions & 21 deletions cmd/cli/commands/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func updateContributoor(
currentVersion = cfg.Version
)

log.WithField("version", currentVersion).Info("Current version")
fmt.Printf("%sUpdating Contributoor Version%s\n", tui.TerminalColorLightBlue, tui.TerminalColorReset)
fmt.Printf("%-20s: %s\n", "Current Version", cfg.Version)

defer func() {
if !success {
Expand All @@ -82,19 +83,21 @@ func updateContributoor(

// Determine target version.
targetVersion, err := determineTargetVersion(c, github)
if err != nil {
if err != nil || targetVersion == "" {
// Flag as success, there's nothing to update on rollback if we fail to determine the target version.
success = true

return err
}

fmt.Printf("%-20s: %s\n", "Latest Version", targetVersion)

// Check if update is needed.
if targetVersion == currentVersion {
// Flag as success, there's nothing to update.
success = true

logUpdateStatus(log, c.IsSet("version"), targetVersion)
printUpdateStatus(c.IsSet("version"), targetVersion)

return nil
}
Expand All @@ -107,21 +110,12 @@ func updateContributoor(
// Refresh our config state, given it was updated above.
cfg = sidecarCfg.Get()

log.WithField("version", cfg.Version).Info("Updating Contributoor")

// Update the sidecar.
success, err = updateSidecar(log, cfg, docker, binary)
if err != nil {
return err
}

log.Infof(
"%sContributoor updated successfully to version %s%s",
tui.TerminalColorGreen,
cfg.Version,
tui.TerminalColorReset,
)

return nil
}

Expand All @@ -147,12 +141,14 @@ func updateBinary(log *logrus.Logger, cfg *sidecar.Config, binary sidecar.Binary

// If the sidecar is running, we need to stop it before we can update the binary.
if running {
fmt.Printf("\n")

if tui.Confirm("Contributoor is running. In order to update, it must be stopped. Would you like to stop it?") {
if err := binary.Stop(); err != nil {
return false, fmt.Errorf("failed to stop sidecar: %w", err)
}
} else {
log.Error("update process was cancelled")
fmt.Printf("%sUpdate process was cancelled%s\n", tui.TerminalColorRed, tui.TerminalColorReset)

return false, nil
}
Expand All @@ -164,6 +160,8 @@ func updateBinary(log *logrus.Logger, cfg *sidecar.Config, binary sidecar.Binary
return false, err
}

fmt.Printf("%sContributoor updated successfully to version %s%s\n", tui.TerminalColorGreen, cfg.Version, tui.TerminalColorReset)

// If it was running, start it again for them.
if running {
if err := binary.Start(); err != nil {
Expand All @@ -181,6 +179,8 @@ func updateDocker(log *logrus.Logger, cfg *sidecar.Config, docker sidecar.Docker
return false, err
}

fmt.Printf("%sContributoor updated successfully to version %s%s\n", tui.TerminalColorGreen, cfg.Version, tui.TerminalColorReset)

// Check if service is currently running.
running, err := docker.IsRunning()
if err != nil {
Expand All @@ -189,6 +189,8 @@ func updateDocker(log *logrus.Logger, cfg *sidecar.Config, docker sidecar.Docker
return true, err
}

fmt.Printf("\n")

// If the service is running, we need to restart it with the new version.
if running {
if tui.Confirm("Contributoor is running. Would you like to restart it with the new version?") {
Expand All @@ -200,7 +202,7 @@ func updateDocker(log *logrus.Logger, cfg *sidecar.Config, docker sidecar.Docker
return true, fmt.Errorf("failed to start sidecar: %w", err)
}
} else {
log.Info("service will continue running with the previous version until next restart")
fmt.Printf("%sContributoor will continue running with the previous version until next restart%s\n", tui.TerminalColorYellow, tui.TerminalColorReset)
}
} else {
if tui.Confirm("Contributoor is not running. Would you like to start it?") {
Expand All @@ -223,12 +225,14 @@ func determineTargetVersion(c *cli.Context, github service.GitHubService) (strin
}

if !exists {
return "", fmt.Errorf(
"%sversion %s not found. Use 'contributoor update' without --version to get the latest version%s",
fmt.Printf(
"%sVersion %s not found. Use 'contributoor update' without --version to get the latest version%s\n",
tui.TerminalColorRed,
version,
tui.TerminalColorReset,
)

return "", nil
}

return version, nil
Expand Down Expand Up @@ -270,17 +274,17 @@ func rollbackVersion(log *logrus.Logger, config sidecar.ConfigManager, version s
return nil
}

func logUpdateStatus(log *logrus.Logger, isVersionSet bool, version string) {
func printUpdateStatus(isVersionSet bool, version string) {
if isVersionSet {
log.Infof(
"%scontributoor is already running version %s%s",
fmt.Printf(
"%sContributoor is already running version %s%s\n",
tui.TerminalColorGreen,
version,
tui.TerminalColorReset,
)
} else {
log.Infof(
"%scontributoor is up to date at version %s%s",
fmt.Printf(
"%sContributoor is up to date at version %s%s\n",
tui.TerminalColorGreen,
version,
tui.TerminalColorReset,
Expand Down
1 change: 0 additions & 1 deletion cmd/cli/commands/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func TestUpdateContributoor(t *testing.T) {
}).Times(1)
g.EXPECT().VersionExists("v999.0.0").Return(false, nil)
},
expectedError: "version v999.0.0 not found",
},
{
name: "binary - updates service successfully",
Expand Down
10 changes: 3 additions & 7 deletions internal/sidecar/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *binarySidecar) Start() error {
}
}()

s.logger.Info("Service started successfully")
fmt.Printf("%sContributoor started successfully%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)

return nil
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *binarySidecar) Stop() error {
s.stderr = nil
}

s.logger.Info("Service stopped and cleaned up successfully")
fmt.Printf("%sContributoor stopped successfully%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)

return nil
}
Expand Down Expand Up @@ -279,11 +279,7 @@ func (s *binarySidecar) Update() error {
return fmt.Errorf("failed to set binary permissions: %w", err)
}

s.logger.WithField("version", cfg.Version).Infof(
"%sBinary updated successfully%s",
tui.TerminalColorGreen,
tui.TerminalColorReset,
)
fmt.Printf("%sBinary updated successfully%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)

// Restart if it was running
if running {
Expand Down
8 changes: 4 additions & 4 deletions internal/sidecar/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *dockerSidecar) Start() error {
return fmt.Errorf("failed to start containers: %w\nOutput: %s", err, string(output))
}

s.logger.Info("Service started successfully")
fmt.Printf("%sContributoor started successfully%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)

return nil
}
Expand All @@ -77,7 +77,7 @@ func (s *dockerSidecar) Stop() error {
return fmt.Errorf("failed to stop containers: %w\nOutput: %s", err, string(output))
}

s.logger.Info("Service stopped and cleaned up successfully")
fmt.Printf("%sContributoor stopped successfully%s\n", tui.TerminalColorGreen, tui.TerminalColorReset)

return nil
}
Expand Down Expand Up @@ -114,8 +114,8 @@ func (s *dockerSidecar) Update() error {
return fmt.Errorf("failed to pull image %s: %w\nOutput: %s", image, err, string(output))
}

s.logger.WithField("version", cfg.Version).Infof(
"%sImage %s updated successfully%s",
fmt.Printf(
"%sImage %s updated successfully%s\n",
tui.TerminalColorGreen,
image,
tui.TerminalColorReset,
Expand Down

0 comments on commit 95de29d

Please sign in to comment.