From 78a9b6b637bbed567a120718a69e4536c3a2799e Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Thu, 1 Oct 2020 04:02:46 +0300 Subject: [PATCH] move reporting of status to lib (#179) Signed-off-by: Avi Deitcher Co-authored-by: Josh Dolitsky <393494+jdolitsky@users.noreply.github.com> --- cmd/oras/pull.go | 25 ++----------------------- cmd/oras/push.go | 16 +--------------- cmd/oras/resolver.go | 1 - pkg/oras/pull_opts.go | 27 +++++++++++++++++++++++++++ pkg/oras/push_opts.go | 21 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/cmd/oras/pull.go b/cmd/oras/pull.go index 512e02d14..72c57a8b9 100644 --- a/cmd/oras/pull.go +++ b/cmd/oras/pull.go @@ -3,16 +3,13 @@ package main import ( "context" "fmt" - "sync" + "os" "github.com/deislabs/oras/pkg/content" ctxo "github.com/deislabs/oras/pkg/context" "github.com/deislabs/oras/pkg/oras" - "github.com/containerd/containerd/images" "github.com/containerd/containerd/reference" - "github.com/opencontainers/go-digest" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -100,7 +97,7 @@ func runPull(opts pullOptions) error { desc, artifacts, err := oras.Pull(ctx, resolver, opts.targetRef, store, oras.WithAllowedMediaTypes(opts.allowedMediaTypes), - oras.WithPullCallbackHandler(pullStatusTrack()), + oras.WithPullStatusTrack(os.Stdout), ) if err != nil { if err == reference.ErrObjectRequired { @@ -116,21 +113,3 @@ func runPull(opts pullOptions) error { return nil } - -func pullStatusTrack() images.Handler { - var printLock sync.Mutex - return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { - if name, ok := content.ResolveName(desc); ok { - digestString := desc.Digest.String() - if err := desc.Digest.Validate(); err == nil { - if algo := desc.Digest.Algorithm(); algo == digest.SHA256 { - digestString = desc.Digest.Encoded()[:12] - } - } - printLock.Lock() - defer printLock.Unlock() - fmt.Println("Downloaded", digestString, name) - } - return nil, nil - }) -} diff --git a/cmd/oras/push.go b/cmd/oras/push.go index 818c242e1..70b40aa31 100644 --- a/cmd/oras/push.go +++ b/cmd/oras/push.go @@ -6,13 +6,11 @@ import ( "fmt" "os" "path/filepath" - "sync" "github.com/deislabs/oras/pkg/content" ctxo "github.com/deislabs/oras/pkg/context" "github.com/deislabs/oras/pkg/oras" - "github.com/containerd/containerd/images" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -133,7 +131,7 @@ func runPush(opts pushOptions) error { // ready to push resolver := newResolver(opts.username, opts.password, opts.insecure, opts.plainHTTP, opts.configs...) - pushOpts = append(pushOpts, oras.WithPushBaseHandler(pushStatusTrack())) + pushOpts = append(pushOpts, oras.WithPushStatusTrack(os.Stdout)) desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...) if err != nil { return err @@ -185,15 +183,3 @@ func loadFiles(store *content.FileStore, annotations map[string]map[string]strin } return files, nil } - -func pushStatusTrack() images.Handler { - var printLock sync.Mutex - return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { - if name, ok := content.ResolveName(desc); ok { - printLock.Lock() - defer printLock.Unlock() - fmt.Println("Uploading", desc.Digest.Encoded()[:12], name) - } - return nil, nil - }) -} diff --git a/cmd/oras/resolver.go b/cmd/oras/resolver.go index 9ee56be48..a9c5f1a17 100644 --- a/cmd/oras/resolver.go +++ b/cmd/oras/resolver.go @@ -29,7 +29,6 @@ func newResolver(username, password string, insecure bool, plainHTTP bool, confi } opts.Client = client - if username != "" || password != "" { opts.Credentials = func(hostName string) (string, string, error) { return username, password, nil diff --git a/pkg/oras/pull_opts.go b/pkg/oras/pull_opts.go index 2e604fabc..17eab5297 100644 --- a/pkg/oras/pull_opts.go +++ b/pkg/oras/pull_opts.go @@ -2,10 +2,14 @@ package oras import ( "context" + "fmt" + "io" + "sync" orascontent "github.com/deislabs/oras/pkg/content" "github.com/containerd/containerd/images" + "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sync/semaphore" ) @@ -87,3 +91,26 @@ func WithPullEmptyNameAllowed() PullOpt { return nil } } + +// WithPullStatusTrack report results to stdout +func WithPullStatusTrack(writer io.Writer) PullOpt { + return WithPullCallbackHandler(pullStatusTrack(writer)) +} + +func pullStatusTrack(writer io.Writer) images.Handler { + var printLock sync.Mutex + return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { + if name, ok := orascontent.ResolveName(desc); ok { + digestString := desc.Digest.String() + if err := desc.Digest.Validate(); err == nil { + if algo := desc.Digest.Algorithm(); algo == digest.SHA256 { + digestString = desc.Digest.Encoded()[:12] + } + } + printLock.Lock() + defer printLock.Unlock() + fmt.Fprintln(writer, "Downloaded", digestString, name) + } + return nil, nil + }) +} diff --git a/pkg/oras/push_opts.go b/pkg/oras/push_opts.go index 090620540..90d5716e0 100644 --- a/pkg/oras/push_opts.go +++ b/pkg/oras/push_opts.go @@ -1,8 +1,12 @@ package oras import ( + "context" + "fmt" + "io" "path/filepath" "strings" + "sync" "github.com/containerd/containerd/images" orascontent "github.com/deislabs/oras/pkg/content" @@ -127,3 +131,20 @@ func WithPushBaseHandler(handlers ...images.Handler) PushOpt { return nil } } + +// WithPushStatusTrack report results to a provided writer +func WithPushStatusTrack(writer io.Writer) PushOpt { + return WithPushBaseHandler(pushStatusTrack(writer)) +} + +func pushStatusTrack(writer io.Writer) images.Handler { + var printLock sync.Mutex + return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { + if name, ok := orascontent.ResolveName(desc); ok { + printLock.Lock() + defer printLock.Unlock() + fmt.Fprintln(writer, "Uploading", desc.Digest.Encoded()[:12], name) + } + return nil, nil + }) +}