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 command to get the digest of a tagged manifest without parsing output #1034

Closed
wants to merge 4 commits into from
Closed
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
85 changes: 85 additions & 0 deletions cmd/oras/root/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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>",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command should be experimental, please check how other commands do it, e.g. oras discover

Short: "Resolves digest of the target artifact",
Args: cobra.ExactArgs(1),
Aliases: []string{"digest"},
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.RawReference = args[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should validate the reference is in form of <name>:<tag>

return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runResolve(cmd.Context(), opts)
},
}

cmd.Flags().BoolVar(&opts.FullRef, "full-ref", false, "print the full artifact reference with digest")
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func runResolve(ctx context.Context, opts resolveOptions) error {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewReadonlyTarget(ctx, opts.Common)

Check failure on line 59 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / Analyze (1.21)

not enough arguments in call to opts.NewReadonlyTarget

Check failure on line 59 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

not enough arguments in call to opts.NewReadonlyTarget

Check failure on line 59 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

not enough arguments in call to opts.NewReadonlyTarget

Check failure on line 59 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / build (1.21)

not enough arguments in call to opts.NewReadonlyTarget
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 the digest: %w", err)
}

if opts.FullRef {
digest := desc.Digest.String()
if !strings.HasSuffix(opts.RawReference, digest) {

Check failure on line 76 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / Analyze (1.21)

undefined: strings

Check failure on line 76 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: strings

Check failure on line 76 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: strings

Check failure on line 76 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / build (1.21)

undefined: strings
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)

Check failure on line 77 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / Analyze (1.21)

undefined: subject

Check failure on line 77 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: subject) (typecheck)

Check failure on line 77 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: subject (typecheck)

Check failure on line 77 in cmd/oras/root/resolve.go

View workflow job for this annotation

GitHub Actions / build (1.21)

undefined: subject
}
fmt.Printf("%s\n", opts.RawReference)
} else {
fmt.Println(desc.Digest.String())
}

return nil
}
Loading