Skip to content

Commit

Permalink
refactor: Move channel write logic to manager (#1445)
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
Terry Howe authored Jul 13, 2024
1 parent d06bb81 commit 9e5e766
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 14 additions & 0 deletions cmd/oras/internal/display/status/progress/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions cmd/oras/internal/display/status/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 9e5e766

Please sign in to comment.