From 165901193e64585543e34c80cdc3de1da21cc59b Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 9 Jan 2023 17:31:42 +0800 Subject: [PATCH] feat: show annotations in `oras discover` tree format output (#735) Signed-off-by: Billy Zha --- cmd/oras/discover.go | 16 +++++++++++++--- go.mod | 1 + go.sum | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/oras/discover.go b/cmd/oras/discover.go index 6a561ba72..5401520b9 100644 --- a/cmd/oras/discover.go +++ b/cmd/oras/discover.go @@ -22,6 +22,7 @@ import ( "os" "strings" + "gopkg.in/yaml.v3" "oras.land/oras-go/v2" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/errors" @@ -101,7 +102,7 @@ func runDiscover(opts discoverOptions) error { if opts.outputType == "tree" { root := tree.New(repo.Reference.String()) - err = fetchAllReferrers(ctx, repo, desc, opts.artifactType, root) + err = fetchAllReferrers(ctx, repo, desc, opts.artifactType, root, &opts) if err != nil { return err } @@ -141,7 +142,7 @@ func fetchReferrers(ctx context.Context, repo *remote.Repository, desc ocispec.D return results, nil } -func fetchAllReferrers(ctx context.Context, repo *remote.Repository, desc ocispec.Descriptor, artifactType string, node *tree.Node) error { +func fetchAllReferrers(ctx context.Context, repo *remote.Repository, desc ocispec.Descriptor, artifactType string, node *tree.Node, opts *discoverOptions) error { results, err := fetchReferrers(ctx, repo, desc, artifactType) if err != nil { return err @@ -150,6 +151,15 @@ func fetchAllReferrers(ctx context.Context, repo *remote.Repository, desc ocispe for _, r := range results { // Find all indirect referrers referrerNode := node.AddPath(r.ArtifactType, r.Digest) + if opts.Verbose { + for k, v := range r.Annotations { + bytes, err := yaml.Marshal(map[string]string{k: v}) + if err != nil { + return err + } + referrerNode.AddPathString(strings.TrimSpace(string(bytes))) + } + } err := fetchAllReferrers( ctx, repo, ocispec.Descriptor{ @@ -157,7 +167,7 @@ func fetchAllReferrers(ctx context.Context, repo *remote.Repository, desc ocispe Size: r.Size, MediaType: r.MediaType, }, - artifactType, referrerNode) + artifactType, referrerNode, opts) if err != nil { return err } diff --git a/go.mod b/go.mod index 4279c556f..72f96c1b7 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 + gopkg.in/yaml.v3 v3.0.1 oras.land/oras-go/v2 v2.0.0-rc.6 ) diff --git a/go.sum b/go.sum index 3089f6644..df3f86184 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,7 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=