Skip to content

Commit

Permalink
refactor printer
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 07d4a8e commit a879b17
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 101 deletions.
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewPullHandler(format string, path string, tty *os.File, out io.Writer, ver
var metadataHandler metadata.PullHandler
switch format {
case "":
metadataHandler = text.NewPullHandler(out)
metadataHandler = text.NewPullHandler()
case "json":
metadataHandler = json.NewPullHandler(path)
default:
Expand Down
15 changes: 5 additions & 10 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -27,20 +25,17 @@ import (

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

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

// NewAttachHandler creates a new handler for attach events.
func NewAttachHandler() metadata.AttachHandler {
return &AttachHandler{}
return &AttachHandler{
Printer: view.NewPrinter(),
}
}

// OnCompleted is called when the attach command is completed.
func (a *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return a.printer.PrintJSON(model.NewPush(root, opts.Path))
return a.PrintJSON(model.NewPush(root, opts.Path))
}
18 changes: 6 additions & 12 deletions cmd/oras/internal/display/metadata/json/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -27,20 +25,16 @@ import (

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

// WithOutput implements metadata.Outputer.
func (ph *PullHandler) WithOutput(out io.Writer) {
ph.printer = view.NewPrinter(out)
path string
pulled model.Pulled
view.Printer
}

// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(path string) metadata.PullHandler {
return &PullHandler{
path: path,
Printer: view.NewPrinter(),
path: path,
}
}

Expand All @@ -51,5 +45,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 ph.printer.PrintJSON(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files))
return ph.PrintJSON(model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files))
}
17 changes: 6 additions & 11 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -27,18 +25,15 @@ import (

// PushHandler handles JSON metadata output for push events.
type PushHandler struct {
path string
printer view.Printer
path string
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)
return &PushHandler{
Printer: view.NewPrinter(),
}
}

// OnCopied is called after files are copied.
Expand All @@ -49,5 +44,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 ph.printer.PrintJSON(model.NewPush(root, ph.path))
return ph.PrintJSON(model.NewPush(root, ph.path))
}
16 changes: 6 additions & 10 deletions cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -28,20 +26,18 @@ import (
// 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)
view.Printer
}

// NewAttachHandler returns a new handler for attach metadata events.
func NewAttachHandler(template string) metadata.AttachHandler {
return &AttachHandler{template: template}
return &AttachHandler{
template: template,
Printer: view.NewPrinter(),
}
}

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return ah.printer.ParseAndWrite(model.NewPush(root, opts.Path), ah.template)
return ah.ParseAndWrite(model.NewPush(root, opts.Path), ah.template)
}
14 changes: 4 additions & 10 deletions cmd/oras/internal/display/metadata/template/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -29,18 +27,13 @@ import (
type PullHandler struct {
template string
path string
printer view.Printer
pulled model.Pulled
}

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

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

// OnFilePulled implements metadata.PullHandler.
Expand All @@ -53,5 +46,6 @@ func NewPullHandler(path string, template string) metadata.PullHandler {
return &PullHandler{
path: path,
template: template,
Printer: view.NewPrinter(),
}
}
16 changes: 6 additions & 10 deletions cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ 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"
Expand All @@ -29,17 +27,15 @@ import (
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)
view.Printer
}

// NewPushHandler returns a new handler for push events.
func NewPushHandler(template string) metadata.PushHandler {
return &PushHandler{template: template}
return &PushHandler{
template: template,
Printer: view.NewPrinter(),
}
}

// OnStarted is called after files are copied.
Expand All @@ -50,5 +46,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 ph.printer.ParseAndWrite(model.NewPush(root, ph.path), ph.template)
return ph.ParseAndWrite(model.NewPush(root, ph.path), ph.template)
}
16 changes: 6 additions & 10 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package text

import (
"fmt"
"io"
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -28,17 +27,14 @@ import (

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

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

// NewAttachHandler returns a new handler for attach events.
func NewAttachHandler() metadata.AttachHandler {
return &AttachHandler{}
return &AttachHandler{
Printer: view.NewPrinter(),
}
}

// OnCompleted is called when the attach command is completed.
Expand All @@ -47,10 +43,10 @@ func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.
if !strings.HasSuffix(opts.RawReference, digest) {
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)
}
_, err := ah.printer.Println("Attached to", opts.AnnotatedReference())
_, err := ah.Println("Attached to", opts.AnnotatedReference())
if err != nil {
return err
}
_, err = ah.printer.Println("Digest:", root.Digest)
_, err = ah.Println("Digest:", root.Digest)
return err
}
23 changes: 9 additions & 14 deletions cmd/oras/internal/display/metadata/text/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ limitations under the License.
package text

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/view"
Expand All @@ -26,22 +24,17 @@ import (

// PullHandler handles text metadata output for pull events.
type PullHandler struct {
printer view.Printer
}

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

// OnCompleted implements metadata.PullHandler.
func (p *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor, layerSkipped bool) error {
if layerSkipped {
_, _ = p.printer.Printf("Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle)
_, _ = p.printer.Printf("Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", opts.RawReference)
_, _ = p.Printf("Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle)
_, _ = p.Printf("Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", opts.RawReference)
} else {
_, _ = p.printer.Println("Pulled", opts.AnnotatedReference())
_, _ = p.printer.Println("Digest:", desc.Digest)
_, _ = p.Println("Pulled", opts.AnnotatedReference())
_, _ = p.Println("Digest:", desc.Digest)
}
return nil
}
Expand All @@ -50,6 +43,8 @@ func (p *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.D
}

// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(out io.Writer) metadata.PullHandler {
return &PullHandler{}
func NewPullHandler() metadata.PullHandler {
return &PullHandler{
Printer: view.NewPrinter(),
}
}
17 changes: 6 additions & 11 deletions cmd/oras/internal/display/metadata/text/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ limitations under the License.
package text

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/view"
Expand All @@ -26,27 +24,24 @@ import (

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

// NewPushHandler returns 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)
return &PushHandler{
Printer: view.NewPrinter(),
}
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
_, err := ph.printer.Println("Pushed", opts.AnnotatedReference())
_, err := ph.Println("Pushed", opts.AnnotatedReference())
return err
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
_, err := ph.printer.Println("Digest:", root.Digest)
_, err := ph.Println("Digest:", root.Digest)
return err
}
Loading

0 comments on commit a879b17

Please sign in to comment.