Skip to content

Commit

Permalink
change on completed for attach
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
Xiaoxuan Wang committed Dec 10, 2024
1 parent 48dc019 commit da87eb3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 29 deletions.
8 changes: 4 additions & 4 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewPushHandler(printer *output.Printer, format option.Format, tty *os.File,
}

// NewAttachHandler returns status and metadata handlers for attach command.
func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.File, fetcher fetcher.Fetcher) (status.AttachHandler, metadata.AttachHandler, error) {
func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.File, fetcher fetcher.Fetcher, target *option.Target) (status.AttachHandler, metadata.AttachHandler, error) {
var statusHandler status.AttachHandler
if tty != nil {
statusHandler = status.NewTTYAttachHandler(tty, fetcher)
Expand All @@ -75,11 +75,11 @@ func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.Fil
var metadataHandler metadata.AttachHandler
switch format.Type {
case option.FormatTypeText.Name:
metadataHandler = text.NewAttachHandler(printer)
metadataHandler = text.NewAttachHandler(printer, target)
case option.FormatTypeJSON.Name:
metadataHandler = json.NewAttachHandler(printer)
metadataHandler = json.NewAttachHandler(printer, target)
case option.FormatTypeGoTemplate.Name:
metadataHandler = template.NewAttachHandler(printer, format.Template)
metadataHandler = template.NewAttachHandler(printer, format.Template, target)
default:
return nil, nil, errors.UnsupportedFormatTypeError(format.Type)
}
Expand Down
5 changes: 3 additions & 2 deletions 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 All @@ -39,7 +40,7 @@ func TestNewPushHandler(t *testing.T) {
func TestNewAttachHandler(t *testing.T) {
mockFetcher := testutils.NewMockFetcher()
printer := output.NewPrinter(os.Stdout, os.Stderr)
_, _, err := NewAttachHandler(printer, option.Format{Type: option.FormatTypeText.Name}, os.Stdout, mockFetcher.Fetcher)
_, _, err := NewAttachHandler(printer, option.Format{Type: option.FormatTypeText.Name}, os.Stdout, mockFetcher.Fetcher, &option.Target{})
if err != nil {
t.Errorf("NewAttachHandler() error = %v, want nil", err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ type PushHandler interface {
OnCompleted(root ocispec.Descriptor) error
}

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

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

// DiscoverHandler handles metadata output for discover events.
Expand Down
18 changes: 13 additions & 5 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ import (

// AttachHandler handles json metadata output for attach events.
type AttachHandler struct {
out io.Writer
out io.Writer
target *option.Target
root ocispec.Descriptor
}

// NewAttachHandler creates a new handler for attach events.
func NewAttachHandler(out io.Writer) metadata.AttachHandler {
func NewAttachHandler(out io.Writer, target *option.Target) metadata.AttachHandler {
return &AttachHandler{
out: out,
out: out,
target: target,
}
}

// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, _ ocispec.Descriptor) {
ah.root = root
}

// 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))
func (ah *AttachHandler) OnCompleted() error {
return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.target.Path))
}
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,17 +29,25 @@ import (
type AttachHandler struct {
template string
out io.Writer
target *option.Target
root ocispec.Descriptor
}

// NewAttachHandler returns a new handler for attach metadata events.
func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler {
func NewAttachHandler(out io.Writer, template string, target *option.Target) metadata.AttachHandler {
return &AttachHandler{
out: out,
template: template,
target: target,
}
}

// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, _ ocispec.Descriptor) {
ah.root = root
}

// 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)
func (ah *AttachHandler) OnCompleted() error {
return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.target.Path), ah.template)
}
24 changes: 17 additions & 7 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,35 @@ import (
// AttachHandler handles text metadata output for attach events.
type AttachHandler struct {
printer *output.Printer
target *option.Target
root ocispec.Descriptor
subject ocispec.Descriptor
}

// NewAttachHandler returns a new handler for attach events.
func NewAttachHandler(printer *output.Printer) metadata.AttachHandler {
func NewAttachHandler(printer *output.Printer, target *option.Target) metadata.AttachHandler {
return &AttachHandler{
printer: printer,
target: target,
}
}

// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, subject ocispec.Descriptor) {
ah.root = root
ah.subject = subject
}

// 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)
func (ah *AttachHandler) OnCompleted() error {
digest := ah.subject.Digest.String()
if !strings.HasSuffix(ah.target.RawReference, digest) {
ah.target.RawReference = fmt.Sprintf("%s@%s", ah.target.Path, ah.subject.Digest)
}
err := ah.printer.Println("Attached to", opts.AnnotatedReference())
err := ah.printer.Println("Attached to", ah.target.AnnotatedReference())
if err != nil {
return err
}
err = ah.printer.Println("Digest:", root.Digest)
err = ah.printer.Println("Digest:", ah.root.Digest)
return err
}
17 changes: 10 additions & 7 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,25 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
if err != nil {
return fmt.Errorf("failed to resolve %s: %w", opts.Reference, err)
}
displayStatus, displayMetadata, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store)
statusHandler, metadataHandler, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store, &opts.Target)
if err != nil {
return err
}
descs, err := loadFiles(ctx, store, opts.Annotations, opts.FileRefs, displayStatus)
descs, err := loadFiles(ctx, store, opts.Annotations, opts.FileRefs, statusHandler)
if err != nil {
return err
}

// prepare push
dst, stopTrack, err := displayStatus.TrackTarget(dst)
dst, stopTrack, err := statusHandler.TrackTarget(dst)
if err != nil {
return err
}
graphCopyOptions := oras.DefaultCopyGraphOptions
graphCopyOptions.Concurrency = opts.concurrency
graphCopyOptions.OnCopySkipped = displayStatus.OnCopySkipped
graphCopyOptions.PreCopy = displayStatus.PreCopy
graphCopyOptions.PostCopy = displayStatus.PostCopy
graphCopyOptions.OnCopySkipped = statusHandler.OnCopySkipped
graphCopyOptions.PreCopy = statusHandler.PreCopy
graphCopyOptions.PostCopy = statusHandler.PostCopy

packOpts := oras.PackManifestOptions{
Subject: &subject,
Expand Down Expand Up @@ -210,7 +210,10 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
if err != nil {
return err
}
err = displayMetadata.OnCompleted(&opts.Target, root, subject)

metadataHandler.OnAttached(root, subject)

err = metadataHandler.OnCompleted()
if err != nil {
return err
}
Expand Down

0 comments on commit da87eb3

Please sign in to comment.