From 692fd3160c2d7a6a0fe3a9865525af66e540f0fc Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 17 Oct 2023 08:53:24 +0000 Subject: [PATCH] resolve comment Signed-off-by: Billy Zha --- cmd/oras/internal/display/track/target.go | 23 +++++++----------- cmd/oras/root/attach.go | 18 +++++++------- cmd/oras/root/cp.go | 2 +- cmd/oras/root/push.go | 29 ++++++++++++++++------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/cmd/oras/internal/display/track/target.go b/cmd/oras/internal/display/track/target.go index be62ac8c6..fa3e3b579 100644 --- a/cmd/oras/internal/display/track/target.go +++ b/cmd/oras/internal/display/track/target.go @@ -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 { @@ -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 @@ -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 @@ -90,7 +89,7 @@ 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 } @@ -98,18 +97,12 @@ func (rgt *referenceGraphTarget) PushReference(ctx context.Context, expected oci 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 diff --git a/cmd/oras/root/attach.go b/cmd/oras/root/attach.go index 1398ec9d8..e9c1ba7c7 100644 --- a/cmd/oras/root/attach.go +++ b/cmd/oras/root/attach.go @@ -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], @@ -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) { diff --git a/cmd/oras/root/cp.go b/cmd/oras/root/cp.go index 165d94893..4f61d279c 100644 --- a/cmd/oras/root/cp.go +++ b/cmd/oras/root/cp.go @@ -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{} diff --git a/cmd/oras/root/push.go b/cmd/oras/root/push.go index ac1284b94..386e69429 100644 --- a/cmd/oras/root/push.go +++ b/cmd/oras/root/push.go @@ -19,6 +19,7 @@ import ( "context" "errors" "fmt" + "os" "strings" "sync" @@ -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 @@ -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]) @@ -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 {