Skip to content

Commit

Permalink
feat(crane): allow setting the repository to push by digest (#1323)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
lippertmarkus and imjasonh authored May 10, 2022
1 parent 03eb0a0 commit b7619f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 11 additions & 3 deletions cmd/crane/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion cmd/crane/doc/crane_mutate.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b7619f2

Please sign in to comment.