From 9e5e7666e99582d5fd0be79df25de9d6a47de89a Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Sat, 13 Jul 2024 16:55:51 -0600 Subject: [PATCH] refactor: Move channel write logic to manager (#1445) 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) }