From f16a83385e7ee6a3b400264728fe451cda48c2dd Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Fri, 12 Jul 2024 15:46:06 -0600 Subject: [PATCH] refactor: Move channel write logic to manager Signed-off-by: Terry Howe --- .../internal/display/status/progress/manager.go | 14 ++++++++++++++ cmd/oras/internal/display/status/track/target.go | 9 +-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cmd/oras/internal/display/status/progress/manager.go b/cmd/oras/internal/display/status/progress/manager.go index b17d61e00..09929156a 100644 --- a/cmd/oras/internal/display/status/progress/manager.go +++ b/cmd/oras/internal/display/status/progress/manager.go @@ -21,6 +21,7 @@ import ( "sync" "time" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" "oras.land/oras/cmd/oras/internal/display/status/console" ) @@ -39,6 +40,7 @@ type Status chan *status // Manager is progress view master type Manager interface { Add() (Status, error) + SendAndStop(desc ocispec.Descriptor, prompt string) error Close() error } @@ -120,6 +122,18 @@ func (m *manager) Add() (Status, error) { return m.statusChan(s), nil } +// SendAndStop send message for descriptor and stop timing. +func (m *manager) SendAndStop(desc ocispec.Descriptor, prompt string) error { + status, err := m.Add() + if err != nil { + return err + } + defer close(status) + status <- NewStatusMessage(prompt, desc, desc.Size) + status <- EndTiming() + return nil +} + func (m *manager) statusChan(s *status) Status { ch := make(chan *status, BufferSize) m.updating.Add(1) diff --git a/cmd/oras/internal/display/status/track/target.go b/cmd/oras/internal/display/status/track/target.go index 9bd54d240..5c704ebbc 100644 --- a/cmd/oras/internal/display/status/track/target.go +++ b/cmd/oras/internal/display/status/track/target.go @@ -110,12 +110,5 @@ func (t *graphTarget) Close() error { // Prompt prompts the user with the provided prompt and descriptor. func (t *graphTarget) Prompt(desc ocispec.Descriptor, prompt string) error { - status, err := t.manager.Add() - if err != nil { - return err - } - defer close(status) - status <- progress.NewStatusMessage(prompt, desc, desc.Size) - status <- progress.EndTiming() - return nil + return t.manager.SendAndStop(desc, prompt) }