Skip to content

Commit

Permalink
Merge branch 'main' into index-create-renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoxuan Wang committed Feb 6, 2025
2 parents 79933e9 + 6e65f7c commit a0c0994
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 53 deletions.
10 changes: 6 additions & 4 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ type AttachHandler interface {

// DiscoverHandler handles metadata output for discover events.
type DiscoverHandler interface {
Renderer

// MultiLevelSupported returns true if the handler supports multi-level
// discovery.
MultiLevelSupported() bool
// OnDiscovered is called after a referrer is discovered.
OnDiscovered(referrer, subject ocispec.Descriptor) error
// OnCompleted is called when referrer discovery is completed.
OnCompleted() error
}

// ManifestFetchHandler handles metadata output for manifest fetch events.
Expand All @@ -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
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
// Render implements metadata.DiscoverHandler.
func (h *discoverHandler) Render() error {
return output.PrintPrettyJSON(h.out, model.NewDiscover(h.path, h.referrers))
}
22 changes: 14 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,7 @@ type PullHandler struct {
path string
pulled model.Pulled
out io.Writer
}

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

// NewPullHandler returns a new handler for Pull events.
Expand All @@ -45,12 +41,22 @@ 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(_ *option.Target, desc ocispec.Descriptor) {
ph.root = desc
}

// Render implements metadata.PullHandler.
func (ph *PullHandler) Render() error {
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+ph.root.Digest.String(), ph.pulled.Files()))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/table/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
// Render implements metadata.DiscoverHandler.
func (h *discoverHandler) Render() error {
if n := len(h.referrers); n > 1 {
fmt.Fprintln(h.out, "Discovered", n, "artifacts referencing", h.rawReference)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/template/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
// Render implements metadata.DiscoverHandler.
func (h *discoverHandler) Render() error {
return output.ParseAndWrite(h.out, model.NewDiscover(h.path, h.referrers), h.template)
}
30 changes: 18 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,26 @@ type PullHandler struct {
path string
out io.Writer
pulled model.Pulled
root 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(_ *option.Target, desc ocispec.Descriptor) {
ph.root = desc
}

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

// OnFilePulled implements metadata.PullHandler.
Expand All @@ -47,12 +62,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
root 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.root = 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.root.Digest)
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/tree/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
// Render implements metadata.DiscoverHandler.
func (h *discoverHandler) Render() error {
return tree.NewPrinter(h.out).Print(h.root)
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ func (f *Format) SetTypes(defaultType *FormatType, otherTypes ...*FormatType) {

// ApplyFlags implements FlagProvider.ApplyFlag.
func (opts *Format) ApplyFlags(fs *pflag.FlagSet) {
buf := bytes.NewBufferString("[Experimental] Format output using a custom template:")
buf := bytes.NewBufferString("[Experimental] format output using a custom template:")
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)
for _, t := range opts.allowedTypes {
_, _ = fmt.Fprintf(w, "\n'%s':\t%s", t.Name, t.Usage)
}
_ = w.Flush()
// apply flags
fs.StringVar(&opts.FormatFlag, "format", opts.FormatFlag, buf.String())
fs.StringVar(&opts.Template, "template", "", "[Experimental] Template string used to format output")
fs.StringVar(&opts.Template, "template", "", "[Experimental] template string used to format output")
}

// Parse parses the input format flag.
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/internal/option/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (opts *Target) AnnotatedReference() string {
func (opts *Target) applyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description string) {
flagPrefix, notePrefix := applyPrefix(prefix, description)
fs.BoolVarP(&opts.IsOCILayout, flagPrefix+"oci-layout", "", false, "set "+notePrefix+"target as an OCI image layout")
fs.StringVar(&opts.Path, flagPrefix+"oci-layout-path", "", "set the path for the "+notePrefix+"OCI image layout target")
fs.StringVar(&opts.Path, flagPrefix+"oci-layout-path", "", "[Experimental] set the path for the "+notePrefix+"OCI image layout target")
}

// ApplyFlagsWithPrefix applies flags to a command flag set with a prefix string.
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func runDiscover(cmd *cobra.Command, opts *discoverOptions) error {
}
}
}
return handler.OnCompleted()
return handler.Render()
}

func fetchAllReferrers(ctx context.Context, repo oras.ReadOnlyGraphTarget, desc ocispec.Descriptor, artifactType string, handler metadata.DiscoverHandler) error {
Expand Down
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
2 changes: 1 addition & 1 deletion cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func runPush(cmd *cobra.Command, opts *pushOptions) error {
copyWithScopeHint := func(root ocispec.Descriptor) error {
// add both pull and push scope hints for dst repository
// to save potential push-scope token requests during copy
ctx = registryutil.WithScopeHint(ctx, dst, auth.ActionPull, auth.ActionPush)
ctx = registryutil.WithScopeHint(ctx, originalDst, auth.ActionPull, auth.ActionPush)

if tag := opts.Reference; tag == "" {
err = oras.CopyGraph(ctx, union, dst, root, copyOptions.CopyGraphOptions)
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/suite/command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ func cpTestRepo(text string) string {
var _ = Describe("ORAS beginners:", func() {
When("running cp command", func() {
It("should show help doc with feature flags", func() {
out := ORAS("cp", "--help").MatchKeyWords("Copy", ExampleDesc).Exec()
out := ORAS("cp", "--help").MatchKeyWords("Copy", ExampleDesc).Exec().Out
Expect(out).Should(gbytes.Say("--from-distribution-spec string\\s+%s", regexp.QuoteMeta(feature.Preview.Mark)))
Expect(out).Should(gbytes.Say("--from-oci-layout-path string\\s+%s", regexp.QuoteMeta(feature.Experimental.Mark)))
Expect(out).Should(gbytes.Say("-r, --recursive\\s+%s", regexp.QuoteMeta(feature.Preview.Mark)))
Expect(out).Should(gbytes.Say("--to-distribution-spec string\\s+%s", regexp.QuoteMeta(feature.Preview.Mark)))
Expect(out).Should(gbytes.Say("--to-oci-layout-path string\\s+%s", regexp.QuoteMeta(feature.Experimental.Mark)))
})

It("should not show --verbose in help doc", func() {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/suite/command/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var _ = Describe("ORAS beginners:", func() {
It("should show help description with feature flags", func() {
out := ORAS("pull", "--help").MatchKeyWords(ExampleDesc).Exec().Out
gomega.Expect(out).Should(gbytes.Say("--include-subject\\s+%s", regexp.QuoteMeta(feature.Preview.Mark)))
gomega.Expect(out).Should(gbytes.Say("--oci-layout-path string\\s+%s", regexp.QuoteMeta(feature.Experimental.Mark)))
})

It("should not show --verbose in help doc", func() {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/suite/command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var _ = Describe("ORAS beginners:", func() {
It("should show help description with feature flags", func() {
out := ORAS("push", "--help").MatchKeyWords(ExampleDesc).Exec().Out
gomega.Expect(out).Should(gbytes.Say("--image-spec string\\s+%s", regexp.QuoteMeta(feature.Preview.Mark)))
gomega.Expect(out).Should(gbytes.Say("--oci-layout-path string\\s+%s", regexp.QuoteMeta(feature.Experimental.Mark)))
})

It("should not show --verbose in help doc", func() {
Expand Down

0 comments on commit a0c0994

Please sign in to comment.