Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: status, metadata and content handlers for manifest index commands #1509

Merged
merged 28 commits into from
Nov 14, 2024
Merged
Changes from 26 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cmd/oras/internal/display/content/discard.go
Original file line number Diff line number Diff line change
@@ -17,14 +17,19 @@ package content

import ocispec "github.com/opencontainers/image-spec/specs-go/v1"

type discardHandler struct{}
type DiscardHandler struct{}

// OnContentFetched implements ManifestFetchHandler.
func (discardHandler) OnContentFetched(ocispec.Descriptor, []byte) error {
func (DiscardHandler) OnContentFetched(ocispec.Descriptor, []byte) error {
return nil
}

// OnContentCreated implements ManifestIndexCreateHandler.
func (DiscardHandler) OnContentCreated([]byte) error {
return nil
}

// NewDiscardHandler returns a new discard handler.
func NewDiscardHandler() ManifestFetchHandler {
return discardHandler{}
func NewDiscardHandler() DiscardHandler {
return DiscardHandler{}
}
9 changes: 9 additions & 0 deletions cmd/oras/internal/display/content/interface.go
Original file line number Diff line number Diff line change
@@ -24,3 +24,12 @@ type ManifestFetchHandler interface {
// OnContentFetched is called after the manifest content is fetched.
OnContentFetched(desc ocispec.Descriptor, content []byte) error
}

// ManifestIndexCreateHandler handles raw output for manifest index create events.
type ManifestIndexCreateHandler interface {
// OnContentCreated is called after the index content is created.
OnContentCreated(content []byte) error
}

// ManifestIndexUpdateHandler handles raw output for manifest index update events.
type ManifestIndexUpdateHandler ManifestIndexCreateHandler
4 changes: 4 additions & 0 deletions cmd/oras/internal/display/content/manifest_fetch.go
Original file line number Diff line number Diff line change
@@ -46,6 +46,10 @@ func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byt

// NewManifestFetchHandler creates a new handler.
func NewManifestFetchHandler(out io.Writer, pretty bool, outputPath string) ManifestFetchHandler {
// ignore --pretty when output to a file
if outputPath != "" && outputPath != "-" {
pretty = false
}
return &manifestFetch{
pretty: pretty,
stdout: out,
58 changes: 58 additions & 0 deletions cmd/oras/internal/display/content/manifest_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package content

import (
"fmt"
"io"
"os"

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

// manifestIndexCreate handles raw content output.
type manifestIndexCreate struct {
pretty bool
stdout io.Writer
outputPath string
}

// NewManifestIndexCreateHandler creates a new handler.
func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string) ManifestIndexCreateHandler {
// ignore --pretty when output to a file
if outputPath != "" && outputPath != "-" {
pretty = false
}
return &manifestIndexCreate{
pretty: pretty,
stdout: out,
outputPath: outputPath,
}
}

// OnContentCreated is called after index content is created.
func (h *manifestIndexCreate) OnContentCreated(manifest []byte) error {
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
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()
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
out = f
}
return output.PrintJSON(out, manifest, h.pretty)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}
28 changes: 28 additions & 0 deletions cmd/oras/internal/display/content/manifest_index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package content

import (
"os"
"testing"
)

func Test_manifestIndexCreate_OnContentCreated(t *testing.T) {
testHandler := NewManifestIndexCreateHandler(os.Stdout, false, "invalid/path")
if err := testHandler.OnContentCreated([]byte("test content")); err == nil {
t.Errorf("manifestIndexCreate.OnContentCreated() error = %v, wantErr non-nil error", err)
}
}
41 changes: 38 additions & 3 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
@@ -174,9 +174,44 @@ func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandle
return text.NewManifestPushHandler(printer)
}

// NewManifestIndexCreateHandler returns an index create handler.
func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestIndexCreateHandler {
return text.NewManifestIndexCreateHandler(printer)
// NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command.
func NewManifestIndexCreateHandler(outputPath string, printer *output.Printer, pretty bool) (status.ManifestIndexCreateHandler, metadata.ManifestIndexCreateHandler, content.ManifestIndexCreateHandler) {
var statusHandler status.ManifestIndexCreateHandler
var metadataHandler metadata.ManifestIndexCreateHandler
var contentHandler content.ManifestIndexCreateHandler
switch outputPath {
case "":
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewDiscardHandler()
case "-":
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
default:
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
}
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
return statusHandler, metadataHandler, contentHandler
}

// NewManifestIndexUpdateHandler returns status, metadata and content handlers for index update command.
func NewManifestIndexUpdateHandler(outputPath string, printer *output.Printer, pretty bool) (
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
status.ManifestIndexUpdateHandler,
metadata.ManifestIndexUpdateHandler,
content.ManifestIndexUpdateHandler) {
statusHandler := status.NewTextManifestIndexUpdateHandler(printer)
metadataHandler := text.NewManifestIndexCreateHandler(printer)
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
switch outputPath {
case "":
contentHandler = content.NewDiscardHandler()
case "-":
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
}
return statusHandler, metadataHandler, contentHandler
}

// NewCopyHandler returns copy handlers.
22 changes: 17 additions & 5 deletions cmd/oras/internal/display/metadata/discard.go
Original file line number Diff line number Diff line change
@@ -15,16 +15,28 @@ limitations under the License.

package metadata

import ocispec "github.com/opencontainers/image-spec/specs-go/v1"
import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

type discard struct{}
type Discard struct{}

// NewDiscardHandler creates a new handler that discards output for all events.
func NewDiscardHandler() discard {
return discard{}
func NewDiscardHandler() Discard {
return Discard{}
}

// OnFetched implements ManifestFetchHandler.
func (discard) OnFetched(string, ocispec.Descriptor, []byte) error {
func (Discard) OnFetched(string, ocispec.Descriptor, []byte) error {
return nil
}

// OnTagged implements ManifestIndexCreateHandler.
func (Discard) OnTagged(ocispec.Descriptor, string) error {
return nil
}

// OnCompleted implements ManifestIndexCreateHandler.
func (Discard) OnCompleted(ocispec.Descriptor) error {
return nil
}
29 changes: 29 additions & 0 deletions cmd/oras/internal/display/metadata/discard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metadata

import (
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestDiscard_OnTagged(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnTagged(ocispec.Descriptor{}, "test"); err != nil {
t.Errorf("testDiscard.OnTagged() error = %v, want nil", err)
}
}
4 changes: 4 additions & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
@@ -81,8 +81,12 @@ type ManifestPushHandler interface {
// ManifestIndexCreateHandler handles metadata output for index create events.
type ManifestIndexCreateHandler interface {
TaggedHandler
OnCompleted(desc ocispec.Descriptor) error
}

// ManifestIndexUpdateHandler handles metadata output for index update events.
type ManifestIndexUpdateHandler ManifestIndexCreateHandler

// CopyHandler handles metadata output for cp events.
type CopyHandler interface {
TaggedHandler
Original file line number Diff line number Diff line change
@@ -33,7 +33,12 @@ func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestInd
}
}

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

// OnCompleted implements ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnCompleted(desc ocispec.Descriptor) error {
return h.printer.Println("Digest:", desc.Digest)
}
36 changes: 36 additions & 0 deletions cmd/oras/internal/display/status/discard.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ package status
import (
"context"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)
@@ -88,3 +89,38 @@ func (DiscardHandler) OnNodeProcessing(desc ocispec.Descriptor) error {
func (DiscardHandler) OnNodeSkipped(desc ocispec.Descriptor) error {
return nil
}

// OnFetching implements referenceFetchHandler.
func (DiscardHandler) OnFetching(string) error {
return nil
}

// OnFetched implements referenceFetchHandler.
func (DiscardHandler) OnFetched(string, ocispec.Descriptor) error {
return nil
}

// OnManifestRemoved implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnManifestRemoved(digest.Digest) error {
return nil
}

// OnManifestAdded implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnManifestAdded(string, ocispec.Descriptor) error {
return nil
}

// OnIndexMerged implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnIndexMerged(string, ocispec.Descriptor) error {
return nil
}

// OnIndexPacked implements ManifestIndexCreateHandler.
func (DiscardHandler) OnIndexPacked(ocispec.Descriptor) error {
return nil
}

// OnIndexPushed implements ManifestIndexCreateHandler.
func (DiscardHandler) OnIndexPushed(string) error {
return nil
}
43 changes: 43 additions & 0 deletions cmd/oras/internal/display/status/discard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package status

import (
"testing"

v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestDiscardHandler_OnManifestRemoved(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestRemoved("sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"); err != nil {
t.Errorf("DiscardHandler.OnManifestRemoved() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnIndexMerged(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnIndexMerged("test", v1.Descriptor{}); err != nil {
t.Errorf("DiscardHandler.OnIndexMerged() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnIndexPushed(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnIndexPushed("test"); err != nil {
t.Errorf("DiscardHandler.OnIndexPushed() error = %v, wantErr nil", err)
}
}
18 changes: 18 additions & 0 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ package status

import (
"context"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)
@@ -61,3 +63,19 @@ type CopyHandler interface {
PostCopy(ctx context.Context, desc ocispec.Descriptor) error
OnMounted(ctx context.Context, desc ocispec.Descriptor) error
}

// ManifestIndexCreateHandler handles status output for manifest index create command.
type ManifestIndexCreateHandler interface {
OnFetching(manifestRef string) error
OnFetched(manifestRef string, desc ocispec.Descriptor) error
OnIndexPacked(desc ocispec.Descriptor) error
OnIndexPushed(path string) error
}

// ManifestIndexUpdateHandler handles status output for manifest index update command.
type ManifestIndexUpdateHandler interface {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
ManifestIndexCreateHandler
OnManifestRemoved(digest digest.Digest) error
OnManifestAdded(manifestRef string, desc ocispec.Descriptor) error
OnIndexMerged(indexRef string, desc ocispec.Descriptor) error
}
Loading
Loading