Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Mar 29, 2024
1 parent d839760 commit 7535fef
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
9 changes: 7 additions & 2 deletions cmd/oras/internal/display/status/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ func (DiscardHandler) OnEmptyArtifact() error {
}

// TrackTarget returns a target with status tracking
func (DiscardHandler) TrackTarget(gt oras.GraphTarget) (oras.GraphTarget, error) {
return gt, nil
func (DiscardHandler) TrackTarget(oras.GraphTarget) (oras.GraphTarget, error) {
return nil, nil
}

// Track implements PullHandler.
func (DiscardHandler) Track(oras.GraphTarget) error {
return nil
}

// Close implements io.Closer.
Expand Down
5 changes: 2 additions & 3 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ type AttachHandler PushHandler

// PullHandler handles status output for pull command.
type PullHandler interface {
// TrackTarget returns a tracked target.
// If no TTY is available, it returns the original target.
TrackTarget(gt oras.GraphTarget) (oras.GraphTarget, error)
// Track tracks status of operations upon gt.
Track(gt oras.GraphTarget) error
// OnNodeProcessing is called when processing a manifest.
OnNodeProcessing(desc ocispec.Descriptor) error
// OnNodeDownloading is called before downloading a node.
Expand Down
8 changes: 3 additions & 5 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ func NewTextAttachHandler(out io.Writer, verbose bool) AttachHandler {

// TextPullHandler handles text status output for pull events.
type TextPullHandler struct {
fetcher content.Fetcher
verbose bool
printer *Printer
}

// TrackTarget returns a tracked target.
func (ph *TextPullHandler) TrackTarget(gt oras.GraphTarget) (oras.GraphTarget, error) {
ph.fetcher = gt
return gt, nil
// Track implements
func (ph *TextPullHandler) Track(oras.GraphTarget) error {
return nil
}

// Close implements io.Closer.
Expand Down
10 changes: 4 additions & 6 deletions cmd/oras/internal/display/status/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@ func (ph *TTYPullHandler) Close() error {
return ph.tracked.Close()
}

// TrackTarget returns a tracked target.
func (ph *TTYPullHandler) TrackTarget(gt oras.GraphTarget) (oras.GraphTarget, error) {
func (ph *TTYPullHandler) Track(gt oras.GraphTarget) error {
tracked, err := track.NewTarget(gt, PullPromptDownloading, PullPromptPulled, ph.tty)
if err != nil {
return nil, err
if err == nil {
ph.tracked = tracked
}
ph.tracked = tracked
return tracked, nil
return err
}
8 changes: 2 additions & 6 deletions cmd/oras/internal/display/status/tty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,15 @@ func Test_TTYPullHandler_TrackTarget(t *testing.T) {
}
defer device.Close()
ph := NewTTYPullHandler(device)
got, err := ph.TrackTarget(src)
err = ph.Track(src)
if err != nil {
t.Fatal(err)
}
if got == src {
t.Fatal("GraphTarget not be modified on TTY")
}
})

t.Run("invalid TTY", func(t *testing.T) {
ph := NewTTYPullHandler(nil)

if _, err := ph.TrackTarget(src); err == nil {
if err := ph.Track(src); err == nil {
t.Fatal("expected error for no tty but got nil")
}
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func doPull(ctx context.Context, src oras.ReadOnlyTarget, dst oras.GraphTarget,
return ocispec.Descriptor{}, false, err
}
}
dst, err = statusHandler.TrackTarget(dst)
err = statusHandler.Track(dst)
if err != nil {
return ocispec.Descriptor{}, false, err
}
Expand Down

0 comments on commit 7535fef

Please sign in to comment.