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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TerryHowe
Copy link
Member

What this PR does / why we need it:

Create an interface for the tracked reader.

This breaks out a small change from #1474

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
@@ -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.

@@ -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.

Comment on lines +45 to -58
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
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.

@@ -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
Contributor

@qweeah qweeah left a comment

Choose a reason for hiding this comment

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

Thanks Terry for contributing. The overall direction of this PR is good but still need some refinement on the detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants