Skip to content

Commit

Permalink
resolved comments
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 da87eb3 commit f05cc66
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 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, target *option.Target) (status.AttachHandler, metadata.AttachHandler, error) {
func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.File, fetcher fetcher.Fetcher) (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, target)
metadataHandler = text.NewAttachHandler(printer)
case option.FormatTypeJSON.Name:
metadataHandler = json.NewAttachHandler(printer, target)
metadataHandler = json.NewAttachHandler(printer)
case option.FormatTypeGoTemplate.Name:
metadataHandler = template.NewAttachHandler(printer, format.Template, target)
metadataHandler = template.NewAttachHandler(printer, format.Template)
default:
return nil, nil, errors.UnsupportedFormatTypeError(format.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,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, &option.Target{})
_, _, err := NewAttachHandler(printer, option.Format{Type: option.FormatTypeText.Name}, os.Stdout, mockFetcher.Fetcher)
if err != nil {
t.Errorf("NewAttachHandler() error = %v, want nil", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type PushHandler interface {

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

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

Expand Down
12 changes: 6 additions & 6 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ type AttachHandler struct {
}

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

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

// OnCompleted is called when the attach command is completed.
func (ah *AttachHandler) OnCompleted() error {
// Render is called when the attach command is completed.
func (ah *AttachHandler) Render() error {
return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.target.Path))
}
10 changes: 5 additions & 5 deletions cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ type AttachHandler struct {
}

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

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

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted() error {
// Render formats the metadata of attach command.
func (ah *AttachHandler) Render() error {
return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.target.Path), ah.template)
}
10 changes: 5 additions & 5 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ type AttachHandler struct {
}

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

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

// OnCompleted is called when the attach command is complete.
func (ah *AttachHandler) OnCompleted() error {
// Render is called when the attach command is complete.
func (ah *AttachHandler) Render() 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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
if err != nil {
return fmt.Errorf("failed to resolve %s: %w", opts.Reference, err)
}
statusHandler, metadataHandler, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store, &opts.Target)
statusHandler, metadataHandler, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store)
if err != nil {
return err
}
Expand Down Expand Up @@ -211,9 +211,9 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
return err
}

metadataHandler.OnAttached(root, subject)
metadataHandler.OnAttached(&opts.Target, root, subject)

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

0 comments on commit f05cc66

Please sign in to comment.