Skip to content

Commit

Permalink
Merge branch 'main' into index-fetch-print
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryHowe authored Dec 26, 2024
2 parents a20c926 + b72d1f5 commit af14715
Show file tree
Hide file tree
Showing 31 changed files with 272 additions and 105 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ check-encoding: ## check file CR/LF encoding
fix-encoding: ## fix file CR/LF encoding
find cmd internal -type f -name "*.go" -exec sed -i -e "s/\r//g" {} +

.PHONY: lint
lint: ## run CI version of lint
golangci-lint run ./...

.PHONY: tidy
tidy: ## go mod tidy
GO111MODULE=on $(GO_EXE) mod tidy
Expand Down
8 changes: 6 additions & 2 deletions cmd/oras/internal/display/content/manifest_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type manifestFetch struct {
outputPath string
}

func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byte) error {
func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byte) (eventErr error) {
out := h.stdout
if h.outputPath != "-" && h.outputPath != "" {
f, err := os.Create(h.outputPath)
if err != nil {
return fmt.Errorf("failed to open %q: %w", h.outputPath, err)
}
defer f.Close()
defer func() {
if err := f.Close(); eventErr == nil {
eventErr = err
}
}()
out = f
}
return output.PrintJSON(out, manifest, h.pretty)
Expand Down
8 changes: 6 additions & 2 deletions cmd/oras/internal/display/content/manifest_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string
}

// OnContentCreated is called after index content is created.
func (h *manifestIndexCreate) OnContentCreated(manifest []byte) error {
func (h *manifestIndexCreate) OnContentCreated(manifest []byte) (eventErr error) {
out := h.stdout
if h.outputPath != "" && h.outputPath != "-" {
f, err := os.Create(h.outputPath)
if err != nil {
return fmt.Errorf("failed to open %q: %w", h.outputPath, err)
}
defer f.Close()
defer func() {
if err := f.Close(); eventErr == nil {
eventErr = err
}
}()
out = f
}
return output.PrintJSON(out, manifest, h.pretty)
Expand Down
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
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ limitations under the License.
package display

import (
"oras.land/oras/internal/testutils"
"os"
"reflect"
"testing"

"oras.land/oras/internal/testutils"

"oras.land/oras/cmd/oras/internal/display/metadata/text"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/option"
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)
}
}
16 changes: 13 additions & 3 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ 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
OnCopied(opts *option.Target, root ocispec.Descriptor) error
}

// AttachHandler handles metadata output for attach events.
type AttachHandler interface {
OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error
Renderer

OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor)
}

// DiscoverHandler handles metadata output for discover events.
Expand Down Expand Up @@ -76,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
16 changes: 12 additions & 4 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (

// AttachHandler handles json metadata output for attach events.
type AttachHandler struct {
out io.Writer
out io.Writer
path string
root ocispec.Descriptor
}

// NewAttachHandler creates a new handler for attach events.
Expand All @@ -37,7 +39,13 @@ func NewAttachHandler(out io.Writer) metadata.AttachHandler {
}
}

// OnCompleted is called when the attach command is completed.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return output.PrintPrettyJSON(ah.out, model.NewAttach(root, opts.Path))
// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, _ ocispec.Descriptor) {
ah.path = target.Path
ah.root = root
}

// Render is called when the attach command is completed.
func (ah *AttachHandler) Render() error {
return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.path))
}
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()))
}
14 changes: 11 additions & 3 deletions cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
type AttachHandler struct {
template string
out io.Writer
path string
root ocispec.Descriptor
}

// NewAttachHandler returns a new handler for attach metadata events.
Expand All @@ -39,7 +41,13 @@ func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler {
}
}

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return output.ParseAndWrite(ah.out, model.NewAttach(root, opts.Path), ah.template)
// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, _ ocispec.Descriptor) {
ah.path = target.Path
ah.root = root
}

// Render formats the metadata of attach command.
func (ah *AttachHandler) Render() error {
return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.path), ah.template)
}
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)
}
28 changes: 19 additions & 9 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (

// AttachHandler handles text metadata output for attach events.
type AttachHandler struct {
printer *output.Printer
printer *output.Printer
subjectRefByDigest string
root ocispec.Descriptor
}

// NewAttachHandler returns a new handler for attach events.
Expand All @@ -37,16 +39,24 @@ func NewAttachHandler(printer *output.Printer) metadata.AttachHandler {
}
}

// OnCompleted is called when the attach command is complete.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
digest := subject.Digest.String()
if !strings.HasSuffix(opts.RawReference, digest) {
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)
// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor) {
ah.root = root
if strings.HasSuffix(target.RawReference, subject.Digest.String()) {
ah.subjectRefByDigest = target.AnnotatedReference()
} else {
// use subject digest instead of tag
newTarget := *target
newTarget.RawReference = fmt.Sprintf("%s@%s", target.Path, subject.Digest)
ah.subjectRefByDigest = newTarget.AnnotatedReference()
}
err := ah.printer.Println("Attached to", opts.AnnotatedReference())
}

// Render is called when the attach command is complete.
func (ah *AttachHandler) Render() error {
err := ah.printer.Println("Attached to", ah.subjectRefByDigest)
if err != nil {
return err
}
err = ah.printer.Println("Digest:", root.Digest)
return err
return ah.printer.Println("Digest:", ah.root.Digest)
}
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
Loading

0 comments on commit af14715

Please sign in to comment.