Skip to content

Commit

Permalink
move reporting of status to lib (#179)
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <avi@deitcher.net>

Co-authored-by: Josh Dolitsky <393494+jdolitsky@users.noreply.github.com>
  • Loading branch information
deitch and jdolitsky committed Oct 1, 2020
1 parent ad8a2d6 commit 78a9b6b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
25 changes: 2 additions & 23 deletions cmd/oras/pull.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
})
}
16 changes: 1 addition & 15 deletions cmd/oras/push.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
}
1 change: 0 additions & 1 deletion cmd/oras/resolver.go
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions pkg/oras/pull_opts.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
})
}
21 changes: 21 additions & 0 deletions 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"
Expand Down Expand Up @@ -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
})
}

0 comments on commit 78a9b6b

Please sign in to comment.