Skip to content

Commit de35f0f

Browse files
authored
Allow setting Content-Type in crane edit manifest (#1551)
This uses the --media-type flag.
1 parent e04520b commit de35f0f

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

internal/cmd/edit.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ func NewCmdEditConfig(options *[]crane.Option) *cobra.Command {
8484

8585
// NewCmdManifest creates a new cobra.Command for the manifest subcommand.
8686
func NewCmdEditManifest(options *[]crane.Option) *cobra.Command {
87-
var dst string
87+
var (
88+
dst string
89+
mt string
90+
)
8891
cmd := &cobra.Command{
8992
Use: "manifest",
9093
Short: "Edit an image's manifest.",
91-
Example: ` # Edit ubuntu's config file
92-
crane edit config ubuntu
93-
94-
# Overwrite ubuntu's config file with '{}'
95-
echo '{}' | crane edit config ubuntu`,
94+
Example: ` # Edit ubuntu's manifest
95+
crane edit manifest ubuntu`,
9696
Args: cobra.ExactArgs(1),
9797
RunE: func(cmd *cobra.Command, args []string) error {
98-
ref, err := editManifest(cmd.InOrStdin(), cmd.OutOrStdout(), args[0], dst, *options...)
98+
ref, err := editManifest(cmd.InOrStdin(), cmd.OutOrStdout(), args[0], dst, mt, *options...)
9999
if err != nil {
100100
return fmt.Errorf("editing manifest: %w", err)
101101
}
@@ -104,6 +104,7 @@ func NewCmdEditManifest(options *[]crane.Option) *cobra.Command {
104104
},
105105
}
106106
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.")
107+
cmd.Flags().StringVarP(&mt, "media-type", "m", "", "Override the mediaType used as the Content-Type for PUT")
107108

108109
return cmd
109110
}
@@ -233,7 +234,7 @@ func editConfig(in io.Reader, out io.Writer, src, dst string, options ...crane.O
233234
return dstRef, nil
234235
}
235236

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

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

280+
if mt == "" {
281+
// If --media-type is unset, use Content-Type by default.
282+
mt = string(desc.MediaType)
283+
284+
// If document contains mediaType, default to that.
285+
wmt := withMediaType{}
286+
if err := json.Unmarshal(edited, &wmt); err == nil {
287+
if wmt.MediaType != "" {
288+
mt = wmt.MediaType
289+
}
290+
}
291+
}
292+
279293
rm := &rawManifest{
280294
body: edited,
281-
mediaType: desc.MediaType,
295+
mediaType: types.MediaType(mt),
282296
}
283297

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

438+
type withMediaType struct {
439+
MediaType string `json:"mediaType,omitempty"`
440+
}
441+
424442
type rawManifest struct {
425443
body []byte
426444
mediaType types.MediaType

0 commit comments

Comments
 (0)