Skip to content

Commit

Permalink
refactor: add display handler for cp, tag and manifest push (#1439
Browse files Browse the repository at this point in the history
)

Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Jul 4, 2024
1 parent f649643 commit e7ffb65
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 12 deletions.
15 changes: 15 additions & 0 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,18 @@ func NewManifestFetchHandler(out io.Writer, format option.Format, outputDescript
}
return metadataHandler, contentHandler, nil
}

// NewTagHandler returns a tag handler.
func NewTagHandler(printer *output.Printer, target option.Target) metadata.TagHandler {
return text.NewTagHandler(printer, target)
}

// NewManifestPushHandler returns a manifest push handler.
func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandler {
return text.NewManifestPushHandler(printer)
}

// NewCopyHandler returns a copy handler.
func NewCopyHandler(printer *output.Printer) metadata.CopyHandler {
return text.NewCopyHandler(printer)
}
10 changes: 10 additions & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ type TagHandler interface {
OnTagging(desc ocispec.Descriptor, tag string) error
TaggedHandler
}

// ManifestPushHandler handles metadata output for manifest push events.
type ManifestPushHandler interface {
TaggedHandler
}

// CopyHandler handles metadata output for cp events.
type CopyHandler interface {
TaggedHandler
}
39 changes: 39 additions & 0 deletions cmd/oras/internal/display/metadata/text/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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"
)

// CopyHandler handles text metadata output for cp events.
type CopyHandler struct {
printer *output.Printer
}

// NewCopyHandler returns a new handler for cp events.
func NewCopyHandler(printer *output.Printer) metadata.CopyHandler {
return &CopyHandler{
printer: printer,
}
}

// OnTagged implements metadata.TaggedHandler.
func (h *CopyHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}
39 changes: 39 additions & 0 deletions cmd/oras/internal/display/metadata/text/manifest_push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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"
)

// ManifestPushHandler handles text metadata output for manifest push events.
type ManifestPushHandler struct {
printer *output.Printer
}

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

// OnTagged implements metadata.TaggedHandler.
func (h *ManifestPushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/metadata/text/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewPushHandler(printer *output.Printer) metadata.PushHandler {
}
}

// OnTagged implements metadata.TextTagHandler.
// OnTagged implements metadata.TaggedHandler.
func (h *PushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
h.tagLock.Lock()
defer h.tagLock.Unlock()
Expand Down
9 changes: 6 additions & 3 deletions cmd/oras/internal/display/metadata/text/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ limitations under the License.
package text

import (
"fmt"
"sync"

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"
"sync"
)

// TagHandler handles text metadata output for tag events.
Expand All @@ -30,10 +33,10 @@ type TagHandler struct {
}

// NewTagHandler returns a new handler for tag events.
func NewTagHandler(printer *output.Printer, refPrefix string) metadata.TagHandler {
func NewTagHandler(printer *output.Printer, target option.Target) metadata.TagHandler {
return &TagHandler{
printer: printer,
refPrefix: refPrefix,
refPrefix: fmt.Sprintf("[%s] %s", target.Type, target.Path),
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display/metadata/text"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/display/status/track"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -142,8 +142,8 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error {
if len(opts.extraRefs) != 0 {
tagNOpts := oras.DefaultTagNOptions
tagNOpts.Concurrency = opts.concurrency
tagHandler := text.NewTagHandler(opts.Printer, "")
tagListener := listener.NewTagListener(dst, nil, tagHandler.OnTagged)
handler := display.NewCopyHandler(opts.Printer)
tagListener := listener.NewTaggedListener(dst, handler.OnTagged)
if _, err = oras.TagN(ctx, tagListener, opts.To.Reference, opts.extraRefs, tagNOpts); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display/metadata/text"
"oras.land/oras/cmd/oras/internal/display"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/manifest"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -190,8 +190,8 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
}
_ = opts.Println("Pushed", opts.AnnotatedReference())
if len(opts.extraRefs) != 0 {
tagHandler := text.NewTagHandler(opts.Printer, "")
tagListener := listener.NewTagListener(target, nil, tagHandler.OnTagged)
handler := display.NewManifestPushHandler(opts.Printer)
tagListener := listener.NewTaggedListener(target, handler.OnTagged)
if _, err = oras.TagBytesN(ctx, tagListener, mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/oras/root/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package root
import (
"errors"
"fmt"
"oras.land/oras/cmd/oras/internal/display/metadata/text"

"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/internal/listener"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -115,7 +116,7 @@ func tagManifest(cmd *cobra.Command, opts *tagOptions) error {

tagNOpts := oras.DefaultTagNOptions
tagNOpts.Concurrency = opts.concurrency
tagHandler := text.NewTagHandler(opts.Printer, fmt.Sprintf("[%s] %s", opts.Type, opts.Path))
tagHandler := display.NewTagHandler(opts.Printer, opts.Target)
tagListener := listener.NewTagListener(target, tagHandler.OnTagging, tagHandler.OnTagged)
_, err = oras.TagN(
ctx,
Expand Down
6 changes: 6 additions & 0 deletions internal/listener/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func NewTagListener(target oras.Target, onTagging, onTagged func(desc ocispec.De
}
}

// NewTaggedListener creates a wrapper type for printing all tagged statuses.
// It can only be used for oras.TagBytes and oras.TagBytesN.
func NewTaggedListener(target oras.Target, onTagged func(desc ocispec.Descriptor, tag string) error) oras.Target {
return NewTagListener(target, nil, onTagged)
}

type tagListenerForRepository struct {
registry.Repository
onTagging func(desc ocispec.Descriptor, tag string) error
Expand Down

0 comments on commit e7ffb65

Please sign in to comment.