Skip to content

Commit 6e65f7c

Browse files
refactor: apply metadata render interface to oras pull command (#1587)
Signed-off-by: Xiaoxuan Wang <[email protected]>
1 parent 05e97ed commit 6e65f7c

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

cmd/oras/internal/display/metadata/interface.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ type ManifestFetchHandler interface {
5959

6060
// PullHandler handles metadata output for pull events.
6161
type PullHandler interface {
62+
Renderer
63+
6264
// OnLayerSkipped is called when a layer is skipped.
6365
OnLayerSkipped(ocispec.Descriptor) error
6466
// OnFilePulled is called after a file is pulled.
6567
OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error
66-
// OnCompleted is called when the pull cmd execution is completed.
67-
OnCompleted(opts *option.Target, desc ocispec.Descriptor) error
68+
// OnPulled is called when a pull operation completes.
69+
OnPulled(target *option.Target, desc ocispec.Descriptor)
6870
}
6971

7072
// TaggedHandler handles status output for tag command.

cmd/oras/internal/display/metadata/json/pull.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ type PullHandler struct {
3030
path string
3131
pulled model.Pulled
3232
out io.Writer
33-
}
34-
35-
// OnLayerSkipped implements metadata.PullHandler.
36-
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
37-
return nil
33+
root ocispec.Descriptor
3834
}
3935

4036
// NewPullHandler returns a new handler for Pull events.
@@ -45,12 +41,22 @@ func NewPullHandler(out io.Writer, path string) metadata.PullHandler {
4541
}
4642
}
4743

44+
// OnLayerSkipped implements metadata.PullHandler.
45+
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
46+
return nil
47+
}
48+
4849
// OnFilePulled implements metadata.PullHandler.
4950
func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error {
5051
return ph.pulled.Add(name, outputDir, desc, descPath)
5152
}
5253

53-
// OnCompleted implements metadata.PullHandler.
54-
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
55-
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
54+
// OnPulled implements metadata.PullHandler.
55+
func (ph *PullHandler) OnPulled(_ *option.Target, desc ocispec.Descriptor) {
56+
ph.root = desc
57+
}
58+
59+
// Render implements metadata.PullHandler.
60+
func (ph *PullHandler) Render() error {
61+
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+ph.root.Digest.String(), ph.pulled.Files()))
5662
}

cmd/oras/internal/display/metadata/template/pull.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,26 @@ type PullHandler struct {
3131
path string
3232
out io.Writer
3333
pulled model.Pulled
34+
root ocispec.Descriptor
3435
}
3536

36-
// OnCompleted implements metadata.PullHandler.
37-
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
38-
return output.ParseAndWrite(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()), ph.template)
37+
// NewPullHandler returns a new handler for pull events.
38+
func NewPullHandler(out io.Writer, path string, template string) metadata.PullHandler {
39+
return &PullHandler{
40+
path: path,
41+
template: template,
42+
out: out,
43+
}
44+
}
45+
46+
// OnPulled implements metadata.PullHandler.
47+
func (ph *PullHandler) OnPulled(_ *option.Target, desc ocispec.Descriptor) {
48+
ph.root = desc
49+
}
50+
51+
// Render implements metadata.PullHandler.
52+
func (ph *PullHandler) Render() error {
53+
return output.ParseAndWrite(ph.out, model.NewPull(ph.path+"@"+ph.root.Digest.String(), ph.pulled.Files()), ph.template)
3954
}
4055

4156
// OnFilePulled implements metadata.PullHandler.
@@ -47,12 +62,3 @@ func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.
4762
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error {
4863
return nil
4964
}
50-
51-
// NewPullHandler returns a new handler for pull events.
52-
func NewPullHandler(out io.Writer, path string, template string) metadata.PullHandler {
53-
return &PullHandler{
54-
path: path,
55-
template: template,
56-
out: out,
57-
}
58-
}

cmd/oras/internal/display/metadata/text/pull.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ import (
2828
type PullHandler struct {
2929
printer *output.Printer
3030
layerSkipped atomic.Bool
31+
target *option.Target
32+
root ocispec.Descriptor
3133
}
3234

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

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

55-
// NewPullHandler returns a new handler for Pull events.
56-
func NewPullHandler(printer *output.Printer) metadata.PullHandler {
57-
return &PullHandler{
58-
printer: printer,
52+
// OnPulled implements metadata.PullHandler.
53+
func (ph *PullHandler) OnPulled(target *option.Target, desc ocispec.Descriptor) {
54+
ph.target = target
55+
ph.root = desc
56+
}
57+
58+
// Render implements metadata.PullHandler.
59+
func (ph *PullHandler) Render() error {
60+
if ph.layerSkipped.Load() {
61+
_ = ph.printer.Printf("Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle)
62+
_ = ph.printer.Printf("Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", ph.target.RawReference)
63+
} else {
64+
_ = ph.printer.Println("Pulled", ph.target.AnnotatedReference())
65+
_ = ph.printer.Println("Digest:", ph.root.Digest)
5966
}
67+
return nil
6068
}

cmd/oras/root/pull.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func runPull(cmd *cobra.Command, opts *pullOptions) (pullError error) {
163163
}
164164
return err
165165
}
166-
167-
return metadataHandler.OnCompleted(&opts.Target, desc)
166+
metadataHandler.OnPulled(&opts.Target, desc)
167+
return metadataHandler.Render()
168168
}
169169

170170
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) {

0 commit comments

Comments
 (0)