Skip to content

Commit 7ea49c2

Browse files
feat: add --config flag support for oras attach command
Add support for customizing the config descriptor when using oras attach, matching the existing --config flag behavior in oras push. This allows users to specify a config file with an optional media type using the format --config <file>[:<type>]. - Add manifestConfigRef field and --config flag to attach command - Parse config file reference and set ConfigDescriptor in pack options - Add config and platform mutual exclusivity validation - Add config annotations support for attach - Add usage example in command help text Fixes #1146
1 parent 52b065a commit 7ea49c2

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

cmd/oras/root/attach.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"oras.land/oras/cmd/oras/internal/command"
3131
"oras.land/oras/cmd/oras/internal/display"
3232
oerrors "oras.land/oras/cmd/oras/internal/errors"
33+
"oras.land/oras/cmd/oras/internal/fileref"
3334
"oras.land/oras/cmd/oras/internal/option"
3435
"oras.land/oras/internal/graph"
3536
"oras.land/oras/internal/registryutil"
@@ -43,8 +44,9 @@ type attachOptions struct {
4344
option.Platform
4445
option.Terminal
4546

46-
artifactType string
47-
concurrency int
47+
artifactType string
48+
manifestConfigRef string
49+
concurrency int
4850
// Deprecated: verbose is deprecated and will be removed in the future.
4951
verbose bool
5052
}
@@ -92,6 +94,9 @@ Example - Attach file to the manifest tagged 'v1' in an OCI image layout folder
9294
9395
Example - Attach file to the manifest tagged 'example.com:v1' in an OCI image layout folder 'layout-dir':
9496
oras attach --artifact-type doc/example --oci-layout-path layout-dir example.com:v1 hi.txt
97+
98+
Example - Attach file 'hi.txt' with the custom manifest config "config.json" of the custom media type "application/vnd.me.config":
99+
oras attach --artifact-type doc/example --config config.json:application/vnd.me.config localhost:5000/hello:v1 hi.txt
95100
`,
96101
Args: oerrors.CheckArgs(argument.AtLeast(1), "the destination artifact for attaching."),
97102
PreRunE: func(cmd *cobra.Command, args []string) error {
@@ -100,6 +105,9 @@ Example - Attach file to the manifest tagged 'example.com:v1' in an OCI image la
100105
err := option.Parse(cmd, &opts)
101106
if err == nil {
102107
opts.DisableTTY(opts.Debug, false)
108+
if err = oerrors.CheckMutuallyExclusiveFlags(cmd.Flags(), "config", "platform"); err != nil {
109+
return err
110+
}
103111
if err = opts.EnsureReferenceNotEmpty(cmd, true); err == nil {
104112
return nil
105113
}
@@ -121,6 +129,7 @@ Example - Attach file to the manifest tagged 'example.com:v1' in an OCI image la
121129
}
122130

123131
cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type")
132+
cmd.Flags().StringVarP(&opts.manifestConfigRef, "config", "", "", "`path` of image config file")
124133
cmd.Flags().IntVarP(&opts.concurrency, "concurrency", "", 5, "concurrency level")
125134
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", true, "print status output for unnamed blobs")
126135
opts.FlagDescription = "attach to an arch-specific subject"
@@ -184,9 +193,22 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
184193

185194
packOpts := oras.PackManifestOptions{
186195
Subject: &subject,
196+
ConfigAnnotations: opts.Annotations[option.AnnotationConfig],
187197
ManifestAnnotations: opts.Annotations[option.AnnotationManifest],
188198
Layers: descs,
189199
}
200+
if opts.manifestConfigRef != "" {
201+
path, cfgMediaType, err := fileref.Parse(opts.manifestConfigRef, oras.MediaTypeUnknownConfig)
202+
if err != nil {
203+
return err
204+
}
205+
desc, err := addFile(ctx, store, option.AnnotationConfig, cfgMediaType, path)
206+
if err != nil {
207+
return err
208+
}
209+
desc.Annotations = packOpts.ConfigAnnotations
210+
packOpts.ConfigDescriptor = &desc
211+
}
190212
pack := func() (ocispec.Descriptor, error) {
191213
return oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, opts.artifactType, packOpts)
192214
}

0 commit comments

Comments
 (0)