Skip to content

Commit

Permalink
fix: refill missing status for push copy and attach (#481)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Aug 12, 2022
1 parent 8bd58d0 commit 0c1856e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
16 changes: 13 additions & 3 deletions cmd/oras/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main
import (
"context"
"fmt"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
Expand Down Expand Up @@ -108,6 +109,7 @@ func runAttach(opts attachOptions) error {
}

// Prepare Push
committed := &sync.Map{}
graphCopyOptions := oras.DefaultCopyGraphOptions
graphCopyOptions.FindSuccessors = func(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if isEqualOCIDescriptor(node, desc) {
Expand All @@ -117,9 +119,17 @@ func runAttach(opts attachOptions) error {
return content.Successors(ctx, fetcher, node)
}
graphCopyOptions.PreCopy = display.StatusPrinter("Uploading", opts.Verbose)
graphCopyOptions.OnCopySkipped = display.StatusPrinter("Exists ", opts.Verbose)
graphCopyOptions.PostCopy = display.StatusPrinter("Uploaded ", opts.Verbose)

graphCopyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintStatus(desc, "Exists ", opts.Verbose)
}
graphCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := display.PrintSuccessorStatus(ctx, desc, "Skipped ", store, committed, opts.Verbose); err != nil {
return err
}
return display.PrintStatus(desc, "Uploaded ", opts.Verbose)
}
// Push
err = oras.CopyGraph(ctx, store, dst, desc, graphCopyOptions)
if err != nil {
Expand Down
27 changes: 13 additions & 14 deletions cmd/oras/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main
import (
"context"
"fmt"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -84,30 +85,28 @@ func runCopy(opts copyOptions) error {
}

// Prepare copy options
committed := &sync.Map{}
extendedCopyOptions := oras.DefaultExtendedCopyOptions
outputStatus := func(status string) func(context.Context, ocispec.Descriptor) error {
return func(ctx context.Context, desc ocispec.Descriptor) error {
name, ok := desc.Annotations[ocispec.AnnotationTitle]
if !ok {
if !opts.Verbose {
return nil
}
name = desc.MediaType
}
return display.Print(status, display.ShortDigest(desc), name)
extendedCopyOptions.PreCopy = display.StatusPrinter("Copying", opts.Verbose)
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := display.PrintSuccessorStatus(ctx, desc, "Skipped", dst, committed, opts.Verbose); err != nil {
return err
}
return display.PrintStatus(desc, "Copied ", opts.Verbose)
}
extendedCopyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintStatus(desc, "Exists ", opts.Verbose)
}
extendedCopyOptions.PreCopy = outputStatus("Copying")
extendedCopyOptions.PostCopy = outputStatus("Copied ")
extendedCopyOptions.OnCopySkipped = outputStatus("Exists ")

if src.Reference.Reference == "" {
return newErrInvalidReference(src.Reference)
}

// push to the destination with digest only if no tag specified
var desc ocispec.Descriptor
if ref := dst.Reference.Reference; ref == "" {
// push to the destination with digest only if no tag specified
desc, err = src.Resolve(ctx, src.Reference.Reference)
if err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions cmd/oras/internal/display/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
)

var printLock sync.Mutex
Expand All @@ -46,3 +47,33 @@ func StatusPrinter(status string, verbose bool) func(context.Context, ocispec.De
return Print(status, ShortDigest(desc), name)
}
}

// PrintStatus prints transfer status.
func PrintStatus(desc ocispec.Descriptor, status string, verbose bool) error {
name, ok := desc.Annotations[ocispec.AnnotationTitle]
if !ok {
if !verbose {
return nil
}
name = desc.MediaType
}
return Print(status, ShortDigest(desc), name)
}

// PrintSuccessorStatus prints transfer status of successors.
func PrintSuccessorStatus(ctx context.Context, desc ocispec.Descriptor, status string, fetcher content.Fetcher, committed *sync.Map, verbose bool) error {
successors, err := content.Successors(ctx, fetcher, desc)
if err != nil {
return err
}
for _, s := range successors {
name := s.Annotations[ocispec.AnnotationTitle]
if v, ok := committed.Load(s.Digest.String()); ok && v != name {
// Reprint status for deduplicated content
if err := PrintStatus(s, status, verbose); err != nil {
return err
}
}
}
return nil
}
15 changes: 13 additions & 2 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -106,10 +107,20 @@ func runPush(opts pushOptions) error {
store.AllowPathTraversalOnWrite = opts.PathValidationDisabled

// Ready to push
committed := &sync.Map{}
copyOptions := oras.DefaultCopyOptions
copyOptions.PreCopy = display.StatusPrinter("Uploading", opts.Verbose)
copyOptions.OnCopySkipped = display.StatusPrinter("Exists ", opts.Verbose)
copyOptions.PostCopy = display.StatusPrinter("Uploaded ", opts.Verbose)
copyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintStatus(desc, "Exists ", opts.Verbose)
}
copyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := display.PrintSuccessorStatus(ctx, desc, "Skipped ", store, committed, opts.Verbose); err != nil {
return err
}
return display.PrintStatus(desc, "Uploaded ", opts.Verbose)
}
desc, err := packManifest(ctx, store, annotations, &opts)
if err != nil {
return err
Expand Down

0 comments on commit 0c1856e

Please sign in to comment.