Skip to content

Commit

Permalink
feat: show annotations in oras discover tree format output (#735)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Jan 9, 2023
1 parent cdeca2f commit 1659011
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions cmd/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -150,14 +151,23 @@ 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{
Digest: r.Digest,
Size: r.Size,
MediaType: r.MediaType,
},
artifactType, referrerNode)
artifactType, referrerNode, opts)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 1659011

Please sign in to comment.