Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging artifacts Branch: Add discover comand #419

Merged
merged 32 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
45a3de2
Add discover command
qweeah Jun 27, 2022
a625b29
Add discover command
qweeah Jun 27, 2022
146353b
Remove containerd dependency
qweeah Jun 27, 2022
9d85d93
Update module info
qweeah Jun 27, 2022
63e962e
Add command line examples
qweeah Jun 27, 2022
a8e1984
Code clean
qweeah Jun 27, 2022
ffcee8c
Code clean
qweeah Jun 27, 2022
829daf8
Add discover command
qweeah Jun 27, 2022
6c06058
Add discover command
qweeah Jun 27, 2022
31c9903
Remove containerd dependency
qweeah Jun 27, 2022
eb5ac00
Update module info
qweeah Jun 27, 2022
0a7fb5c
Add command line examples
qweeah Jun 27, 2022
d3921bb
Code clean
qweeah Jun 27, 2022
91505b0
Code clean
qweeah Jun 27, 2022
b3b47d2
Add exprimental command logging
qweeah Jun 29, 2022
d1ee21a
Merge branch 'artifacts-discover' of https://github.com/qweeah/oras i…
qweeah Jun 29, 2022
120b776
Update module
qweeah Jun 29, 2022
259b811
Merge remote-tracking branch 'origin_src/main' into artifacts-discover
qweeah Jun 29, 2022
a646450
Merge branch 'artifacts-discover' of https://github.com/qweeah/oras i…
qweeah Jun 29, 2022
35a777a
Resolve partial comments
qweeah Jun 30, 2022
3c961df
Use oras-go to do artifact filtering
qweeah Jun 30, 2022
69aefd7
Refactoring
qweeah Jun 30, 2022
f7924f6
Add go.sum
qweeah Jun 30, 2022
5834cb2
Resolve all comments
qweeah Jun 30, 2022
1aa0ee5
Support pagination
qweeah Jun 30, 2022
681c5e1
Resolve comments
qweeah Jun 30, 2022
e098eff
Reword
qweeah Jun 30, 2022
7fb6be4
Resolve comments
qweeah Jul 1, 2022
3ebdbca
Resolve more comments
qweeah Jul 1, 2022
5385549
Add default prompt
qweeah Jul 1, 2022
1530d8a
Add preview warning
qweeah Jul 1, 2022
fc8a6a0
Add latest tag support
qweeah Jul 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions cmd/oras/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package main
qweeah marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/option"

"github.com/need-being/go-tree"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
"github.com/spf13/cobra"
)

type discoverOptions struct {
option.Common
option.Remote

targetRef string
artifactType string
outputType string
}

func discoverCmd() *cobra.Command {
var opts discoverOptions
cmd := &cobra.Command{
Use: "discover [options] <name:tag|name@digest>",
Short: "discover artifacts from remote registry",
Long: `discover artifacts from remote registry

Example - Discover all the artifacts linked with the specified reference:
qweeah marked this conversation as resolved.
Show resolved Hide resolved
oras discover localhost:5000/hello

Example - Discover all the artifacts linked with the specified reference in a tree view:
oras discover localhost:5000/hello -o tree

Example - Discover artifacts of type test-artifact test-artifact linked with the specified reference:
oras discover --artifact test-artifact localhost:5000/hello
`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
fmt.Println("Command discover is experimental and under development.")
qweeah marked this conversation as resolved.
Show resolved Hide resolved
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
return runDiscover(opts)
},
}

cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type")
qweeah marked this conversation as resolved.
Show resolved Hide resolved
qweeah marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringVarP(&opts.outputType, "output", "o", "table", fmt.Sprintf("Format in which to display references (%s, %s, or %s). tree format will show all references including nested", "table", "json", "tree"))
qweeah marked this conversation as resolved.
Show resolved Hide resolved
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func runDiscover(opts discoverOptions) error {
ctx, _ := opts.SetLoggerLevel()
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
}

// discover artifacts
root := tree.New(opts.targetRef)
desc, err := repo.Resolve(ctx, repo.Reference.Reference)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
desc, refs, err := getAllReferences(ctx, repo, desc, opts.artifactType, root, opts.outputType == "tree")
if err != nil {
return err
}

switch opts.outputType {
case "tree":
tree.Print(root)
case "json":
printDiscoveredReferencesJSON(desc, *refs)
default:
fmt.Println("Discovered", len(*refs), "artifacts referencing", opts.targetRef)
fmt.Println("Digest:", desc.Digest)

if len(*refs) != 0 {
fmt.Println()
printDiscoveredReferencesTable(*refs, opts.Verbose)
}
}
return nil
}

