Skip to content

Commit

Permalink
fix: propagate any errors in src/pkg/message
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Sep 25, 2024
1 parent 5addccc commit 7c04dff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/pkg/message/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package message

import (
"fmt"
"log/slog"
"os"

"github.com/pterm/pterm"
Expand All @@ -22,17 +23,21 @@ type ProgressBar struct {
// NewProgressBar creates a new ProgressBar instance from a total value and a format.
func NewProgressBar(total int64, text string) *ProgressBar {
var progress *pterm.ProgressbarPrinter
var err error
if NoProgress {
Info(text)
} else {
progress, _ = pterm.DefaultProgressbar.
progress, err = pterm.DefaultProgressbar.
WithTotal(int(total)).
WithShowCount(false).
WithTitle(padding + text).
WithRemoveWhenDone(true).
WithMaxWidth(TermWidth).
WithWriter(os.Stderr).
Start()
if err != nil {
slog.Debug("Unable to create default progressbar", "error", err)
}
}

return &ProgressBar{
Expand All @@ -53,7 +58,10 @@ func (p *ProgressBar) Updatef(format string, a ...any) {

// Failf marks the ProgressBar as failed in the CLI.
func (p *ProgressBar) Failf(format string, a ...any) {
p.Close()
err := p.Close()
if err != nil {
slog.Debug("unable to close failed progressbar", "error", err)
}
Warnf(format, a...)
}

Expand Down Expand Up @@ -103,7 +111,10 @@ func (p *ProgressBar) Write(data []byte) (int, error) {

// Successf marks the ProgressBar as successful in the CLI.
func (p *ProgressBar) Successf(format string, a ...any) {
p.Close()
err := p.Close()
if err != nil {
slog.Debug("unable to close successful progressbar", "error", err)
}
pterm.Success.Printfln(format, a...)
}

Expand Down
12 changes: 10 additions & 2 deletions src/pkg/message/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
"log/slog"
"strings"

"github.com/pterm/pterm"
Expand All @@ -34,15 +35,19 @@ func NewProgressSpinner(format string, a ...any) *Spinner {
}

var spinner *pterm.SpinnerPrinter
var err error
text := pterm.Sprintf(format, a...)
if NoProgress {
Info(text)
} else {
spinner, _ = pterm.DefaultSpinner.
spinner, err = pterm.DefaultSpinner.
WithRemoveWhenDone(false).
// Src: https://github.com/gernest/wow/blob/master/spin/spinners.go#L335
WithSequence(sequence...).
Start(text)
if err != nil {
slog.Debug("unable to create default spinner", "error", err)
}
}

activeSpinner = &Spinner{
Expand Down Expand Up @@ -108,7 +113,10 @@ func (p *Spinner) Updatef(format string, a ...any) {
// Stop the spinner.
func (p *Spinner) Stop() {
if p.spinner != nil && p.spinner.IsActive {
_ = p.spinner.Stop()
err := p.spinner.Stop()
if err != nil {
slog.Debug("unable to stop spinner", "error", err)
}
}
activeSpinner = nil
}
Expand Down

0 comments on commit 7c04dff

Please sign in to comment.