Skip to content

Commit

Permalink
Better UX on Push / Pull (#84)
Browse files Browse the repository at this point in the history
New push and pull experiences
  • Loading branch information
shizhMSFT committed Apr 19, 2019
1 parent 40d2096 commit 4223f0b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
40 changes: 29 additions & 11 deletions cmd/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package main
import (
"context"
"fmt"
"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"
"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 @@ -68,7 +72,7 @@ func runPull(opts pullOptions) error {
ctx := context.Background()
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
} else if !opts.verbose {
ctx = ctxo.WithLoggerDiscarded(ctx)
}
if opts.allowAllMediaTypes {
Expand All @@ -82,20 +86,34 @@ func runPull(opts pullOptions) error {
defer store.Close()
store.DisableOverwrite = opts.keepOldFiles
store.AllowPathTraversalOnWrite = opts.pathTraversal
desc, files, err := oras.Pull(ctx, resolver, opts.targetRef, store, oras.WithAllowedMediaTypes(opts.allowedMediaTypes))

desc, _, err := oras.Pull(ctx, resolver, opts.targetRef, store,
oras.WithAllowedMediaTypes(opts.allowedMediaTypes),
oras.WithPullCallbackHandler(pullStatusTrack()),
)
if err != nil {
return err
}
fmt.Println("Pulled", opts.targetRef)
fmt.Println("Digest:", desc.Digest)

return nil
}

if opts.verbose {
for _, file := range files {
if name, ok := content.ResolveName(file); ok {
fmt.Println(name)
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)
}
fmt.Println("Pulled", opts.targetRef)
fmt.Println(desc.Digest)
}

return nil
return nil, nil
})
}
70 changes: 46 additions & 24 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ 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 @@ -77,14 +79,13 @@ func runPush(opts pushOptions) error {
ctx := context.Background()
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
} else if !opts.verbose {
ctx = ctxo.WithLoggerDiscarded(ctx)
}

// load files
var (
annotations map[string]map[string]string
files []ocispec.Descriptor
store = content.NewFileStore("")
pushOpts []oras.PushOpt
)
Expand Down Expand Up @@ -112,6 +113,36 @@ func runPush(opts pushOptions) error {
if opts.pathValidationDisabled {
pushOpts = append(pushOpts, oras.WithNameValidation(nil))
}
files, err := loadFiles(store, annotations, &opts)
if err != nil {
return err
}

// ready to push
resolver := newResolver(opts.username, opts.password, opts.configs...)
pushOpts = append(pushOpts, oras.WithPushBaseHandler(pushStatusTrack()))
desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...)
if err != nil {
return err
}

fmt.Println("Pushed", opts.targetRef)
fmt.Println("Digest:", desc.Digest)

return nil
}

func decodeJSON(filename string, v interface{}) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
return json.NewDecoder(file).Decode(v)
}

func loadFiles(store *content.FileStore, annotations map[string]map[string]string, opts *pushOptions) ([]ocispec.Descriptor, error) {
var files []ocispec.Descriptor
for _, fileRef := range opts.fileRefs {
filename, mediaType := parseFileRef(fileRef, "")
name := filepath.Clean(filename)
Expand All @@ -120,11 +151,11 @@ func runPush(opts pushOptions) error {
name = filepath.ToSlash(name)
}
if opts.verbose {
fmt.Println(name)
fmt.Println("Preparing", name)
}
file, err := store.Add(name, mediaType, filename)
if err != nil {
return err
return nil, err
}
if annotations != nil {
if value, ok := annotations[filename]; ok {
Expand All @@ -139,26 +170,17 @@ func runPush(opts pushOptions) error {
}
files = append(files, file)
}

// ready to push
resolver := newResolver(opts.username, opts.password, opts.configs...)
desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...)
if err != nil {
return err
}

if opts.verbose {
fmt.Println("Pushed", opts.targetRef)
fmt.Println(desc.Digest)
}
return nil
return files, nil
}

func decodeJSON(filename string, v interface{}) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
return json.NewDecoder(file).Decode(v)
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: 1 addition & 0 deletions pkg/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func fetchContent(ctx context.Context, fetcher remotes.Fetcher, desc ocispec.Des
picker,
images.ChildrenHandler(store),
)
handlers = append(handlers, opts.callbackHandlers...)

if err := opts.dispatch(ctx, images.Handlers(handlers...), desc); err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions pkg/oras/pull_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type pullOpts struct {
allowedMediaTypes []string
dispatch func(context.Context, images.Handler, ...ocispec.Descriptor) error
baseHandlers []images.Handler
callbackHandlers []images.Handler
}

// PullOpt allows callers to set options on the oras pull
Expand Down Expand Up @@ -52,3 +53,12 @@ func WithPullBaseHandler(handlers ...images.Handler) PullOpt {
return nil
}
}

// WithPullCallbackHandler provides callback handlers, which will be called after
// any pull specific handlers.
func WithPullCallbackHandler(handlers ...images.Handler) PullOpt {
return func(o *pullOpts) error {
o.callbackHandlers = append(o.callbackHandlers, handlers...)
return nil
}
}

0 comments on commit 4223f0b

Please sign in to comment.