From 9e83417ce93b118334ec1ead78e519647a868aa2 Mon Sep 17 00:00:00 2001 From: amands98 Date: Sat, 22 Jul 2023 18:14:25 +0530 Subject: [PATCH 1/2] add command to get the digest of a tagged manifest without parsing output Signed-off-by: amands98 --- cmd/oras/root/cmd.go | 1 + cmd/oras/root/resolve.go | 81 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 cmd/oras/root/resolve.go diff --git a/cmd/oras/root/cmd.go b/cmd/oras/root/cmd.go index 67fd6d231..14e0e468a 100644 --- a/cmd/oras/root/cmd.go +++ b/cmd/oras/root/cmd.go @@ -34,6 +34,7 @@ func New() *cobra.Command { logoutCmd(), versionCmd(), discoverCmd(), + resolveCmd(), copyCmd(), tagCmd(), attachCmd(), diff --git a/cmd/oras/root/resolve.go b/cmd/oras/root/resolve.go new file mode 100644 index 000000000..60d12ed2f --- /dev/null +++ b/cmd/oras/root/resolve.go @@ -0,0 +1,81 @@ +/* +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] :", + 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] + 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) + 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 { + fmt.Printf("%s@%s\n", opts.RawReference, desc.Digest.String()) + } else { + fmt.Println(desc.Digest.String()) + } + + return nil +} \ No newline at end of file From 413e2229f419fbf956f43ede73c9e15bf6ec5313 Mon Sep 17 00:00:00 2001 From: Aman <136564604+amands98@users.noreply.github.com> Date: Sun, 30 Jul 2023 19:00:41 +0530 Subject: [PATCH 2/2] Update cmd/oras/root/resolve.go Co-authored-by: Billy Zha Signed-off-by: Aman <136564604+amands98@users.noreply.github.com> --- cmd/oras/root/resolve.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/oras/root/resolve.go b/cmd/oras/root/resolve.go index 60d12ed2f..e2cd724e9 100644 --- a/cmd/oras/root/resolve.go +++ b/cmd/oras/root/resolve.go @@ -72,7 +72,11 @@ func runResolve(ctx context.Context, opts resolveOptions) error { } if opts.FullRef { - fmt.Printf("%s@%s\n", opts.RawReference, desc.Digest.String()) + digest := desc.Digest.String() + if !strings.HasSuffix(opts.RawReference, digest) { + opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest) + } + fmt.Printf("%s\n", opts.RawReference) } else { fmt.Println(desc.Digest.String()) }