Skip to content

Commit

Permalink
Add output option to mutate to save to a tar file and not push to a r…
Browse files Browse the repository at this point in the history
…egistry (same as append) (#1257)

* Add output option to mutate to save to a tar file and not push to a registry (same as append)

* Fix docs for new output flag in mutate
  • Loading branch information
ehmm authored Feb 23, 2022
1 parent dd8d514 commit 17d8b10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 18 additions & 11 deletions cmd/crane/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
var entrypoint, cmd []string
var envVars map[string]string
var newLayers []string

var outFile string
var newRef string

mutateCmd := &cobra.Command{
Expand Down Expand Up @@ -122,17 +122,23 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
if err != nil {
return fmt.Errorf("digesting new image: %w", err)
}
r, err := name.ParseReference(newRef)
if err != nil {
return fmt.Errorf("parsing %s: %w", newRef, err)
}
if _, ok := r.(name.Digest); ok {
newRef = r.Context().Digest(digest.String()).String()
}
if err := crane.Push(img, newRef, *options...); err != nil {
return fmt.Errorf("pushing %s: %w", newRef, err)
if outFile != "" {
if err := crane.Save(img, newRef, outFile); err != nil {
return fmt.Errorf("writing output %q: %w", outFile, err)
}
} else {
r, err := name.ParseReference(newRef)
if err != nil {
return fmt.Errorf("parsing %s: %w", newRef, err)
}
if _, ok := r.(name.Digest); ok {
newRef = r.Context().Digest(digest.String()).String()
}
if err := crane.Push(img, newRef, *options...); err != nil {
return fmt.Errorf("pushing %s: %w", newRef, err)
}
fmt.Println(r.Context().Digest(digest.String()))
}
fmt.Println(r.Context().Digest(digest.String()))
return nil
},
}
Expand All @@ -142,6 +148,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
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().StringVarP(&outFile, "output", "o", "", "Path to new tarball of resulting image")
mutateCmd.Flags().StringSliceVar(&newLayers, "append", []string{}, "Path to tarball to append to image")
return mutateCmd
}
Expand Down
1 change: 1 addition & 0 deletions 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 17d8b10

Please sign in to comment.