Skip to content

Commit

Permalink
feat: support warning in remote targets
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Aug 9, 2023
1 parent 9520375 commit 84e3b16
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 23 deletions.
24 changes: 22 additions & 2 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strconv"
"strings"
"sync"

credentials "github.com/oras-project/oras-credentials-go"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -248,21 +249,30 @@ func (opts *Remote) Credential() auth.Credential {
}

// NewRegistry assembles a oras remote registry.
func (opts *Remote) NewRegistry(hostname string, common Common) (reg *remote.Registry, err error) {
func (opts *Remote) NewRegistry(hostname string, log func(...interface{}), common Common) (reg *remote.Registry, err error) {
reg, err = remote.NewRegistry(hostname)
if err != nil {
return nil, err
}
hostname = reg.Reference.Registry
reg.PlainHTTP = opts.isPlainHttp(hostname)

if opts.distributionSpec.referrersAPI == nil || *opts.distributionSpec.referrersAPI != false {

Check failure on line 260 in cmd/oras/internal/option/remote.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

S1002: should omit comparison to bool constant, can be simplified to `*opts.distributionSpec.referrersAPI` (gosimple)
once := sync.Once{}
reg.HandleWarning = func(warning remote.Warning) {
once.Do(func() {
log(warning.Text)
})
}
}
if reg.Client, err = opts.authClient(hostname, common.Debug); err != nil {
return nil, err
}
return
}

// NewRepository assembles a oras remote repository.
func (opts *Remote) NewRepository(reference string, common Common) (repo *remote.Repository, err error) {
func (opts *Remote) NewRepository(reference string, log func(...interface{}), common Common) (repo *remote.Repository, err error) {
repo, err = remote.NewRepository(reference)
if err != nil {
return nil, err
Expand All @@ -272,6 +282,16 @@ func (opts *Remote) NewRepository(reference string, common Common) (repo *remote
if repo.Client, err = opts.authClient(hostname, common.Debug); err != nil {
return nil, err
}

if opts.distributionSpec.referrersAPI == nil || *opts.distributionSpec.referrersAPI != false {

Check failure on line 286 in cmd/oras/internal/option/remote.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

S1002: should omit comparison to bool constant, can be simplified to `*opts.distributionSpec.referrersAPI` (gosimple)
once := sync.Once{}
repo.HandleWarning = func(warning remote.Warning) {
once.Do(func() {
log(warning.Text)
})
}
}

if opts.distributionSpec.referrersAPI != nil {
if err := repo.SetReferrersCapability(*opts.distributionSpec.referrersAPI); err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/option/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestRemote_NewRegistry(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
reg, err := opts.NewRegistry(uri.Host, opts.Common)
reg, err := opts.NewRegistry(uri.Host, nil, opts.Common)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestRemote_NewRepository(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
repo, err := opts.NewRepository(uri.Host+"/"+testRepo, opts.Common)
repo, err := opts.NewRepository(uri.Host+"/"+testRepo, nil, opts.Common)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestRemote_NewRepository_Retry(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
repo, err := opts.NewRepository(uri.Host+"/"+testRepo, opts.Common)
repo, err := opts.NewRepository(uri.Host+"/"+testRepo, nil, opts.Common)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/option/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func parseOCILayoutReference(raw string) (path string, ref string, err error) {
}

// NewTarget generates a new target based on opts.
func (opts *Target) NewTarget(common Common) (oras.GraphTarget, error) {
func (opts *Target) NewTarget(common Common, log func(...interface{})) (oras.GraphTarget, error) {
switch opts.Type {
case TargetTypeOCILayout:
var err error
Expand All @@ -121,7 +121,7 @@ func (opts *Target) NewTarget(common Common) (oras.GraphTarget, error) {
}
return oci.New(opts.Path)
case TargetTypeRemote:
repo, err := opts.NewRepository(opts.RawReference, common)
repo, err := opts.NewRepository(opts.RawReference, nil, common)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (opts *Target) NewReadonlyTarget(ctx context.Context, common Common) (ReadO
}
return oci.NewFromTar(ctx, opts.Path)
case TargetTypeRemote:
repo, err := opts.NewRepository(opts.RawReference, common)
repo, err := opts.NewRepository(opts.RawReference, nil, common)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func runAttach(ctx context.Context, opts attachOptions) error {
}
defer store.Close()

dst, err := opts.NewTarget(opts.Common)
dst, err := opts.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/blob/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Example - Delete a blob and print its descriptor:

func deleteBlob(ctx context.Context, opts deleteBlobOptions) (err error) {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
repo, err := opts.NewRepository(opts.targetRef, nil, opts.Common)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir':
}

func pushBlob(ctx context.Context, opts pushBlobOptions) (err error) {
ctx, _ = opts.WithContext(ctx)
ctx, logger := opts.WithContext(ctx)

repo, err := opts.NewTarget(opts.Common)
repo, err := opts.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
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 @@ -111,7 +111,7 @@ func runCopy(ctx context.Context, opts copyOptions) error {
}

// Prepare destination
dst, err := opts.To.NewTarget(opts.Common)
dst, err := opts.To.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func runLogin(ctx context.Context, opts loginOptions) (err error) {
if err != nil {
return err
}
remote, err := opts.Remote.NewRegistry(opts.Hostname, opts.Common)
remote, err := opts.Remote.NewRegistry(opts.Hostname, nil, opts.Common)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/manifest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614
}

func deleteManifest(ctx context.Context, opts deleteOptions) error {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
ctx, logger := opts.WithContext(ctx)
repo, err := opts.NewRepository(opts.targetRef, logger.Warn, opts.Common)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func pushManifest(ctx context.Context, opts pushOptions) error {
ctx, logger := opts.WithContext(ctx)
var target oras.Target
var err error
target, err = opts.NewTarget(opts.Common)
target, err = opts.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Example - Push file "hi.txt" into an OCI image layout folder 'layout-dir' with t
}

func runPush(ctx context.Context, opts pushOptions) error {
ctx, _ = opts.WithContext(ctx)
ctx, logger := opts.WithContext(ctx)
annotations, err := opts.LoadManifestAnnotations()
if err != nil {
return err
Expand Down Expand Up @@ -173,7 +173,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
}

// prepare push
dst, err := opts.NewTarget(opts.Common)
dst, err := opts.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/repo/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Example - List the repositories under the registry that include values lexically
}

func listRepository(ctx context.Context, opts repositoryOptions) error {
ctx, _ = opts.WithContext(ctx)
reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common)
ctx, logger := opts.WithContext(ctx)
reg, err := opts.Remote.NewRegistry(opts.hostname, logger.Warn, opts.Common)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Example - Tag the manifest 'v1.0.1' to 'v1.0.2' in an OCI image layout folder 'l
}

func tagManifest(ctx context.Context, opts tagOptions) error {
ctx, _ = opts.WithContext(ctx)
target, err := opts.NewTarget(opts.Common)
ctx, logger := opts.WithContext(ctx)
target, err := opts.NewTarget(opts.Common, logger.Warn)
if err != nil {
return err
}
Expand Down

0 comments on commit 84e3b16

Please sign in to comment.