func getAllReferences(ctx context.Context, repo *remote.Repository, desc ocispec.Descriptor, artifactType string, node *tree.Node, queryGraph bool) (ocispec.Descriptor, *[]artifactspec.Descriptor, error) {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
qweeah marked this conversation as resolved.
Show resolved Hide resolved
var results []artifactspec.Descriptor
err := repo.Referrers(ctx, desc, func(referrers []artifactspec.Descriptor) error {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
for _, r := range referrers {
if artifactType == "" || artifactType == r.ArtifactType {
results = append(results, r)
if !queryGraph {
continue
}
// Find all referrers
referrerNode := node.AddPath(r.ArtifactType, r.Digest)
_, nestedReferrers, err := getAllReferences(
ctx, repo,
ocispec.Descriptor{
Digest: r.Digest,
Size: r.Size,
MediaType: r.MediaType,
},
artifactType, referrerNode, queryGraph)
if err != nil {
return err
}
results = append(results, *nestedReferrers...)
}
}
return nil
})
if err != nil {
return ocispec.Descriptor{}, nil, err
}
return desc, &results, nil
}

func printDiscoveredReferencesTable(refs []artifactspec.Descriptor, verbose bool) {
typeNameTitle := "Artifact Type"
typeNameLength := len(typeNameTitle)
for _, ref := range refs {
if length := len(ref.ArtifactType); length > typeNameLength {
typeNameLength = length
}
}

print := func(key string, value interface{}) {
fmt.Println(key, strings.Repeat(" ", typeNameLength-len(key)+1), value)
}

print(typeNameTitle, "Digest")
for _, ref := range refs {
print(ref.ArtifactType, ref.Digest)
if verbose {
printJSON(ref)
}
}
}

func printDiscoveredReferencesJSON(desc ocispec.Descriptor, refs []artifactspec.Descriptor) {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
qweeah marked this conversation as resolved.
Show resolved Hide resolved
type reference struct {
Digest digest.Digest `json:"digest"`
Artifact string `json:"artifactType"`
}
output := struct {
Digest digest.Digest `json:"digest"`
References []reference `json:"references"`
}{
Digest: desc.Digest,
References: make([]reference, len(refs)),
}

for i, ref := range refs {
output.References[i] = reference{
Digest: ref.Digest,
Artifact: ref.ArtifactType,
}
}

printJSON(output)
}

func printJSON(object interface{}) {
encoder := json.NewEncoder(os.Stdout)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
encoder.Encode(object)
}
qweeah marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion cmd/oras/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
Use: "oras [command]",
SilenceUsage: true,
}
cmd.AddCommand(pullCmd(), pushCmd(), loginCmd(), logoutCmd(), versionCmd())
cmd.AddCommand(pullCmd(), pushCmd(), loginCmd(), logoutCmd(), versionCmd(), discoverCmd())
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.18
require (
github.com/docker/cli v20.10.17+incompatible
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/need-being/go-tree v0.1.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799
github.com/oras-project/artifacts-spec v1.0.0-rc.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
Expand All @@ -18,7 +20,6 @@ require (
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/oras-project/artifacts-spec v1.0.0-draft.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw=
github.com/need-being/go-tree v0.1.0 h1:blQrtD006cFm97UDeMUfixwPc9o06A6c+uLaUskdNNw=
github.com/need-being/go-tree v0.1.0/go.mod h1:UOHUchuOm+lxM+EtvQ9h/IO88hK/ke7FHai4oGhhEoI=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 h1:rc3tiVYb5z54aKaDfakKn0dDjIyPpTtszkjuMzyt7ec=
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/oras-project/artifacts-spec v1.0.0-draft.1.1 h1:2YMUDyDH0glYA4gNG/zEg9HNVzgGX8kr/NBLR9AQkLQ=
github.com/oras-project/artifacts-spec v1.0.0-draft.1.1/go.mod h1:Xch2aLzSwtkhbFFN6LUzTfLtukYvMMdXJ4oZ8O7BOdc=
github.com/oras-project/artifacts-spec v1.0.0-rc.1 h1:bCHf9mPbrgiNwQFyVzBX79BYZVAl0OUrmvICZOCOwts=
github.com/oras-project/artifacts-spec v1.0.0-rc.1/go.mod h1:Xch2aLzSwtkhbFFN6LUzTfLtukYvMMdXJ4oZ8O7BOdc=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down