From 4947bb61a2e2962ffe74c5ea2bedb22a89a8c39d Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Mon, 30 Dec 2024 17:57:56 +0800 Subject: [PATCH] fix: fix status Signed-off-by: Shiwei Zhang --- .../display/status/progress/manager.go | 8 ++-- .../display/status/progress/messenger.go | 45 +------------------ .../display/status/progress/messenger_test.go | 13 +++--- .../display/status/progress/status.go | 38 ++++++++++++++-- .../display/status/progress/status_test.go | 30 ++++++------- 5 files changed, 57 insertions(+), 77 deletions(-) diff --git a/cmd/oras/internal/display/status/progress/manager.go b/cmd/oras/internal/display/status/progress/manager.go index 81b6b398c..07d0e1044 100644 --- a/cmd/oras/internal/display/status/progress/manager.go +++ b/cmd/oras/internal/display/status/progress/manager.go @@ -105,18 +105,17 @@ func (m *manager) Track(desc ocispec.Descriptor) (progress.Tracker, error) { return nil, errManagerStopped } - s := newStatus() - s.descriptor = desc + s := newStatus(desc) m.statusLock.Lock() m.status = append(m.status, s) m.statusLock.Unlock() defer m.console.NewRow() defer m.console.NewRow() - return m.statusChan(s, desc), nil + return m.statusChan(s), nil } -func (m *manager) statusChan(s *status, desc ocispec.Descriptor) progress.Tracker { +func (m *manager) statusChan(s *status) progress.Tracker { ch := make(chan *status, BufferSize) m.updating.Add(1) go func() { @@ -127,7 +126,6 @@ func (m *manager) statusChan(s *status, desc ocispec.Descriptor) progress.Tracke }() return &Messenger{ ch: ch, - desc: desc, prompt: m.prompt, } } diff --git a/cmd/oras/internal/display/status/progress/messenger.go b/cmd/oras/internal/display/status/progress/messenger.go index 8d1e201ec..cb9de3fba 100644 --- a/cmd/oras/internal/display/status/progress/messenger.go +++ b/cmd/oras/internal/display/status/progress/messenger.go @@ -15,19 +15,12 @@ limitations under the License. package progress -import ( - "time" - - ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "oras.land/oras/cmd/oras/internal/display/status/progress/humanize" - "oras.land/oras/internal/progress" -) +import "oras.land/oras/internal/progress" // Messenger is progress message channel. type Messenger struct { ch chan *status closed bool - desc ocispec.Descriptor prompt map[progress.State]string } @@ -60,7 +53,7 @@ func (m *Messenger) start() { func (m *Messenger) send(prompt string, offset int64) { for { select { - case m.ch <- newStatusMessage(prompt, m.desc, offset): + case m.ch <- newStatusMessage(prompt, offset): return case <-m.ch: // purge the channel until successfully pushed @@ -80,37 +73,3 @@ func (m *Messenger) stop() { close(m.ch) m.closed = true } - -// newStatus generates a base empty status. -func newStatus() *status { - return &status{ - offset: -1, - total: humanize.ToBytes(0), - speedWindow: newSpeedWindow(framePerSecond), - } -} - -// newStatusMessage generates a status for messaging. -func newStatusMessage(prompt string, descriptor ocispec.Descriptor, offset int64) *status { - return &status{ - prompt: prompt, - descriptor: descriptor, - offset: offset, - } -} - -// startTiming creates start timing message. -func startTiming() *status { - return &status{ - offset: -1, - startTime: time.Now(), - } -} - -// endTiming creates end timing message. -func endTiming() *status { - return &status{ - offset: -1, - endTime: time.Now(), - } -} diff --git a/cmd/oras/internal/display/status/progress/messenger_test.go b/cmd/oras/internal/display/status/progress/messenger_test.go index 8ae5b385c..e41ea25db 100644 --- a/cmd/oras/internal/display/status/progress/messenger_test.go +++ b/cmd/oras/internal/display/status/progress/messenger_test.go @@ -24,14 +24,8 @@ import ( func Test_Messenger(t *testing.T) { var msg *status ch := make(chan *status, BufferSize) - desc := ocispec.Descriptor{ - Digest: "mouse", - Size: 100, - } - messenger := &Messenger{ - ch: ch, - desc: desc, - } + + messenger := &Messenger{ch: ch} messenger.start() select { @@ -43,6 +37,9 @@ func Test_Messenger(t *testing.T) { t.Error("Expected start message") } + desc := ocispec.Descriptor{ + Size: 100, + } expected := int64(50) messenger.send("Reading", expected) select { diff --git a/cmd/oras/internal/display/status/progress/status.go b/cmd/oras/internal/display/status/progress/status.go index feb653834..7d35bc1b2 100644 --- a/cmd/oras/internal/display/status/progress/status.go +++ b/cmd/oras/internal/display/status/progress/status.go @@ -56,6 +56,40 @@ type status struct { lock sync.Mutex } +// newStatus generates a base empty status. +func newStatus(desc ocispec.Descriptor) *status { + return &status{ + descriptor: desc, + offset: -1, + total: humanize.ToBytes(desc.Size), + speedWindow: newSpeedWindow(framePerSecond), + } +} + +// newStatusMessage generates a status for messaging. +func newStatusMessage(prompt string, offset int64) *status { + return &status{ + prompt: prompt, + offset: offset, + } +} + +// startTiming creates start timing message. +func startTiming() *status { + return &status{ + offset: -1, + startTime: time.Now(), + } +} + +// endTiming creates end timing message. +func endTiming() *status { + return &status{ + offset: -1, + endTime: time.Now(), + } +} + func (s *status) isZero() bool { return s.offset < 0 && s.startTime.IsZero() && s.endTime.IsZero() } @@ -167,10 +201,6 @@ func (s *status) update(n *status) { if n.offset >= 0 { s.offset = n.offset - if n.descriptor.Size != s.descriptor.Size { - s.total = humanize.ToBytes(n.descriptor.Size) - } - s.descriptor = n.descriptor } if n.prompt != "" { s.prompt = n.prompt diff --git a/cmd/oras/internal/display/status/progress/status_test.go b/cmd/oras/internal/display/status/progress/status_test.go index 8542d651d..cc03f44af 100644 --- a/cmd/oras/internal/display/status/progress/status_test.go +++ b/cmd/oras/internal/display/status/progress/status_test.go @@ -29,22 +29,20 @@ import ( func Test_status_String(t *testing.T) { // zero status and progress - s := newStatus() + s := newStatus(ocispec.Descriptor{ + MediaType: "application/vnd.oci.empty.oras.test.v1+json", + Size: 2, + Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", + }) if status, digest := s.String(console.MinWidth); status != zeroStatus || digest != zeroDigest { t.Errorf("status.String() = %v, %v, want %v, %v", status, digest, zeroStatus, zeroDigest) } // not done s.update(&status{ - prompt: "test", - descriptor: ocispec.Descriptor{ - MediaType: "application/vnd.oci.empty.oras.test.v1+json", - Size: 2, - Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", - }, + prompt: "test", startTime: time.Now().Add(-time.Minute), offset: 0, - total: humanize.ToBytes(2), }) // full name statusStr, digestStr := s.String(120) @@ -70,22 +68,20 @@ func Test_status_String(t *testing.T) { func Test_status_String_zeroWidth(t *testing.T) { // zero status and progress - s := newStatus() + s := newStatus(ocispec.Descriptor{ + MediaType: "application/vnd.oci.empty.oras.test.v1+json", + Size: 0, + Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }) if status, digest := s.String(console.MinWidth); status != zeroStatus || digest != zeroDigest { t.Errorf("status.String() = %v, %v, want %v, %v", status, digest, zeroStatus, zeroDigest) } // not done s.update(&status{ - prompt: "test", - descriptor: ocispec.Descriptor{ - MediaType: "application/vnd.oci.empty.oras.test.v1+json", - Size: 0, - Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - }, + prompt: "test", startTime: time.Now().Add(-time.Minute), offset: 0, - total: humanize.ToBytes(0), }) // not done statusStr, digestStr := s.String(120) @@ -105,7 +101,7 @@ func Test_status_String_zeroWidth(t *testing.T) { } func Test_status_durationString(t *testing.T) { // zero duration - s := newStatus() + s := newStatus(ocispec.Descriptor{}) if d := s.durationString(); d != zeroDuration { t.Errorf("status.durationString() = %v, want %v", d, zeroDuration) }