Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply metadata render interface to oras pull command #1587

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ type ManifestFetchHandler interface {

// PullHandler handles metadata output for pull events.
type PullHandler interface {
Renderer

// OnLayerSkipped is called when a layer is skipped.
OnLayerSkipped(ocispec.Descriptor) error
// OnFilePulled is called after a file is pulled.
OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error
// OnCompleted is called when the pull cmd execution is completed.
OnCompleted(opts *option.Target, desc ocispec.Descriptor) error
// OnPulled is called when a pull operation completes.
OnPulled(target *option.Target, desc ocispec.Descriptor)
}

// TaggedHandler handles status output for tag command.
Expand Down
24 changes: 16 additions & 8 deletions cmd/oras/internal/display/metadata/json/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ type PullHandler struct {
path string
pulled model.Pulled
out io.Writer
}

// OnLayerSkipped implements metadata.PullHandler.
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
return nil
target *option.Target
desc ocispec.Descriptor
}

// NewPullHandler returns a new handler for Pull events.
Expand All @@ -45,12 +42,23 @@ func NewPullHandler(out io.Writer, path string) metadata.PullHandler {
}
}

// OnLayerSkipped implements metadata.PullHandler.
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
return nil
}

// OnFilePulled implements metadata.PullHandler.
func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error {
return ph.pulled.Add(name, outputDir, desc, descPath)
}

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
// OnPulled implements metadata.PullHandler.
func (ph *PullHandler) OnPulled(target *option.Target, desc ocispec.Descriptor) {
ph.target = target
ph.desc = desc
}

// Render implements metadata.PullHandler.
func (ph *PullHandler) Render() error {
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+ph.desc.Digest.String(), ph.pulled.Files()))
}
32 changes: 20 additions & 12 deletions cmd/oras/internal/display/metadata/template/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ type PullHandler struct {
path string
out io.Writer
pulled model.Pulled
target *option.Target
desc ocispec.Descriptor
}

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
return output.ParseAndWrite(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()), ph.template)
// NewPullHandler returns a new handler for pull events.
func NewPullHandler(out io.Writer, path string, template string) metadata.PullHandler {
return &PullHandler{
path: path,
template: template,
out: out,
}
}

// OnPulled implements metadata.PullHandler.
func (ph *PullHandler) OnPulled(target *option.Target, desc ocispec.Descriptor) {
ph.target = target
ph.desc = desc
}

// Render implements metadata.PullHandler.
func (ph *PullHandler) Render() error {
return output.ParseAndWrite(ph.out, model.NewPull(ph.path+"@"+ph.desc.Digest.String(), ph.pulled.Files()), ph.template)
}

// OnFilePulled implements metadata.PullHandler.
Expand All @@ -47,12 +64,3 @@ func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
return nil
}

// NewPullHandler returns a new handler for pull events.
func NewPullHandler(out io.Writer, path string, template string) metadata.PullHandler {
return &PullHandler{
path: path,
template: template,
out: out,
}
}
34 changes: 21 additions & 13 deletions cmd/oras/internal/display/metadata/text/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ import (
type PullHandler struct {
printer *output.Printer
layerSkipped atomic.Bool
target *option.Target
desc ocispec.Descriptor
}

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
if ph.layerSkipped.Load() {
_ = ph.printer.Printf("Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle)
_ = ph.printer.Printf("Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", opts.RawReference)
} else {
_ = ph.printer.Println("Pulled", opts.AnnotatedReference())
_ = ph.printer.Println("Digest:", desc.Digest)
// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(printer *output.Printer) metadata.PullHandler {
return &PullHandler{
printer: printer,
}
return nil
}

func (ph *PullHandler) OnFilePulled(_ string, _ string, _ ocispec.Descriptor, _ string) error {
Expand All @@ -52,9 +49,20 @@ func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
return nil
}

// NewPullHandler returns a new handler for Pull events.
func NewPullHandler(printer *output.Printer) metadata.PullHandler {
return &PullHandler{
printer: printer,
// OnPulled implements metadata.PullHandler.
func (ph *PullHandler) OnPulled(target *option.Target, desc ocispec.Descriptor) {
ph.target = target
ph.desc = desc
}

// Render implements metadata.PullHandler.
func (ph *PullHandler) Render() error {
if ph.layerSkipped.Load() {
_ = ph.printer.Printf("Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle)
_ = ph.printer.Printf("Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", ph.target.RawReference)
} else {
_ = ph.printer.Println("Pulled", ph.target.AnnotatedReference())
_ = ph.printer.Println("Digest:", ph.desc.Digest)
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/oras/root/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func runPull(cmd *cobra.Command, opts *pullOptions) (pullError error) {
}
return err
}

return metadataHandler.OnCompleted(&opts.Target, desc)
metadataHandler.OnPulled(&opts.Target, desc)
return metadataHandler.Render()
}

func doPull(ctx context.Context, src oras.ReadOnlyTarget, dst oras.GraphTarget, opts oras.CopyOptions, metadataHandler metadata.PullHandler, statusHandler status.PullHandler, po *pullOptions) (ocispec.Descriptor, error) {
Expand Down
Loading