Skip to content

Commit b7619f2

Browse files
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 <[email protected]> * apply suggestions Signed-off-by: Markus Lippert <[email protected]> * Update cmd/crane/cmd/mutate.go Co-authored-by: Jason Hall <[email protected]> * update docs Signed-off-by: Markus Lippert <[email protected]> Co-authored-by: Jason Hall <[email protected]>
1 parent 03eb0a0 commit b7619f2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Diff for: cmd/crane/cmd/mutate.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
3535
var newLayers []string
3636
var outFile string
3737
var newRef string
38+
var newRepo string
3839
var user string
3940

4041
mutateCmd := &cobra.Command{
@@ -55,6 +56,10 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
5556
}
5657
}
5758

59+
if newRepo != "" && newRef != "" {
60+
return errors.New("repository can't be set when a tag is specified")
61+
}
62+
5863
img, err := crane.Pull(ref, *options...)
5964
if err != nil {
6065
return fmt.Errorf("pulling %s: %w", ref, err)
@@ -121,7 +126,9 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
121126
// If that ref was provided by digest (e.g., output from
122127
// another crane command), then strip that and push the
123128
// mutated image by digest instead.
124-
if newRef == "" {
129+
if newRepo != "" {
130+
newRef = newRepo
131+
} else if newRef == "" {
125132
newRef = ref
126133
}
127134
digest, err := img.Digest()
@@ -137,7 +144,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
137144
if err != nil {
138145
return fmt.Errorf("parsing %s: %w", newRef, err)
139146
}
140-
if _, ok := r.(name.Digest); ok {
147+
if _, ok := r.(name.Digest); ok || newRepo != "" {
141148
newRef = r.Context().Digest(digest.String()).String()
142149
}
143150
if err := crane.Push(img, newRef, *options...); err != nil {
@@ -153,7 +160,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
153160
mutateCmd.Flags().StringToStringVarP(&envVars, "env", "e", nil, "New envvar to add")
154161
mutateCmd.Flags().StringSliceVar(&entrypoint, "entrypoint", nil, "New entrypoint to set")
155162
mutateCmd.Flags().StringSliceVar(&cmd, "cmd", nil, "New cmd to set")
156-
mutateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag to apply to mutated image. If not provided, push by digest to the original image repository.")
163+
mutateCmd.Flags().StringVar(&newRepo, "repo", "", "Repository to push the mutated image to. If provided, push by digest to this repository.")
164+
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.")
157165
mutateCmd.Flags().StringVarP(&outFile, "output", "o", "", "Path to new tarball of resulting image")
158166
mutateCmd.Flags().StringSliceVar(&newLayers, "append", []string{}, "Path to tarball to append to image")
159167
mutateCmd.Flags().StringVarP(&user, "user", "u", "", "New user to set")

Diff for: cmd/crane/doc/crane_mutate.md

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)