From 9f19734efc601e9f43f697ed4ec54281554e340c Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Tue, 31 Dec 2024 17:34:09 +0800 Subject: [PATCH] feat: add failure case Signed-off-by: Shiwei Zhang --- .../display/status/progress/messenger.go | 1 + .../display/status/progress/status.go | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/oras/internal/display/status/progress/messenger.go b/cmd/oras/internal/display/status/progress/messenger.go index cb9de3fba..f4da6cc2b 100644 --- a/cmd/oras/internal/display/status/progress/messenger.go +++ b/cmd/oras/internal/display/status/progress/messenger.go @@ -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 } diff --git a/cmd/oras/internal/display/status/progress/status.go b/cmd/oras/internal/display/status/progress/status.go index 7d35bc1b2..57b46dadf 100644 --- a/cmd/oras/internal/display/status/progress/status.go +++ b/cmd/oras/internal/display/status/progress/status.go @@ -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 @@ -90,6 +92,13 @@ func endTiming() *status { } } +func fail(err error) *status { + return &status{ + err: err, + offset: -1, + } +} + func (s *status) isZero() bool { return s.offset < 0 && s.startTime.IsZero() && s.endTime.IsZero() } @@ -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("✗") + } + 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 { @@ -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 + } if n.offset >= 0 { s.offset = n.offset } @@ -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 + } } }