From 0b2f43af49598b063ca539ac1a1b876392a8097c Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 16 Dec 2024 10:25:24 +0800 Subject: [PATCH] refactor: apply metadata render interface to oras pull command Signed-off-by: Xiaoxuan Wang --- .../internal/display/metadata/interface.go | 6 ++-- .../internal/display/metadata/json/pull.go | 24 ++++++++----- .../display/metadata/template/pull.go | 32 ++++++++++------- .../internal/display/metadata/text/pull.go | 34 ++++++++++++------- cmd/oras/root/pull.go | 4 +-- 5 files changed, 63 insertions(+), 37 deletions(-) diff --git a/cmd/oras/internal/display/metadata/interface.go b/cmd/oras/internal/display/metadata/interface.go index 145a65d0a..60bfef2a8 100644 --- a/cmd/oras/internal/display/metadata/interface.go +++ b/cmd/oras/internal/display/metadata/interface.go @@ -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. diff --git a/cmd/oras/internal/display/metadata/json/pull.go b/cmd/oras/internal/display/metadata/json/pull.go index 8477286b2..0631da61f 100644 --- a/cmd/oras/internal/display/metadata/json/pull.go +++ b/cmd/oras/internal/display/metadata/json/pull.go @@ -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. @@ -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())) } diff --git a/cmd/oras/internal/display/metadata/template/pull.go b/cmd/oras/internal/display/metadata/template/pull.go index d66c2986c..8f4dfc30c 100644 --- a/cmd/oras/internal/display/metadata/template/pull.go +++ b/cmd/oras/internal/display/metadata/template/pull.go @@ -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. @@ -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, - } -} diff --git a/cmd/oras/internal/display/metadata/text/pull.go b/cmd/oras/internal/display/metadata/text/pull.go index 1b94de7bd..bd01d9293 100644 --- a/cmd/oras/internal/display/metadata/text/pull.go +++ b/cmd/oras/internal/display/metadata/text/pull.go @@ -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 ' 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 { @@ -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 ' 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 } diff --git a/cmd/oras/root/pull.go b/cmd/oras/root/pull.go index 60bd60a32..7c45ba4a6 100644 --- a/cmd/oras/root/pull.go +++ b/cmd/oras/root/pull.go @@ -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) {