Skip to content

Commit

Permalink
refactor: Successor getting with separation of concerns
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
  • Loading branch information
TerryHowe committed Jul 11, 2024
1 parent d06bb81 commit 7c843ca
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 40 deletions.
7 changes: 6 additions & 1 deletion cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"

"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/descriptor"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
Expand Down Expand Up @@ -65,9 +66,13 @@ func (ph *TextPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetche
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := output.PrintSuccessorStatus(ctx, desc, fetcher, committed, ph.printer.StatusPrinter(PushPromptSkipped)); err != nil {
err, successors := descriptor.GetSuccessors(ctx, desc, fetcher, committed)
if err != nil {
return err
}
for _, successor := range successors {
_ = ph.printer.PrintStatus(successor, PushPromptSkipped)
}
return ph.printer.PrintStatus(desc, PushPromptUploaded)
}
}
Expand Down
18 changes: 13 additions & 5 deletions cmd/oras/internal/display/status/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"os"
"sync"

"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/cmd/oras/internal/display/status/track"
"oras.land/oras/internal/descriptor"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras/cmd/oras/internal/display/status/track"
)

// TTYPushHandler handles TTY status output for push command.
Expand Down Expand Up @@ -70,9 +70,17 @@ func (ph *TTYPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetcher
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return output.PrintSuccessorStatus(ctx, desc, fetcher, committed, func(d ocispec.Descriptor) error {
return ph.tracked.Prompt(d, PushPromptSkipped)
})
err, successors := descriptor.GetSuccessors(ctx, desc, fetcher, committed)
if err != nil {
return err

Check warning on line 75 in cmd/oras/internal/display/status/tty.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/tty.go#L75

Added line #L75 was not covered by tests
}
for _, successor := range successors {
err = ph.tracked.Prompt(successor, PushPromptSkipped)
if err != nil {
return err

Check warning on line 80 in cmd/oras/internal/display/status/tty.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/tty.go#L80

Added line #L80 was not covered by tests
}
}
return nil
}
}

Expand Down
30 changes: 0 additions & 30 deletions cmd/oras/internal/output/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ limitations under the License.
package output

import (
"context"
"fmt"
"io"
"sync"

"oras.land/oras/internal/descriptor"

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

// PrintFunc is the function type returned by StatusPrinter.
type PrintFunc func(ocispec.Descriptor) error

// Printer prints for status handlers.
type Printer struct {
out io.Writer
Expand Down Expand Up @@ -92,28 +87,3 @@ func (p *Printer) PrintStatus(desc ocispec.Descriptor, status string) error {
}
return p.Println(status, descriptor.ShortDigest(desc), name)
}

// StatusPrinter returns a tracking function for transfer status.
func (p *Printer) StatusPrinter(status string) PrintFunc {
return func(desc ocispec.Descriptor) error {
return p.PrintStatus(desc, status)
}
}

// PrintSuccessorStatus prints transfer status of successors.
func PrintSuccessorStatus(ctx context.Context, desc ocispec.Descriptor, fetcher content.Fetcher, committed *sync.Map, print PrintFunc) 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 := print(s); err != nil {
return err
}
}
}
return nil
}
21 changes: 17 additions & 4 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/descriptor"
"oras.land/oras/internal/docker"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/listener"
Expand Down Expand Up @@ -188,9 +189,13 @@ func doCopy(ctx context.Context, printer *output.Printer, src oras.ReadOnlyGraph
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := output.PrintSuccessorStatus(ctx, desc, dst, committed, printer.StatusPrinter(promptSkipped)); err != nil {
err, successors := descriptor.GetSuccessors(ctx, desc, dst, committed)
if err != nil {
return err
}
for _, successor := range successors {
_ = printer.PrintStatus(successor, promptSkipped)
}
return printer.PrintStatus(desc, promptCopied)
}
extendedCopyOptions.OnMounted = func(ctx context.Context, desc ocispec.Descriptor) error {
Expand All @@ -211,9 +216,17 @@ func doCopy(ctx context.Context, printer *output.Printer, src oras.ReadOnlyGraph
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return output.PrintSuccessorStatus(ctx, desc, tracked, committed, func(desc ocispec.Descriptor) error {
return tracked.Prompt(desc, promptSkipped)
})
err, successors := descriptor.GetSuccessors(ctx, desc, tracked, committed)
if err != nil {
return err

Check warning on line 221 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L221

Added line #L221 was not covered by tests
}
for _, successor := range successors {
err = tracked.Prompt(successor, promptSkipped)
if err != nil {
return err

Check warning on line 226 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L224-L226

Added lines #L224 - L226 were not covered by tests
}
}
return nil
}
extendedCopyOptions.OnMounted = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
Expand Down
18 changes: 18 additions & 0 deletions internal/descriptor/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ limitations under the License.
package descriptor

import (
"context"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"sync"

"oras.land/oras/internal/docker"
)
Expand Down Expand Up @@ -52,3 +55,18 @@ func GetTitleOrMediaType(desc ocispec.Descriptor) (name string, isTitle bool) {
func GenerateContentKey(desc ocispec.Descriptor) string {
return desc.Digest.String() + desc.Annotations[ocispec.AnnotationTitle]
}

// GetSuccessors prints transfer status of successors.
func GetSuccessors(ctx context.Context, desc ocispec.Descriptor, fetcher content.Fetcher, committed *sync.Map) (err error, successors []ocispec.Descriptor) {
allSuccessors, err := content.Successors(ctx, fetcher, desc)
if err != nil {
return err, nil

Check warning on line 63 in internal/descriptor/descriptor.go

View check run for this annotation

Codecov / codecov/patch

internal/descriptor/descriptor.go#L63

Added line #L63 was not covered by tests
}
for _, s := range allSuccessors {
name := s.Annotations[ocispec.AnnotationTitle]
if v, ok := committed.Load(s.Digest.String()); ok && v != name {
successors = append(successors, s)
}
}
return nil, successors
}

0 comments on commit 7c843ca

Please sign in to comment.