-
Notifications
You must be signed in to change notification settings - Fork 185
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>", | ||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should validate the reference is in form of |
||
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 GitHub Actions / Analyze (1.21)
Check failure on line 59 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
Check failure on line 59 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
|
||
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 GitHub Actions / Analyze (1.21)
Check failure on line 76 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
Check failure on line 76 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
|
||
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest) | ||
Check failure on line 77 in cmd/oras/root/resolve.go GitHub Actions / Analyze (1.21)
Check failure on line 77 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
Check failure on line 77 in cmd/oras/root/resolve.go GitHub Actions / lint (1.21)
|
||
} | ||
fmt.Printf("%s\n", opts.RawReference) | ||
} else { | ||
fmt.Println(desc.Digest.String()) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
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