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

[WIP] Add digest-only option #429

Closed
wants to merge 1 commit 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
49 changes: 49 additions & 0 deletions cmd/oras/internal/option/pusher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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 option

import (
"context"
"os"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/pflag"
"oras.land/oras-go/v2/content"
)

// Pusher option struct.
type Pusher struct {
DigestOnly bool
ManifestExport string
}

// ApplyFlags applies flags to a command flag set.
func (opts *Pusher) ApplyFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&opts.DigestOnly, "digest-only", "", false, "will manifest(s) be pushed without tagging")
fs.StringVarP(&opts.ManifestExport, "export-manifest", "", "", "export the pushed manifest")
}

// ExportManifest saves the pushed manifest to a local file.
func (opts *Pusher) ExportManifest(ctx context.Context, pushed ocispec.Descriptor, pushedTo content.Fetcher) error {
if opts.ManifestExport == "" {
return nil
}
manifestBytes, err := content.FetchAll(ctx, pushedTo, pushed)
if err != nil {
return err
}
return os.WriteFile(opts.ManifestExport, manifestBytes, 0666)
}
30 changes: 10 additions & 20 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
Expand All @@ -40,12 +39,12 @@ const (
type pushOptions struct {
option.Common
option.Remote
option.Pusher

targetRef string
fileRefs []string
pathValidationDisabled bool
manifestAnnotations string
manifestExport string
manifestConfigRef string
}

Expand Down Expand Up @@ -88,7 +87,6 @@ Example - Push file to the HTTP registry:
cmd.Flags().StringVarP(&opts.manifestAnnotations, "manifest-annotations", "", "", "manifest annotation file")
cmd.Flags().BoolVarP(&opts.pathValidationDisabled, "disable-path-validation", "", false, "skip path validation")
cmd.Flags().StringVarP(&opts.manifestConfigRef, "manifest-config", "", "", "manifest config file")
cmd.Flags().StringVarP(&opts.manifestExport, "export-manifest", "", "", "export the pushed manifest")

option.ApplyFlags(&opts, cmd.Flags())
return cmd
Expand All @@ -97,11 +95,6 @@ Example - Push file to the HTTP registry:
func runPush(opts pushOptions) error {
ctx, _ := opts.SetLoggerLevel()

dst, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
}

// Load annotations
var annotations map[string]map[string]string
if opts.manifestAnnotations != "" {
Expand Down Expand Up @@ -134,7 +127,13 @@ func runPush(opts pushOptions) error {
if err != nil {
return err
}
if tag := dst.Reference.Reference; tag == "" {

// Push
dst, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
}
if tag := dst.Reference.Reference; tag == "" || opts.DigestOnly {
err = oras.CopyGraph(ctx, store, dst, desc, copyOptions.CopyGraphOptions)
} else {
desc, err = oras.Copy(ctx, store, tagStaged, dst, tag, copyOptions)
Expand All @@ -146,17 +145,8 @@ func runPush(opts pushOptions) error {
fmt.Println("Pushed", opts.targetRef)
fmt.Println("Digest:", desc.Digest)

// export manifest
if opts.manifestExport != "" {
manifestBytes, err := content.FetchAll(ctx, store, desc)
if err != nil {
return err
}
if err = os.WriteFile(opts.manifestExport, manifestBytes, 0666); err != nil {
return err
}
}
return nil
// Export manifest
return opts.ExportManifest(ctx, desc, store)
}

func decodeJSON(filename string, v interface{}) error {
Expand Down