Skip to content

Commit

Permalink
refacor: No return for print errors
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
  • Loading branch information
TerryHowe committed May 6, 2024
1 parent 3812a2d commit 6c66d5a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/status/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ var printer = NewPrinter(os.Stdout)

// Print objects to display concurrent-safely.
func Print(a ...any) error {
return printer.Println(a...)
printer.Println(a...)
return printer.errored
}

// StatusPrinter returns a tracking function for transfer status.
Expand Down
20 changes: 15 additions & 5 deletions cmd/oras/internal/display/status/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"oras.land/oras/internal/descriptor"
"os"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -31,8 +32,9 @@ type PrintFunc func(ocispec.Descriptor) error

// Printer prints for status handlers.
type Printer struct {
out io.Writer
lock sync.Mutex
out io.Writer
lock sync.Mutex
errored error
}

// NewPrinter creates a new Printer.
Expand All @@ -41,11 +43,18 @@ func NewPrinter(out io.Writer) *Printer {
}

// Println prints objects concurrent-safely with newline.
func (p *Printer) Println(a ...any) error {
func (p *Printer) Println(a ...any) {
p.lock.Lock()
defer p.lock.Unlock()
_, err := fmt.Fprintln(p.out, a...)
return err
if err != nil {
if p.errored == nil {
p.errored = fmt.Errorf("display output error: %w", err)
_, _ = fmt.Fprint(os.Stderr, p.errored)
return
}
}
return

Check failure on line 57 in cmd/oras/internal/display/status/print.go

View workflow job for this annotation

GitHub Actions / lint (1.22)

S1023: redundant `return` statement (gosimple)
}

// PrintStatus prints transfer status.
Expand All @@ -58,7 +67,8 @@ func (p *Printer) PrintStatus(desc ocispec.Descriptor, status string, verbose bo
}
name = desc.MediaType
}
return p.Println(status, descriptor.ShortDigest(desc), name)
p.Println(status, descriptor.ShortDigest(desc), name)
return p.errored
}

// StatusPrinter returns a tracking function for transfer status.
Expand Down
6 changes: 4 additions & 2 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ func (ph *TextPushHandler) OnFileLoading(name string) error {
if !ph.verbose {
return nil
}
return ph.printer.Println("Preparing", name)
ph.printer.Println("Preparing", name)
return ph.printer.errored
}

// OnEmptyArtifact is called when an empty artifact is being uploaded.
func (ph *TextPushHandler) OnEmptyArtifact() error {
return ph.printer.Println("Uploading empty artifact")
ph.printer.Println("Uploading empty artifact")
return ph.printer.errored
}

// TrackTarget returns a tracked target.
Expand Down

0 comments on commit 6c66d5a

Please sign in to comment.