Skip to content

Commit

Permalink
Merge branch 'main' into add-lint-make-target
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryHowe authored Dec 16, 2024
2 parents b044ba9 + 245b141 commit c324960
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 43 deletions.
7 changes: 5 additions & 2 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ func NewTagHandler(printer *output.Printer, target option.Target) metadata.TagHa
}

// NewManifestPushHandler returns a manifest push handler.
func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandler {
return text.NewManifestPushHandler(printer)
func NewManifestPushHandler(printer *output.Printer, outputDescriptor bool, pretty bool, desc ocispec.Descriptor, target *option.Target) (status.ManifestPushHandler, metadata.ManifestPushHandler) {
if outputDescriptor {
return status.NewDiscardHandler(), metadata.NewDiscardHandler()
}
return status.NewTextManifestPushHandler(printer, desc), text.NewManifestPushHandler(printer, target)
}

// NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command.
Expand Down
10 changes: 10 additions & 0 deletions cmd/oras/internal/display/metadata/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func (Discard) OnFetched(string, ocispec.Descriptor, []byte) error {
return nil
}

// OnManifestPushed implements ManifestPushHandler.
func (Discard) OnManifestPushed(ocispec.Descriptor) error {
return nil
}

// Render implements ManifestPushHandler.
func (Discard) Render() error {
return nil
}

// OnTagged implements ManifestIndexCreateHandler.
func (Discard) OnTagged(ocispec.Descriptor, string) error {
return nil
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/metadata/discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ func TestDiscard_OnTagged(t *testing.T) {
t.Errorf("testDiscard.OnTagged() error = %v, want nil", err)
}
}

func TestDiscardHandler_OnManifestPushed(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestPushed(ocispec.Descriptor{}); err != nil {
t.Errorf("DiscardHandler.OnManifestPushed() error = %v, wantErr nil", err)
}
}
17 changes: 10 additions & 7 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"oras.land/oras/cmd/oras/internal/option"
)

// Renderer renders metadata information when an operation is complete.
type Renderer interface {
Render() error
}

// PushHandler handles metadata output for push events.
type PushHandler interface {
TaggedHandler
Renderer

OnCopied(opts *option.Target) error
OnCompleted(root ocispec.Descriptor) error
}

// Renderer renders metadata information when an operation is complete.
type Renderer interface {
Render() error
OnCopied(opts *option.Target, root ocispec.Descriptor) error
}

