Skip to content

Commit

Permalink
resolved billy's comment
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Oct 14, 2024
1 parent 6e3b09a commit 851701d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 62 deletions.
10 changes: 4 additions & 6 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandle
func NewManifestIndexCreateHandler(outputPath string, printer *output.Printer, pretty bool) (
status.ManifestIndexCreateHandler,
metadata.ManifestIndexCreateHandler,
content.ManifestIndexCreateHandler,
error) {
content.ManifestIndexCreateHandler) {
statusHandler := status.NewTextManifestIndexCreateHandler(printer)
metadataHandler := text.NewManifestIndexCreateHandler(printer)
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
Expand All @@ -190,15 +189,14 @@ func NewManifestIndexCreateHandler(outputPath string, printer *output.Printer, p
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
}
return statusHandler, metadataHandler, contentHandler, nil
return statusHandler, metadataHandler, contentHandler
}

// NewManifestIndexUpdateHandler returns status, metadata and content handlers for index update command.
func NewManifestIndexUpdateHandler(outputPath string, printer *output.Printer, pretty bool) (
status.ManifestIndexUpdateHandler,
metadata.ManifestIndexUpdateHandler,
content.ManifestIndexUpdateHandler,
error) {
content.ManifestIndexUpdateHandler) {
statusHandler := status.NewTextManifestIndexUpdateHandler(printer)
metadataHandler := text.NewManifestIndexUpdateHandler(printer)
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
Expand All @@ -209,7 +207,7 @@ func NewManifestIndexUpdateHandler(outputPath string, printer *output.Printer, p
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
}
return statusHandler, metadataHandler, contentHandler, nil
return statusHandler, metadataHandler, contentHandler
}

// NewCopyHandler returns copy handlers.
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func (discard) OnManifestRemoved(digest.Digest) error {
}

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

// OnIndexMerged implements ManifestIndexUpdateHandler.
func (discard) OnIndexMerged(string, digest.Digest) error {
func (discard) OnIndexMerged(string, ocispec.Descriptor) error {
return nil
}
10 changes: 5 additions & 5 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ManifestPushHandler interface {
TaggedHandler
}

type manifestPackHandler interface {
type ManifestPackHandler interface {
OnIndexPacked(desc ocispec.Descriptor) error
OnIndexPushed(path string) error
OnCompleted(desc ocispec.Descriptor) error
Expand All @@ -88,16 +88,16 @@ type manifestPackHandler interface {
// ManifestIndexCreateHandler handles metadata output for index create events.
type ManifestIndexCreateHandler interface {
TaggedHandler
manifestPackHandler
ManifestPackHandler
}

// ManifestIndexUpdateHandler handles metadata output for index update events.
type ManifestIndexUpdateHandler interface {
TaggedHandler
manifestPackHandler
ManifestPackHandler
OnManifestRemoved(digest digest.Digest) error
OnManifestAdded(manifestRef string, digest digest.Digest) error
OnIndexMerged(indexRef string, digest digest.Digest) error
OnManifestAdded(manifestRef string, desc ocispec.Descriptor) error
OnIndexMerged(indexRef string, desc ocispec.Descriptor) error
}

// CopyHandler handles metadata output for cp events.
Expand Down
55 changes: 55 additions & 0 deletions cmd/oras/internal/display/metadata/text/manifest_index_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 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/output"
"oras.land/oras/internal/descriptor"
)

// ManifestIndexCreateHandler handles text metadata output for index create events.
type ManifestIndexCreateHandler struct {
printer *output.Printer
}

// NewManifestIndexCreateHandler returns a new handler for index create events.
func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestIndexCreateHandler {
return &ManifestIndexCreateHandler{
printer: printer,
}
}

// OnIndexPacked implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnIndexPacked(desc ocispec.Descriptor) error {
return h.printer.Println("Packed", descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex)
}

// OnIndexPushed implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnIndexPushed(path string) error {
return h.printer.Println("Pushed", path)
}

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

// OnCompleted implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnCompleted(desc ocispec.Descriptor) error {
return h.printer.Println("Digest:", desc.Digest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,8 @@ import (
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/contentutil"
"oras.land/oras/internal/descriptor"
)

// ManifestIndexCreateHandler handles text metadata output for index create events.
type ManifestIndexCreateHandler struct {
printer *output.Printer
}

// NewManifestIndexCreateHandler returns a new handler for index create events.
func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestIndexCreateHandler {
return &ManifestIndexCreateHandler{
printer: printer,
}
}

// OnIndexPacked implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnIndexPacked(desc ocispec.Descriptor) error {
return h.printer.Println("Packed", descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex)
}

// OnIndexPushed implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnIndexPushed(path string) error {
return h.printer.Println("Pushed", path)
}

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

// OnCompleted implements metadata.ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnCompleted(desc ocispec.Descriptor) error {
return h.printer.Println("Digest:", desc.Digest)
}

type ManifestIndexUpdateHandler struct {
printer *output.Printer
}
Expand All @@ -73,19 +40,19 @@ func (miuh ManifestIndexUpdateHandler) OnManifestRemoved(digest digest.Digest) e
}

