Skip to content

Commit

Permalink
change target to graph target
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 781874f commit 0dd031a
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions cmd/oras/internal/display/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,33 @@ package track

import (
"context"
"fmt"
"io"
"os"
"reflect"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/registry"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/display/progress"
)

// Target tracks the progress of pushing ot underlying oras.Target.
type Target struct {
oras.Target
oras.GraphTarget
manager progress.Manager
actionPrompt string
donePrompt string
}

// NewTarget creates a new tracked Target.
func NewTarget(t oras.Target, actionPrompt, donePrompt string, tty *os.File) (*Target, error) {
func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File) (*Target, error) {
manager, err := progress.NewManager(tty)
if err != nil {
return nil, err
}

return &Target{
Target: t,
GraphTarget: t,
manager: manager,
actionPrompt: actionPrompt,
donePrompt: donePrompt,
Expand All @@ -61,7 +58,7 @@ func (t *Target) Push(ctx context.Context, expected ocispec.Descriptor, content
}
defer r.Close()
r.Start()
if err := t.Target.Push(ctx, expected, r); err != nil {
if err := t.GraphTarget.Push(ctx, expected, r); err != nil {
return err
}
r.Done()
Expand All @@ -76,13 +73,13 @@ func (t *Target) PushReference(ctx context.Context, expected ocispec.Descriptor,
}
defer r.Close()
r.Start()
if rp, ok := t.Target.(registry.ReferencePusher); ok {
if rp, ok := t.GraphTarget.(registry.ReferencePusher); ok {
err = rp.PushReference(ctx, expected, r, reference)
} else {
if err := t.Target.Push(ctx, expected, r); err != nil {
if err := t.GraphTarget.Push(ctx, expected, r); err != nil {
return err
}
err = t.Target.Tag(ctx, expected, reference)
err = t.GraphTarget.Tag(ctx, expected, reference)
}
if err != nil {
return err
Expand All @@ -93,10 +90,7 @@ func (t *Target) PushReference(ctx context.Context, expected ocispec.Descriptor,

// Predecessors returns the predecessors of the node if supported.
func (t *Target) Predecessors(ctx context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if p, ok := t.Target.(content.PredecessorFinder); ok {
return p.Predecessors(ctx, node)
}
return nil, fmt.Errorf("Target %v does not support Predecessors", reflect.TypeOf(t.Target))
return t.GraphTarget.Predecessors(ctx, node)
}

// Close closes the tracking manager.
Expand Down

0 comments on commit 0dd031a

Please sign in to comment.