// AttachHandler handles metadata output for attach events.
Expand Down Expand Up @@ -83,6 +83,9 @@ type TagHandler interface {
// ManifestPushHandler handles metadata output for manifest push events.
type ManifestPushHandler interface {
TaggedHandler
Renderer

OnManifestPushed(desc ocispec.Descriptor) error
}

// ManifestIndexCreateHandler handles metadata output for index create events.
Expand Down
10 changes: 6 additions & 4 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type PushHandler struct {
path string
out io.Writer
tagged model.Tagged
root ocispec.Descriptor
}

// NewPushHandler creates a new handler for push events.
Expand All @@ -47,15 +48,16 @@ func (ph *PushHandler) OnTagged(desc ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
func (ph *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
if opts.RawReference != "" && !contentutil.IsDigest(opts.Reference) {
ph.tagged.AddTag(opts.Reference)
}
ph.path = opts.Path
ph.root = root
return nil
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return output.PrintPrettyJSON(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()))
// Render implements PushHandler.
func (ph *PushHandler) Render() error {
return output.PrintPrettyJSON(ph.out, model.NewPush(ph.root, ph.path, ph.tagged.Tags()))
}
10 changes: 6 additions & 4 deletions cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PushHandler struct {
path string
tagged model.Tagged
out io.Writer
root ocispec.Descriptor
}

// NewPushHandler returns a new handler for push events.
Expand All @@ -49,15 +50,16 @@ func (ph *PushHandler) OnTagged(desc ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
func (ph *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
if opts.RawReference != "" && !contentutil.IsDigest(opts.Reference) {
ph.tagged.AddTag(opts.Reference)
}
ph.path = opts.Path
ph.root = root
return nil
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return output.ParseAndWrite(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()), ph.template)
// Render implements PushHandler.
func (ph *PushHandler) Render() error {
return output.ParseAndWrite(ph.out, model.NewPush(ph.root, ph.path, ph.tagged.Tags()), ph.template)
}
17 changes: 16 additions & 1 deletion cmd/oras/internal/display/metadata/text/manifest_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,37 @@ package text
import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
)

// ManifestPushHandler handles text metadata output for manifest push events.
type ManifestPushHandler struct {
printer *output.Printer
target *option.Target
desc ocispec.Descriptor
}

// NewManifestPushHandler returns a new handler for manifest push events.
func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandler {
func NewManifestPushHandler(printer *output.Printer, target *option.Target) metadata.ManifestPushHandler {
return &ManifestPushHandler{
printer: printer,
target: target,
}
}

// OnTagged implements metadata.TaggedHandler.
func (h *ManifestPushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}

// OnManifestPushed implements metadata.ManifestPushHandler.
func (h *ManifestPushHandler) OnManifestPushed(desc ocispec.Descriptor) error {
h.desc = desc
return h.printer.Println("Pushed:", h.target.AnnotatedReference())
}

// Render implements metadata.ManifestPushHandler.
func (h *ManifestPushHandler) Render() error {
return h.printer.Println("Digest:", h.desc.Digest)
}
12 changes: 7 additions & 5 deletions cmd/oras/internal/display/metadata/text/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type PushHandler struct {
printer *output.Printer
tagLock sync.Mutex
root ocispec.Descriptor
}

// NewPushHandler returns a new handler for push events.
Expand All @@ -45,15 +46,16 @@ func (h *PushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (h *PushHandler) OnCopied(opts *option.Target) error {
func (h *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
h.root = root
return h.printer.Println("Pushed", opts.AnnotatedReference())
}

// OnCompleted is called after the push is completed.
func (h *PushHandler) OnCompleted(root ocispec.Descriptor) error {
err := h.printer.Println("ArtifactType:", root.ArtifactType)
// Render implements PushHandler.
func (h *PushHandler) Render() error {
err := h.printer.Println("ArtifactType:", h.root.ArtifactType)
if err != nil {
return err
}
return h.printer.Println("Digest:", root.Digest)
return h.printer.Println("Digest:", h.root.Digest)
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/text/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func TestPushHandler_OnCompleted(t *testing.T) {
printer := output.NewPrinter(tt.out, os.Stderr)
p := &PushHandler{
printer: printer,
root: tt.root,
}
if err := p.OnCompleted(tt.root); (err != nil) != tt.wantErr {
if err := p.Render(); (err != nil) != tt.wantErr {
t.Errorf("PushHandler.OnCompleted() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
15 changes: 15 additions & 0 deletions cmd/oras/internal/display/status/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ func (DiscardHandler) OnFetched(string, ocispec.Descriptor) error {
return nil
}

// OnManifestPushSkipped implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushSkipped() error {
return nil
}

// OnManifestPushing implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushing() error {
return nil
}

// OnManifestPushed implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushed() error {
return nil
}

// OnManifestRemoved implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnManifestRemoved(digest.Digest) error {
return nil
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestDiscardHandler_OnPushSkipped(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestPushSkipped(); err != nil {
t.Errorf("DiscardHandler.OnPushSkipped() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnManifestRemoved(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestRemoved("sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ type CopyHandler interface {
StopTracking() error
}

// ManifestPushHandler handles status output for manifest push command.
type ManifestPushHandler interface {
OnManifestPushSkipped() error
OnManifestPushing() error
OnManifestPushed() error
}

// ManifestIndexCreateHandler handles status output for manifest index create command.
type ManifestIndexCreateHandler interface {
OnFetching(manifestRef string) error
Expand Down
26 changes: 26 additions & 0 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ func (ch *TextCopyHandler) OnMounted(_ context.Context, desc ocispec.Descriptor)
return ch.printer.PrintStatus(desc, copyPromptMounted)
}

// TextManifestPushHandler handles text status output for manifest push events.
type TextManifestPushHandler struct {
desc ocispec.Descriptor
printer *output.Printer
}

// NewTextManifestPushHandler returns a new handler for manifest push command.
func NewTextManifestPushHandler(printer *output.Printer, desc ocispec.Descriptor) ManifestPushHandler {
return &TextManifestPushHandler{
desc: desc,
printer: printer,
}
}

func (mph *TextManifestPushHandler) OnManifestPushSkipped() error {
return mph.printer.PrintStatus(mph.desc, PushPromptExists)
}

func (mph *TextManifestPushHandler) OnManifestPushing() error {
return mph.printer.PrintStatus(mph.desc, PushPromptUploading)
}

func (mph *TextManifestPushHandler) OnManifestPushed() error {
return mph.printer.PrintStatus(mph.desc, PushPromptUploaded)
}

// TextManifestIndexCreateHandler handles text status output for manifest index create events.
type TextManifestIndexCreateHandler struct {
printer *output.Printer
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ func TestTextPushHandler_PreCopy(t *testing.T) {
validatePrinted(t, "Uploading 0b442c23c1dd oci-image")
}

func TestTextManifestPushHandler_OnPushSkipped(t *testing.T) {
mph := NewTextManifestPushHandler(printer, ocispec.Descriptor{})
if mph.OnManifestPushSkipped() != nil {
t.Error("OnManifestExists() should not return an error")
}
}

func TestTextManifestIndexUpdateHandler_OnManifestAdded(t *testing.T) {
tests := []struct {
name string
Expand Down
20 changes: 10 additions & 10 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Example - Push a manifest to an OCI image layout folder 'layout-dir' and tag wit
return option.Parse(cmd, &opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Printer.Verbose = opts.verbose && !opts.OutputDescriptor
opts.Printer.Verbose = opts.verbose
return pushManifest(cmd, opts)
},
}
Expand Down Expand Up @@ -151,6 +151,7 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {

// prepare manifest descriptor
desc := content.NewDescriptorFromBytes(mediaType, contentBytes)
statusHandler, metadataHandler := display.NewManifestPushHandler(opts.Printer, opts.OutputDescriptor, opts.Pretty.Pretty, desc, &opts.Target)

ref := opts.Reference
if ref == "" {
Expand All @@ -161,17 +162,17 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
return err
}
if match {
if err := opts.Printer.PrintStatus(desc, "Exists"); err != nil {
if err := statusHandler.OnManifestPushSkipped(); err != nil {
return err
}
} else {
if err = opts.Printer.PrintStatus(desc, "Uploading"); err != nil {
if err = statusHandler.OnManifestPushing(); err != nil {
return err
}
if _, err := oras.TagBytes(ctx, target, mediaType, contentBytes, ref); err != nil {
return err
}
if err = opts.Printer.PrintStatus(desc, "Uploaded "); err != nil {
if err = statusHandler.OnManifestPushed(); err != nil {
return err
}
}
Expand All @@ -192,18 +193,17 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
}
return opts.Output(os.Stdout, descJSON)
}
_ = opts.Printer.Println("Pushed", opts.AnnotatedReference())
if err := metadataHandler.OnManifestPushed(desc); err != nil {
return err
}
if len(opts.extraRefs) != 0 {
handler := display.NewManifestPushHandler(opts.Printer)
tagListener := listener.NewTaggedListener(target, handler.OnTagged)
tagListener := listener.NewTaggedListener(target, metadataHandler.OnTagged)
if _, err = oras.TagBytesN(ctx, tagListener, mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
return err
}
}

_ = opts.Printer.Println("Digest:", desc.Digest)

return nil
return metadataHandler.Render()
}

// matchDigest checks whether the manifest's digest matches to it in the remote
Expand Down
Loading

0 comments on commit c324960

Please sign in to comment.