-
Notifications
You must be signed in to change notification settings - Fork 181
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not right to merge |
||
} | ||
tr.messenger, err = manager.Add() | ||
return &tr, err | ||
} | ||
|
||
// StopManager stops the messenger channel and related manager. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ type GraphTarget interface { | |
|
||
type graphTarget struct { | ||
oras.GraphTarget | ||
tty *os.File | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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, | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A manager controls output to tty and cannot be be shared between |
||
if err != nil { | ||
return err | ||
} | ||
|
@@ -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 | ||
} | ||
|
There was a problem hiding this comment.
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.