Skip to content

Commit

Permalink
Allow setting Content-Type in crane edit manifest (#1551)
Browse files Browse the repository at this point in the history
This uses the --media-type flag.
  • Loading branch information
jonjohnsonjr committed Feb 3, 2023
1 parent e04520b commit de35f0f
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions internal/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ func NewCmdEditConfig(options *[]crane.Option) *cobra.Command {

// NewCmdManifest creates a new cobra.Command for the manifest subcommand.
func NewCmdEditManifest(options *[]crane.Option) *cobra.Command {
var dst string
var (
dst string
mt string
)
cmd := &cobra.Command{
Use: "manifest",
Short: "Edit an image's manifest.",
Example: ` # Edit ubuntu's config file
crane edit config ubuntu
# Overwrite ubuntu's config file with '{}'
echo '{}' | crane edit config ubuntu`,
Example: ` # Edit ubuntu's manifest
crane edit manifest ubuntu`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ref, err := editManifest(cmd.InOrStdin(), cmd.OutOrStdout(), args[0], dst, *options...)
ref, err := editManifest(cmd.InOrStdin(), cmd.OutOrStdout(), args[0], dst, mt, *options...)
if err != nil {
return fmt.Errorf("editing manifest: %w", err)
}
Expand All @@ -104,6 +104,7 @@ func NewCmdEditManifest(options *[]crane.Option) *cobra.Command {
},
}
cmd.Flags().StringVarP(&dst, "tag", "t", "", "New tag reference to apply to mutated image. If not provided, uses original tag or pushes a new digest.")
cmd.Flags().StringVarP(&mt, "media-type", "m", "", "Override the mediaType used as the Content-Type for PUT")

return cmd
}
Expand Down Expand Up @@ -233,7 +234,7 @@ func editConfig(in io.Reader, out io.Writer, src, dst string, options ...crane.O
return dstRef, nil
}

func editManifest(in io.Reader, out io.Writer, src string, dst string, options ...crane.Option) (name.Reference, error) {
func editManifest(in io.Reader, out io.Writer, src string, dst string, mt string, options ...crane.Option) (name.Reference, error) {
o := crane.GetOptions(options...)

ref, err := name.ParseReference(src, o.Name...)
Expand Down Expand Up @@ -276,9 +277,22 @@ func editManifest(in io.Reader, out io.Writer, src string, dst string, options .
return nil, err
}

if mt == "" {
// If --media-type is unset, use Content-Type by default.
mt = string(desc.MediaType)

// If document contains mediaType, default to that.
wmt := withMediaType{}
if err := json.Unmarshal(edited, &wmt); err == nil {
if wmt.MediaType != "" {
mt = wmt.MediaType
}
}
}

rm := &rawManifest{
body: edited,
mediaType: desc.MediaType,
mediaType: types.MediaType(mt),
}

if err := remote.Put(dstRef, rm, o.Remote...); err != nil {
Expand Down Expand Up @@ -421,6 +435,10 @@ func normalize(name string) string {
return filepath.Clean("/" + name)
}

type withMediaType struct {
MediaType string `json:"mediaType,omitempty"`
}

type rawManifest struct {
body []byte
mediaType types.MediaType
Expand Down

0 comments on commit de35f0f

Please sign in to comment.