|
| 1 | +package manifest |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/docker/cli/cli" |
| 7 | + "github.com/docker/cli/cli/command" |
| 8 | + "github.com/docker/cli/cli/manifest/store" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +type annotateOptions struct { |
| 14 | + target string // the target manifest list name (also transaction ID) |
| 15 | + image string // the manifest to annotate within the list |
| 16 | + variant string // an architecture variant |
| 17 | + os string |
| 18 | + arch string |
| 19 | + osFeatures []string |
| 20 | +} |
| 21 | + |
| 22 | +// NewAnnotateCommand creates a new `docker manifest annotate` command |
| 23 | +func newAnnotateCommand(dockerCli command.Cli) *cobra.Command { |
| 24 | + var opts annotateOptions |
| 25 | + |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "annotate [OPTIONS] MANIFEST_LIST MANIFEST", |
| 28 | + Short: "Add additional information to a local image manifest", |
| 29 | + Args: cli.ExactArgs(2), |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + opts.target = args[0] |
| 32 | + opts.image = args[1] |
| 33 | + return runManifestAnnotate(dockerCli, opts) |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + flags := cmd.Flags() |
| 38 | + |
| 39 | + flags.StringVar(&opts.os, "os", "", "Set operating system") |
| 40 | + flags.StringVar(&opts.arch, "arch", "", "Set architecture") |
| 41 | + flags.StringSliceVar(&opts.osFeatures, "os-features", []string{}, "Set operating system feature") |
| 42 | + flags.StringVar(&opts.variant, "variant", "", "Set architecture variant") |
| 43 | + |
| 44 | + return cmd |
| 45 | +} |
| 46 | + |
| 47 | +func runManifestAnnotate(dockerCli command.Cli, opts annotateOptions) error { |
| 48 | + targetRef, err := normalizeReference(opts.target) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrapf(err, "annotate: Error parsing name for manifest list (%s): %s", opts.target) |
| 51 | + } |
| 52 | + imgRef, err := normalizeReference(opts.image) |
| 53 | + if err != nil { |
| 54 | + return errors.Wrapf(err, "annotate: Error parsing name for manifest (%s): %s:", opts.image) |
| 55 | + } |
| 56 | + |
| 57 | + manifestStore := dockerCli.ManifestStore() |
| 58 | + imageManifest, err := manifestStore.Get(targetRef, imgRef) |
| 59 | + switch { |
| 60 | + case store.IsNotFound(err): |
| 61 | + return fmt.Errorf("manifest for image %s does not exist in %s", opts.image, opts.target) |
| 62 | + case err != nil: |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + // Update the mf |
| 67 | + if opts.os != "" { |
| 68 | + imageManifest.Platform.OS = opts.os |
| 69 | + } |
| 70 | + if opts.arch != "" { |
| 71 | + imageManifest.Platform.Architecture = opts.arch |
| 72 | + } |
| 73 | + for _, osFeature := range opts.osFeatures { |
| 74 | + imageManifest.Platform.OSFeatures = appendIfUnique(imageManifest.Platform.OSFeatures, osFeature) |
| 75 | + } |
| 76 | + if opts.variant != "" { |
| 77 | + imageManifest.Platform.Variant = opts.variant |
| 78 | + } |
| 79 | + |
| 80 | + if !isValidOSArch(imageManifest.Platform.OS, imageManifest.Platform.Architecture) { |
| 81 | + return errors.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch) |
| 82 | + } |
| 83 | + return manifestStore.Save(targetRef, imgRef, imageManifest) |
| 84 | +} |
| 85 | + |
| 86 | +func appendIfUnique(list []string, str string) []string { |
| 87 | + for _, s := range list { |
| 88 | + if s == str { |
| 89 | + return list |
| 90 | + } |
| 91 | + } |
| 92 | + return append(list, str) |
| 93 | +} |
0 commit comments