Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Create tracked reader interface #1511

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need comment doc for this interface.

io.Reader
Done()
Close()
Start()
StopManager()
}

type reader struct {
base io.Reader
offset int64
Expand All @@ -34,28 +42,20 @@ 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
Comment on lines +45 to -58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not right to merge managedReader into NewReader. I would suggest to move it to target.go.

}
tr.messenger, err = manager.Add()
return &tr, err
}

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manager should be the only way to access tty, trackable readers or targets should not access tty directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The weirdness here is because this PR is part of #1474

Now that the console interface has merged, maybe the older PR is small enough

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A manager controls output to tty and cannot be be shared between track.Readers. I think that's why the unit tests are failing.

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
Loading