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: support tagging multiple tags at once to a remote registry #574

Merged
merged 7 commits into from
Sep 19, 2022
Merged
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
51 changes: 43 additions & 8 deletions cmd/oras/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ limitations under the License.
package tag

import (
"context"
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
)
Expand All @@ -26,14 +32,15 @@ type tagOptions struct {
option.Common
option.Remote

srcRef string
targetRef string
concurrency int64
srcRef string
targetRefs []string
}

func TagCmd() *cobra.Command {
var opts tagOptions
cmd := &cobra.Command{
Use: "tag name<:tag|@digest> new_tag",
Use: "tag [flags] name<:tag|@digest> <new_tag...>",
Short: "[Preview] tag a manifest in the remote registry",
Long: `[Preview] tag a manifest in the remote registry

Expand All @@ -42,24 +49,33 @@ func TagCmd() *cobra.Command {
Example - Tag the manifest 'v1.0.1' in 'locahost:5000/hello' to 'v1.0.2':
oras tag localhost:5000/hello:v1.0.1 v1.0.2

Example - Tag the manifest with digest sha256:9463e0d192846bc994279417b50114606712d516aab45f4d8b31cbc6e46aad71 to 'v1.0.3'
oras tag localhost:5000/hello@sha256:9463e0d192846bc994279417b50114606712d516aab45f4d8b31cbc6e46aad71 v1.0.3
Example - Tag the manifest with digest sha256:9463e0d192846bc994279417b50114606712d516aab45f4d8b31cbc6e46aad71 to 'v1.0.2'
oras tag localhost:5000/hello@sha256:9463e0d192846bc994279417b50114606712d516aab45f4d8b31cbc6e46aad71 v1.0.2
wju-MSFT marked this conversation as resolved.
Show resolved Hide resolved

Example - Tag the manifest 'v1.0.1' in 'locahost:5000/hello' to 'v1.0.2', 'latest'
orast tag localhost:5000/hello:v1.0.1 v1.0.2 latest

Example - Tag the manifest 'v1.0.1' in 'locahost:5000/hello' to 'v1.0.2' 'latest' with the custom concurrency number of 1:
oras tag --concurrency 1 localhost:5000/hello:v1.0.1 v1.0.2 latest
`,
Args: cobra.ExactArgs(2),
Args: cobra.MinimumNArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.ReadPassword()
},
RunE: func(_ *cobra.Command, args []string) error {
opts.srcRef = args[0]
opts.targetRef = args[1]
opts.targetRefs = args[1:]
return tagManifest(opts)
},
}

cmd.Flags().Int64VarP(&opts.concurrency, "concurrency", "", 5, "provide concurrency number, default is 5")
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func tagManifest(opts tagOptions) error {
var nOpts oras.TagNOptions
ctx, _ := opts.SetLoggerLevel()
repo, err := opts.NewRepository(opts.srcRef, opts.Common)
if err != nil {
Expand All @@ -70,5 +86,24 @@ func tagManifest(opts tagOptions) error {
return errors.NewErrInvalidReference(repo.Reference)
}

return oras.Tag(ctx, repo, opts.srcRef, opts.targetRef)
listener := &tagManifestListener{
repo,
}

nOpts.Concurrency = opts.concurrency

return oras.TagN(ctx, listener, opts.srcRef, opts.targetRefs, nOpts)
}

type tagManifestListener struct {
*remote.Repository
}

// PushReference overrides Repository.PushReference method to print off which tag(s) were added successfully.
func (l *tagManifestListener) PushReference(ctx context.Context, expected ocispec.Descriptor, content io.Reader, reference string) error {
if err := l.Repository.PushReference(ctx, expected, content, reference); err != nil {
return err
}
display.Print("Tagged", reference)
return nil
}