// OnManifestAdded implements metadata.ManifestIndexUpdateHandler.
func (miuh ManifestIndexUpdateHandler) OnManifestAdded(ref string, digest digest.Digest) error {
func (miuh ManifestIndexUpdateHandler) OnManifestAdded(ref string, desc ocispec.Descriptor) error {
if contentutil.IsDigest(ref) {
return miuh.printer.Println("Added", ref)
}
return miuh.printer.Println("Added", digest, ref)
return miuh.printer.Println("Added", desc.Digest, ref)
}

// OnIndexMerged implements metadata.ManifestIndexUpdateHandler.
func (miuh ManifestIndexUpdateHandler) OnIndexMerged(ref string, digest digest.Digest) error {
func (miuh ManifestIndexUpdateHandler) OnIndexMerged(ref string, desc ocispec.Descriptor) error {
if contentutil.IsDigest(ref) {
return miuh.printer.Println("Merged", ref)
}
return miuh.printer.Println("Merged", digest, ref)
return miuh.printer.Println("Merged", desc.Digest, ref)
}

// OnIndexUpdated implements metadata.ManifestIndexUpdateHandler.
Expand Down
11 changes: 6 additions & 5 deletions cmd/oras/root/manifest/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/status"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -107,7 +108,7 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
if err != nil {
return err
}
displayStatus, displayMetadata, displayContent, err := display.NewManifestIndexCreateHandler(opts.outputPath, opts.Printer, opts.Pretty.Pretty)
displayStatus, displayMetadata, displayContent := display.NewManifestIndexCreateHandler(opts.outputPath, opts.Printer, opts.Pretty.Pretty)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
return err
}
if opts.outputPath == "" {
if err := pushIndex(ctx, displayMetadata.OnIndexPushed, displayMetadata.OnTagged, target, desc, indexBytes, opts.Reference, opts.extraRefs, opts.AnnotatedReference()); err != nil {
if err := pushIndex(ctx, displayMetadata, displayMetadata, target, desc, indexBytes, opts.Reference, opts.extraRefs, opts.AnnotatedReference()); err != nil {
return err
}
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func getPlatform(ctx context.Context, target oras.ReadOnlyTarget, manifestBytes
return &platform, nil
}

func pushIndex(ctx context.Context, onIndexPushed func(path string) error, onTagged func(desc ocispec.Descriptor, tag string) error,
func pushIndex(ctx context.Context, mph metadata.ManifestPackHandler, th metadata.TaggedHandler,
target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path string) error {
// push the index
var err error
Expand All @@ -205,11 +206,11 @@ func pushIndex(ctx context.Context, onIndexPushed func(path string) error, onTag
if err != nil {
return err
}
if err := onIndexPushed(path); err != nil {
if err := mph.OnIndexPushed(path); err != nil {
return err
}
if len(extraRefs) != 0 {
tagListener := listener.NewTaggedListener(target, onTagged)
tagListener := listener.NewTaggedListener(target, th.OnTagged)
if _, err = oras.TagBytesN(ctx, tagListener, desc.MediaType, content, extraRefs, oras.DefaultTagBytesNOptions); err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/oras/root/manifest/index/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
if err := opts.EnsureReferenceNotEmpty(cmd, true); err != nil {
return err
}
displayStatus, displayMetadata, displayContent, err := display.NewManifestIndexUpdateHandler(opts.outputPath, opts.Printer, opts.Pretty.Pretty)
if err != nil {
return err
}
displayStatus, displayMetadata, displayContent := display.NewManifestIndexUpdateHandler(opts.outputPath, opts.Printer, opts.Pretty.Pretty)
index, err := fetchIndex(ctx, displayStatus, target, opts.Reference)
if err != nil {
return err
Expand Down Expand Up @@ -148,7 +145,7 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
return err
}
if opts.outputPath == "" {
if err := pushIndex(ctx, displayMetadata.OnIndexPushed, displayMetadata.OnTagged, target, desc, indexBytes, opts.Reference, opts.tags, path); err != nil {
if err := pushIndex(ctx, displayMetadata, displayMetadata, target, desc, indexBytes, opts.Reference, opts.tags, path); err != nil {
return err
}
}
Expand Down Expand Up @@ -198,7 +195,7 @@ func addManifests(ctx context.Context, displayStatus status.ManifestIndexUpdateH
}
}
manifests = append(manifests, desc)
if err := displayMetadata.OnManifestAdded(manifestRef, desc.Digest); err != nil {
if err := displayMetadata.OnManifestAdded(manifestRef, desc); err != nil {
return nil, err
}
}
Expand All @@ -225,7 +222,7 @@ func mergeIndexes(ctx context.Context, displayStatus status.ManifestIndexUpdateH
return nil, err
}
manifests = append(manifests, index.Manifests...)
if err := displayMetadata.OnIndexMerged(indexRef, desc.Digest); err != nil {
if err := displayMetadata.OnIndexMerged(indexRef, desc); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit 851701d

Please sign in to comment.