Skip to content

Commit

Permalink
add printer handle output
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Mar 25, 2024
1 parent 8f8f9a4 commit 07d4a8e
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 113 deletions.
13 changes: 7 additions & 6 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// NewPushHandler returns status and metadata handlers for push command.
func NewPushHandler(format string, tty *os.File, verbose bool) (status.PushHandler, metadata.PushHandler) {
func NewPushHandler(format string, tty *os.File, out io.Writer, verbose bool) (status.PushHandler, metadata.PushHandler) {
var statusHandler status.PushHandler
if tty != nil {
statusHandler = status.NewTTYPushHandler(tty)
Expand All @@ -46,12 +46,12 @@ func NewPushHandler(format string, tty *os.File, verbose bool) (status.PushHandl
default:
metadataHandler = template.NewPushHandler(format)
}

metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}

// NewAttachHandler returns status and metadata handlers for attach command.
func NewAttachHandler(format string, tty *os.File, verbose bool) (status.AttachHandler, metadata.AttachHandler) {
func NewAttachHandler(format string, tty *os.File, out io.Writer, verbose bool) (status.AttachHandler, metadata.AttachHandler) {
var statusHandler status.AttachHandler
if tty != nil {
statusHandler = status.NewTTYAttachHandler(tty)
Expand All @@ -70,7 +70,7 @@ func NewAttachHandler(format string, tty *os.File, verbose bool) (status.AttachH
default:
metadataHandler = template.NewAttachHandler(format)
}

metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}

Expand All @@ -90,9 +90,10 @@ func NewPullHandler(format string, path string, tty *os.File, out io.Writer, ver
case "":
metadataHandler = text.NewPullHandler(out)
case "json":
metadataHandler = json.NewPullHandler(path, out)
metadataHandler = json.NewPullHandler(path)
default:
metadataHandler = template.NewPullHandler(path, format, out)
metadataHandler = template.NewPullHandler(path, format)
}
metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}
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 @@ -16,19 +16,28 @@ limitations under the License.
package metadata

import (
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/option"
)

type Outputable interface {
// WithOutput resets output of the handler
WithOutput(out io.Writer)
}

// PushHandler handles metadata output for push events.
type PushHandler interface {
OnCopied(opts *option.Target) error
OnCompleted(root ocispec.Descriptor) error
Outputable
}

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

// PullHandler handles metadata output for attach events.
Expand All @@ -37,4 +46,5 @@ type PullHandler interface {
OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string)
// OnCompleted is called when the pull cmd execution is completed.
OnCompleted(opts *option.Target, desc ocispec.Descriptor, layerSkipped bool) error
Outputable
}
18 changes: 14 additions & 4 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,31 @@ limitations under the License.
package json

import (
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// AttachHandler handles json metadata output for attach events.
type AttachHandler struct{}
type AttachHandler struct {
printer view.Printer
}

// WithOutput implements metadata.AttachHandler.
func (a *AttachHandler) WithOutput(out io.Writer) {
a.printer = view.NewPrinter(out)
}

// NewAttachHandler creates a new handler for attach events.
func NewAttachHandler() metadata.AttachHandler {
return AttachHandler{}
return &AttachHandler{}
}

// OnCompleted is called when the attach command is completed.
func (AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return printJSON(model.NewPush(root, opts.Path))
func (a *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return a.printer.PrintJSON(model.NewPush(root, opts.Path))
}
32 changes: 0 additions & 32 deletions cmd/oras/internal/display/metadata/json/json.go

This file was deleted.

17 changes: 11 additions & 6 deletions cmd/oras/internal/display/metadata/json/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PullHandler handles JSON metadata output for pull events.
type PullHandler struct {
path string
out io.Writer
pulled model.Pulled
path string
printer view.Printer
pulled model.Pulled
}

// WithOutput implements metadata.Outputer.
func (ph *PullHandler) WithOutput(out io.Writer) {
ph.printer = view.NewPrinter(out)
}

// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(path string, out io.Writer) metadata.PullHandler {
func NewPullHandler(path string) metadata.PullHandler {
return &PullHandler{
path: path,
out: out,
}
}

Expand All @@ -46,5 +51,5 @@ func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor, _ bool) error {
return printJSON(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files))
return ph.printer.PrintJSON(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files))
}
13 changes: 11 additions & 2 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ limitations under the License.
package json

import (
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles JSON metadata output for push events.
type PushHandler struct {
path string
path string
printer view.Printer
}

// NewPushHandler creates a new handler for push events.
func NewPushHandler() metadata.PushHandler {
return &PushHandler{}
}

// WithOutput implements metadata.Outputer.
func (ph *PushHandler) WithOutput(out io.Writer) {
ph.printer = view.NewPrinter(out)
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
ph.path = opts.Path
Expand All @@ -40,5 +49,5 @@ func (ph *PushHandler) OnCopied(opts *option.Target) error {

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return printJSON(model.NewPush(root, ph.path))
return ph.printer.PrintJSON(model.NewPush(root, ph.path))
}
11 changes: 10 additions & 1 deletion cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ limitations under the License.
package template

import (
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// AttachHandler handles go-template metadata output for attach events.
type AttachHandler struct {
template string
printer view.Printer
}

// WithOutput implements metadata.AttachHandler.
func (a *AttachHandler) WithOutput(out io.Writer) {
a.printer = view.NewPrinter(out)
}

// NewAttachHandler returns a new handler for attach metadata events.
Expand All @@ -34,5 +43,5 @@ func NewAttachHandler(template string) metadata.AttachHandler {

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return parseAndWrite(model.NewPush(root, opts.Path), ah.template)
return ah.printer.ParseAndWrite(model.NewPush(root, opts.Path), ah.template)
}
13 changes: 9 additions & 4 deletions cmd/oras/internal/display/metadata/template/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PullHandler handles text metadata output for pull events.
type PullHandler struct {
template string
path string
out io.Writer
printer view.Printer
pulled model.Pulled
}

// WithOutput implements metadata.Outputer.
func (ph *PullHandler) WithOutput(out io.Writer) {
ph.printer = view.NewPrinter(out)
}

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor, _ bool) error {
return parseAndWrite(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files), ph.template)
return ph.printer.ParseAndWrite(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files), ph.template)
}

// OnFilePulled implements metadata.PullHandler.
Expand All @@ -43,10 +49,9 @@ func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.
}

// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(path string, template string, out io.Writer) metadata.PullHandler {
func NewPullHandler(path string, template string) metadata.PullHandler {
return &PullHandler{
path: path,
template: template,
out: out,
}
}
11 changes: 10 additions & 1 deletion cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ limitations under the License.
package template

import (
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles go-template metadata output for push events.
type PushHandler struct {
template string
path string
printer view.Printer
}

// WithOutput implements metadata.Outputer.
func (ph *PushHandler) WithOutput(out io.Writer) {
ph.printer = view.NewPrinter(out)
}

// NewPushHandler returns a new handler for push events.
Expand All @@ -41,5 +50,5 @@ func (ph *PushHandler) OnCopied(opts *option.Target) error {

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return parseAndWrite(model.NewPush(root, ph.path), ph.template)
return ph.printer.ParseAndWrite(model.NewPush(root, ph.path), ph.template)
}
36 changes: 0 additions & 36 deletions cmd/oras/internal/display/metadata/template/template.go

This file was deleted.

Loading

0 comments on commit 07d4a8e

Please sign in to comment.