From b7619f2a53b178095a2809db1215ba309f90303b Mon Sep 17 00:00:00 2001 From: Markus Lippert Date: Tue, 10 May 2022 21:04:22 +0200 Subject: [PATCH] feat(crane): allow setting the repository to push by digest (#1323) * feat(crane): allow setting the repository to push by digest Signed-off-by: Markus Lippert * apply suggestions Signed-off-by: Markus Lippert * Update cmd/crane/cmd/mutate.go Co-authored-by: Jason Hall * update docs Signed-off-by: Markus Lippert Co-authored-by: Jason Hall --- cmd/crane/cmd/mutate.go | 14 +++++++++++--- cmd/crane/doc/crane_mutate.md | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/crane/cmd/mutate.go b/cmd/crane/cmd/mutate.go index 0fffa77ec..a99def0e9 100644 --- a/cmd/crane/cmd/mutate.go +++ b/cmd/crane/cmd/mutate.go @@ -35,6 +35,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { var newLayers []string var outFile string var newRef string + var newRepo string var user string mutateCmd := &cobra.Command{ @@ -55,6 +56,10 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { } } + if newRepo != "" && newRef != "" { + return errors.New("repository can't be set when a tag is specified") + } + img, err := crane.Pull(ref, *options...) if err != nil { return fmt.Errorf("pulling %s: %w", ref, err) @@ -121,7 +126,9 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { // If that ref was provided by digest (e.g., output from // another crane command), then strip that and push the // mutated image by digest instead. - if newRef == "" { + if newRepo != "" { + newRef = newRepo + } else if newRef == "" { newRef = ref } digest, err := img.Digest() @@ -137,7 +144,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { if err != nil { return fmt.Errorf("parsing %s: %w", newRef, err) } - if _, ok := r.(name.Digest); ok { + if _, ok := r.(name.Digest); ok || newRepo != "" { newRef = r.Context().Digest(digest.String()).String() } if err := crane.Push(img, newRef, *options...); err != nil { @@ -153,7 +160,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { mutateCmd.Flags().StringToStringVarP(&envVars, "env", "e", nil, "New envvar to add") mutateCmd.Flags().StringSliceVar(&entrypoint, "entrypoint", nil, "New entrypoint to set") mutateCmd.Flags().StringSliceVar(&cmd, "cmd", nil, "New cmd to set") - mutateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag to apply to mutated image. If not provided, push by digest to the original image repository.") + mutateCmd.Flags().StringVar(&newRepo, "repo", "", "Repository to push the mutated image to. If provided, push by digest to this repository.") + mutateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag reference to apply to mutated image. If not provided, push by digest to the original image repository.") mutateCmd.Flags().StringVarP(&outFile, "output", "o", "", "Path to new tarball of resulting image") mutateCmd.Flags().StringSliceVar(&newLayers, "append", []string{}, "Path to tarball to append to image") mutateCmd.Flags().StringVarP(&user, "user", "u", "", "New user to set") diff --git a/cmd/crane/doc/crane_mutate.md b/cmd/crane/doc/crane_mutate.md index ad1a816ad..be6d28f94 100644 --- a/cmd/crane/doc/crane_mutate.md +++ b/cmd/crane/doc/crane_mutate.md @@ -17,7 +17,8 @@ crane mutate [flags] -h, --help help for mutate -l, --label stringToString New labels to add (default []) -o, --output string Path to new tarball of resulting image - -t, --tag string New tag to apply to mutated image. If not provided, push by digest to the original image repository. + --repo string Repository to push the mutated image to. If provided, push by digest to this repository. + -t, --tag string New tag reference to apply to mutated image. If not provided, push by digest to the original image repository. -u, --user string New user to set ```