Skip to content

Commit

Permalink
refactor: Create tracked reader interface
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
TerryHowe committed Sep 28, 2024
1 parent e0f4066 commit 7df760f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
25 changes: 12 additions & 13 deletions cmd/oras/internal/display/status/track/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import (
"oras.land/oras/cmd/oras/internal/display/status/progress"
)

type Reader interface {
io.Reader
Done()
Close()
Start()
StopManager()
}

type reader struct {
base io.Reader
offset int64
Expand All @@ -34,28 +42,19 @@ type reader struct {
}

// NewReader returns a new reader with tracked progress.
func NewReader(r io.Reader, descriptor ocispec.Descriptor, actionPrompt string, donePrompt string, tty *os.File) (*reader, error) {
func NewReader(r io.Reader, descriptor ocispec.Descriptor, actionPrompt string, donePrompt string, tty *os.File) (Reader, error) {
manager, err := progress.NewManager(tty)
if err != nil {
return nil, err
}
return managedReader(r, descriptor, manager, actionPrompt, donePrompt)
}

func managedReader(r io.Reader, descriptor ocispec.Descriptor, manager progress.Manager, actionPrompt string, donePrompt string) (*reader, error) {
messenger, err := manager.Add()
if err != nil {
return nil, err
}

return &reader{
tr := reader{
base: r,
descriptor: descriptor,
actionPrompt: actionPrompt,
donePrompt: donePrompt,
manager: manager,
messenger: messenger,
}, nil
}
return &tr, nil
}

// StopManager stops the messenger channel and related manager.
Expand Down
6 changes: 4 additions & 2 deletions cmd/oras/internal/display/status/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type GraphTarget interface {

type graphTarget struct {
oras.GraphTarget
tty *os.File
manager progress.Manager
actionPrompt string
donePrompt string
Expand All @@ -52,6 +53,7 @@ func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File
}
gt := &graphTarget{
GraphTarget: t,
tty: tty,
manager: manager,
actionPrompt: actionPrompt,
donePrompt: donePrompt,
Expand All @@ -74,7 +76,7 @@ func (t *graphTarget) Mount(ctx context.Context, desc ocispec.Descriptor, fromRe

// Push pushes the content to the base oras.GraphTarget with tracking.
func (t *graphTarget) Push(ctx context.Context, expected ocispec.Descriptor, content io.Reader) error {
r, err := managedReader(content, expected, t.manager, t.actionPrompt, t.donePrompt)
r, err := NewReader(content, expected, t.actionPrompt, t.donePrompt, t.tty)
if err != nil {
return err
}
Expand All @@ -89,7 +91,7 @@ func (t *graphTarget) Push(ctx context.Context, expected ocispec.Descriptor, con

// PushReference pushes the content to the base oras.GraphTarget with tracking.
func (rgt *referenceGraphTarget) PushReference(ctx context.Context, expected ocispec.Descriptor, content io.Reader, reference string) error {
r, err := managedReader(content, expected, rgt.manager, rgt.actionPrompt, rgt.donePrompt)
r, err := NewReader(content, expected, rgt.actionPrompt, rgt.donePrompt, rgt.tty)
if err != nil {
return err
}
Expand Down

0 comments on commit 7df760f

Please sign in to comment.