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

feat: add resolve command #1187

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions cmd/oras/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func New() *cobra.Command {
logoutCmd(),
versionCmd(),
discoverCmd(),
resolveCmd(),
copyCmd(),
tagCmd(),
attachCmd(),
Expand Down
86 changes: 86 additions & 0 deletions cmd/oras/root/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package root

import (
"context"
"fmt"

"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/option"
)

type resolveOptions struct {
option.Common
option.Platform
option.Target

FullRef bool
}

func resolveCmd() *cobra.Command {
var opts resolveOptions

cmd := &cobra.Command{
Use: "resolve [flags] <name>{:<tag>|@<digest>}",
Short: "[Experimental] Resolves digest of the target artifact",
Long: `[Experimental] Resolves digest of the target artifact

Example - Resolve digest of the target artifact:
oras resolve localhost:5000/hello-world:v1
`,
Args: cobra.ExactArgs(1),
Aliases: []string{"digest"},
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.RawReference = args[0]
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runResolve(cmd.Context(), opts)
},
}

cmd.Flags().BoolVarP(&opts.FullRef, "full-reference", "l", false, "print the full artifact reference with digest")
qweeah marked this conversation as resolved.
Show resolved Hide resolved
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func runResolve(ctx context.Context, opts resolveOptions) error {
ctx, logger := opts.WithContext(ctx)
repo, err := opts.NewReadonlyTarget(ctx, opts.Common, logger)
if err != nil {
return err
}
if err := opts.EnsureReferenceNotEmpty(); err != nil {
return err
}
resolveOpts := oras.DefaultResolveOptions
resolveOpts.TargetPlatform = opts.Platform.Platform
desc, err := oras.Resolve(ctx, repo, opts.Reference, resolveOpts)

if err != nil {
return fmt.Errorf("failed to resolve digest: %w", err)
}

if opts.FullRef {
fmt.Printf("%s@%s\n", opts.Path, desc.Digest)
} else {
fmt.Println(desc.Digest.String())
}

return nil
}
1 change: 1 addition & 0 deletions test/e2e/internal/utils/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
BlobRepo = "command/blobs"
ArtifactRepo = "command/artifacts"
Namespace = "command"
InvalidRepo = "INVALID"
// env
RegHostKey = "ORAS_REGISTRY_HOST"
FallbackRegHostKey = "ORAS_REGISTRY_FALLBACK_HOST"
Expand Down
58 changes: 58 additions & 0 deletions test/e2e/suite/command/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"fmt"
"strings"

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"oras.land/oras/test/e2e/internal/testdata/multi_arch"
. "oras.land/oras/test/e2e/internal/utils"
)

var _ = Describe("ORAS beginners:", func() {
When("running resolve command", func() {
It("should fail when no manifest reference provided", func() {
ORAS("resolve").ExpectFailure().MatchErrKeyWords("Error:").Exec()
})
It("should fail when repo is invalid", func() {
ORAS("resolve", fmt.Sprintf("%s/%s", ZOTHost, InvalidRepo)).ExpectFailure().MatchErrKeyWords("Error:", fmt.Sprintf("invalid reference: invalid repository %q", InvalidRepo)).Exec()
})
It("should fail when no tag or digest provided", func() {
ORAS("resolve", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "no tag or digest when expecting <name:tag|name@digest>").Exec()
})
It("should fail when provided manifest reference is not found", func() {
ORAS("resolve", RegistryRef(ZOTHost, ImageRepo, "i-dont-think-this-tag-exists")).ExpectFailure().MatchErrKeyWords("Error: failed to resolve digest:", "not found").Exec()
})
It("should resolve with just digest", func() {
out := ORAS("resolve", RegistryRef(ZOTHost, ImageRepo, multi_arch.Digest)).Exec().Out
outString := string(out.Contents())
outString = strings.TrimSpace(outString)
gomega.Expect(outString).To(gomega.Equal(multi_arch.Digest))
})
It("should resolve with a fully qualified reference", func() {
out := ORAS("digest", "-l", RegistryRef(ZOTHost, ImageRepo, multi_arch.Tag)).Exec().Out
gomega.Expect(out).To(gbytes.Say(fmt.Sprintf("%s/%s@%s", ZOTHost, ImageRepo, multi_arch.Digest)))
})
It("should resolve with a fully qualified reference for a platform", func() {
out := ORAS("resolve", "--full-reference", "--platform", "linux/amd64", RegistryRef(ZOTHost, ImageRepo, multi_arch.Tag)).Exec().Out
gomega.Expect(out).To(gbytes.Say(fmt.Sprintf("%s/%s@%s", ZOTHost, ImageRepo, multi_arch.LinuxAMD64.Digest)))
})
})
})
Loading