Skip to content

Commit

Permalink
resolve comment
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Oct 17, 2023
1 parent 060940b commit 692fd31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
23 changes: 8 additions & 15 deletions cmd/oras/internal/display/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
"oras.land/oras/cmd/oras/internal/display/progress"
)

type Trackable interface {
// GraphTarget is a tracked oras.GraphTarget.
type GraphTarget interface {
oras.GraphTarget
Close() error
Prompt(ocispec.Descriptor, string, bool) error
io.Closer
Prompt(desc ocispec.Descriptor, prompt string, verbose bool) error
}

type graphTarget struct {
Expand All @@ -42,11 +43,10 @@ type graphTarget struct {

type referenceGraphTarget struct {
*graphTarget
registry.ReferencePusher
}

// NewTarget creates a new tracked Target.
func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File) (Trackable, error) {
func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File) (GraphTarget, error) {
manager, err := progress.NewManager(tty)
if err != nil {
return nil, err
Expand All @@ -58,10 +58,9 @@ func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File
donePrompt: donePrompt,
}

if refPusher, ok := t.(registry.ReferencePusher); ok {
if _, ok := t.(registry.ReferencePusher); ok {
return &referenceGraphTarget{
graphTarget: gt,
ReferencePusher: refPusher,
graphTarget: gt,
}, nil
}
return gt, nil
Expand Down Expand Up @@ -90,26 +89,20 @@ func (rgt *referenceGraphTarget) PushReference(ctx context.Context, expected oci
}
defer r.Close()
r.Start()
err = rgt.ReferencePusher.PushReference(ctx, expected, r, reference)
err = rgt.GraphTarget.(registry.ReferencePusher).PushReference(ctx, expected, r, reference)
if err != nil {
return err
}
r.Done()
return nil
}

// Predecessors returns the predecessors of the node if supported.
func (t *graphTarget) Predecessors(ctx context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
return t.GraphTarget.Predecessors(ctx, node)
}

// Close closes the tracking manager.
func (t *graphTarget) Close() error {
return t.manager.Close()
}

// Prompt prompts the user with the provided prompt and descriptor.
// If Target is not set, only prints status.
func (t *graphTarget) Prompt(desc ocispec.Descriptor, prompt string, verbose bool) error {
if t == nil {
// this should not happen
Expand Down
18 changes: 8 additions & 10 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ func runAttach(ctx context.Context, opts attachOptions) error {
}

// prepare push
var tracked track.Trackable
if opts.TTY != nil {
tracked, err = track.NewTarget(dst, "Uploading", "Uploaded ", opts.TTY)
if err != nil {
return err
}
dst = tracked
var tracked track.GraphTarget
dst, tracked, err = getTrackedTarget(dst, opts.TTY)
if err != nil {
return err
}
graphCopyOptions := oras.DefaultCopyGraphOptions
graphCopyOptions.Concurrency = opts.concurrency
updateDisplayOption(&graphCopyOptions, store, opts.Verbose, tracked)

packOpts := oras.PackManifestOptions{
Subject: &subject,
ManifestAnnotations: annotations[option.AnnotationManifest],
Expand All @@ -149,9 +150,6 @@ func runAttach(ctx context.Context, opts attachOptions) error {
return oras.PackManifest(ctx, store, oras.PackManifestVersion1_1_RC4, opts.artifactType, packOpts)
}

graphCopyOptions := oras.DefaultCopyGraphOptions
graphCopyOptions.Concurrency = opts.concurrency
updateDisplayOption(&graphCopyOptions, store, opts.Verbose, tracked)
copy := func(root ocispec.Descriptor) error {
graphCopyOptions.FindSuccessors = func(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if content.Equal(node, root) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func runCopy(ctx context.Context, opts copyOptions) error {
}

func doCopy(ctx context.Context, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts copyOptions) (ocispec.Descriptor, error) {
var tracked track.Trackable
var tracked track.GraphTarget
var err error
// Prepare copy options
committed := &sync.Map{}
Expand Down
29 changes: 20 additions & 9 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -183,13 +184,10 @@ func runPush(ctx context.Context, opts pushOptions) error {
if err != nil {
return err
}
var tracked track.Trackable
if opts.TTY != nil {
tracked, err = track.NewTarget(dst, "Uploading", "Uploaded ", opts.TTY)
if err != nil {
return err
}
dst = tracked
var tracked track.GraphTarget
dst, tracked, err = getTrackedTarget(dst, opts.TTY)
if err != nil {
return err
}
copyOptions := oras.DefaultCopyOptions
copyOptions.Concurrency = opts.concurrency
Expand Down Expand Up @@ -234,14 +232,14 @@ func runPush(ctx context.Context, opts pushOptions) error {
}

func doPush(dst oras.Target, pack packFunc, copy copyFunc) (ocispec.Descriptor, error) {
if tracked, ok := dst.(track.Trackable); ok {
if tracked, ok := dst.(track.GraphTarget); ok {
defer tracked.Close()
}
// Push
return pushArtifact(dst, pack, copy)
}

func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, verbose bool, tracked track.Trackable) {
func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, verbose bool, tracked track.GraphTarget) {
committed := &sync.Map{}
opts.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
Expand Down Expand Up @@ -276,6 +274,19 @@ func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, v
type packFunc func() (ocispec.Descriptor, error)
type copyFunc func(desc ocispec.Descriptor) error

func getTrackedTarget(gt oras.GraphTarget, tty *os.File) (oras.GraphTarget, track.GraphTarget, error) {
var tracked track.GraphTarget
var err error
if tty != nil {
tracked, err = track.NewTarget(gt, "Uploading", "Uploaded ", tty)
if err != nil {
return nil, nil, err
}
gt = tracked
}
return gt, tracked, nil
}

func pushArtifact(dst oras.Target, pack packFunc, copy copyFunc) (ocispec.Descriptor, error) {
root, err := pack()
if err != nil {
Expand Down

0 comments on commit 692fd31

Please sign in to comment.