Skip to content

Commit

Permalink
feat: add failure case
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Dec 31, 2024
1 parent 287db9d commit 9f19734
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/oras/internal/display/status/progress/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (m *Messenger) Update(status progress.Status) error {
}

func (m *Messenger) Fail(err error) error {
m.ch <- fail(err)
return nil

Check warning on line 37 in cmd/oras/internal/display/status/progress/messenger.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/progress/messenger.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}

Expand Down
26 changes: 22 additions & 4 deletions cmd/oras/internal/display/status/progress/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ var (
spinnerColor = aec.LightYellowF
doneMarkColor = aec.LightGreenF
progressColor = aec.LightBlueB
failureColor = aec.LightRedF
)

// status is used as message to update progress view.
type status struct {
done bool // done is true when the end time is set
err error
prompt string
descriptor ocispec.Descriptor
offset int64
Expand Down Expand Up @@ -90,6 +92,13 @@ func endTiming() *status {
}
}

func fail(err error) *status {
return &status{
err: err,
offset: -1,
}

Check warning on line 99 in cmd/oras/internal/display/status/progress/status.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/progress/status.go#L95-L99

Added lines #L95 - L99 were not covered by tests
}

func (s *status) isZero() bool {
return s.offset < 0 && s.startTime.IsZero() && s.endTime.IsZero()
}
Expand Down Expand Up @@ -140,9 +149,13 @@ func (s *status) String(width int) (string, string) {
lenBar := int(percent * barLength)
bar := fmt.Sprintf("[%s%s]", progressColor.Apply(strings.Repeat(" ", lenBar)), strings.Repeat(".", barLength-lenBar))
speed := s.calculateSpeed()
left = fmt.Sprintf("%s %s(%*s/s) %s %s",
spinnerColor.Apply(string(s.mark.symbol())),
bar, speedLength, speed, s.prompt, name)
var mark string
if s.err == nil {
mark = spinnerColor.Apply(string(s.mark.symbol()))
} else {
mark = failureColor.Apply("✗")
}

Check warning on line 157 in cmd/oras/internal/display/status/progress/status.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/progress/status.go#L156-L157

Added lines #L156 - L157 were not covered by tests
left = fmt.Sprintf("%s %s(%*s/s) %s %s", mark, bar, speedLength, speed, s.prompt, name)
// bar + wrapper(2) + space(1) + speed + "/s"(2) + wrapper(2) = len(bar) + len(speed) + 7
lenLeft = barLength + speedLength + 7
} else {
Expand Down Expand Up @@ -199,6 +212,9 @@ func (s *status) update(n *status) {
s.lock.Lock()
defer s.lock.Unlock()

if n.err != nil {
s.err = n.err
}

Check warning on line 217 in cmd/oras/internal/display/status/progress/status.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/progress/status.go#L216-L217

Added lines #L216 - L217 were not covered by tests
if n.offset >= 0 {
s.offset = n.offset
}
Expand All @@ -211,6 +227,8 @@ func (s *status) update(n *status) {
}
if !n.endTime.IsZero() {
s.endTime = n.endTime
s.done = true
if s.err == nil {
s.done = true
}
}
}

0 comments on commit 9f19734

Please sign in to comment.