Skip to content

Commit

Permalink
refactor: Improve build and deploy process with enhanced console feed…
Browse files Browse the repository at this point in the history
…back
  • Loading branch information
yarlson committed Nov 19, 2024
1 parent 94580ac commit b3362dd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
13 changes: 8 additions & 5 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,24 @@ func buildAndPushServices(ctx context.Context, services []config.Service, builde
}

func buildAndPushService(ctx context.Context, service config.Service, builder *build.Build, skipPush bool) error {
spinner := console.NewSpinner(fmt.Sprintf("building service %s", service.Name))
spinner := console.NewSpinner(fmt.Sprintf("Building service %s", service.Name))

if err := builder.Build(ctx, service.Image, service.Path); err != nil {
spinner.Fail("build failed")
spinner.Fail("Build failed")
return fmt.Errorf("failed to build image: %w", err)
}

spinner.Success(fmt.Sprintf("Service %s built successfully", service.Name))
spinner = console.NewSpinner(fmt.Sprintf("Pushing service %s", service.Name))

if !skipPush {
spinner.UpdateText(fmt.Sprintf("pushing service %s", service.Name))
spinner.UpdateText(fmt.Sprintf("Pushing service %s", service.Name))
if err := builder.Push(ctx, service.Image); err != nil {
spinner.Fail("push failed")
spinner.Fail("Push failed")
return fmt.Errorf("failed to push image: %w", err)
}
spinner.Success(fmt.Sprintf("Service %s pushed successfully", service.Name))
}

spinner.Success(fmt.Sprintf("service %s built successfully", service.Name))
return nil
}
29 changes: 17 additions & 12 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,56 @@ func deployToServer(project string, cfg *config.Config, server config.Server) er

events := deploy.Deploy(ctx, project, cfg)

var spinner *pterm.SpinnerPrinter
multi := pterm.DefaultMultiPrinter

spinners := make(map[string]*pterm.SpinnerPrinter)

_, _ = multi.Start()
defer func() { _, _ = multi.Stop() }()

for event := range events {
switch event.Type {
case deployment.EventTypeStart:
if spinner != nil {
spinner.Success()
}
spinner = console.NewSpinner(event.Message)
spinner := console.NewSpinnerWithWriter(event.Message, multi.NewWriter())
spinners[event.Name] = spinner
case deployment.EventTypeProgress:
if spinner != nil {
if spinner, ok := spinners[event.Name]; ok {
spinner.UpdateText(event.Message)
} else {
console.Info(event.Message)
}
case deployment.EventTypeFinish:
if spinner != nil {
if spinner, ok := spinners[event.Name]; ok {
spinner.Success(event.Message)
spinner = nil
delete(spinners, event.Name)
} else {
console.Success(event.Message)
}
case deployment.EventTypeError:
if spinner != nil {
if spinner, ok := spinners[event.Name]; ok {
spinner.Fail(fmt.Sprintf("Deployment error: %s", event.Message))
delete(spinners, event.Name)
} else {
console.Error(fmt.Sprintf("Deployment error: %s", event.Message))
}
return fmt.Errorf("deployment error: %s", event.Message)
case deployment.EventTypeComplete:
if spinner != nil {
if spinner, ok := spinners[event.Name]; ok {
spinner.Success(event.Message)
delete(spinners, event.Name)
} else {
console.Success(event.Message)
}
default:
if spinner != nil {
if spinner, ok := spinners[event.Name]; ok {
spinner.UpdateText(event.Message)
} else {
console.Info(event.Message)
}
}
}

if spinner != nil {
for _, spinner := range spinners {
_ = spinner.Stop()
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package console
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -86,6 +87,10 @@ func ReadPassword() (string, error) {
}

func NewSpinner(initialText string) *pterm.SpinnerPrinter {
return NewSpinnerWithWriter(initialText, os.Stdout)
}

func NewSpinnerWithWriter(initialText string, writer io.Writer) *pterm.SpinnerPrinter {
spinner := pterm.SpinnerPrinter{
Sequence: []string{" ⠋ ", " ⠙ ", " ⠹ ", " ⠸ ", " ⠼ ", " ⠴ ", " ⠦ ", " ⠧ ", " ⠇ ", " ⠏ "},
Style: &pterm.ThemeDefault.SpinnerStyle,
Expand All @@ -98,6 +103,7 @@ func NewSpinner(initialText string) *pterm.SpinnerPrinter {
SuccessPrinter: successPrinter,
FailPrinter: errorPrinter,
WarningPrinter: warningPrinter,
Writer: writer,
}

spinnerPrinter, _ := spinner.Start(initialText)
Expand Down

0 comments on commit b3362dd

Please sign in to comment.