|
| 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/distribution/reference" |
| 9 | + |
| 10 | + "github.com/Sirupsen/logrus" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +type annotateOptions struct { |
| 16 | + target string // the target manifest list name (also transaction ID) |
| 17 | + image string // the manifest to annotate within the list |
| 18 | + variant string // an architecture variant |
| 19 | + os string |
| 20 | + arch string |
| 21 | + osFeatures []string |
| 22 | +} |
| 23 | + |
| 24 | +// NewAnnotateCommand creates a new `docker manifest annotate` command |
| 25 | +func newAnnotateCommand(dockerCli *command.DockerCli) *cobra.Command { |
| 26 | + var opts annotateOptions |
| 27 | + |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "annotate NAME[:TAG] [OPTIONS]", |
| 30 | + Short: "Add additional information to an image's manifest.", |
| 31 | + Args: cli.ExactArgs(2), |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + opts.target = args[0] |
| 34 | + opts.image = args[1] |
| 35 | + return runManifestAnnotate(dockerCli, opts) |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + flags := cmd.Flags() |
| 40 | + |
| 41 | + flags.StringVar(&opts.os, "os", "", "Add ios info to a manifest before pushing it.") |
| 42 | + flags.StringVar(&opts.arch, "arch", "", "Add arch info to a manifest before pushing it.") |
| 43 | + flags.StringSliceVar(&opts.osFeatures, "os-features", []string{}, "Add feature info to a manifest before pushing it.") |
| 44 | + flags.StringVar(&opts.variant, "variant", "", "Add arch variant to a manifest before pushing it.") |
| 45 | + |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func runManifestAnnotate(dockerCli *command.DockerCli, opts annotateOptions) error { |
| 50 | + |
| 51 | + // Make sure the manifests are pulled, find the file you need, unmarshal the json, edit the file, and done. |
| 52 | + targetRef, err := reference.ParseNormalizedNamed(opts.target) |
| 53 | + if err != nil { |
| 54 | + return errors.Wrapf(err, "annotate: Error parsing name for manifest list (%s): %s", opts.target) |
| 55 | + } |
| 56 | + imgRef, err := reference.ParseNormalizedNamed(opts.image) |
| 57 | + if err != nil { |
| 58 | + return errors.Wrapf(err, "annotate: Error prasing name for manifest (%s): %s:", opts.image) |
| 59 | + } |
| 60 | + |
| 61 | + // Make sure we've got tags or digests: |
| 62 | + if _, isDigested := targetRef.(reference.Canonical); !isDigested { |
| 63 | + targetRef = reference.TagNameOnly(targetRef) |
| 64 | + } |
| 65 | + if _, isDigested := imgRef.(reference.Canonical); !isDigested { |
| 66 | + imgRef = reference.TagNameOnly(imgRef) |
| 67 | + } |
| 68 | + transactionID := makeFilesafeName(targetRef.String()) |
| 69 | + imgID := makeFilesafeName(imgRef.String()) |
| 70 | + logrus.Debugf("beginning annotate for %s/%s", transactionID, imgID) |
| 71 | + |
| 72 | + imgInspect, _, err := getImageData(dockerCli, imgRef.String(), targetRef.String(), false) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if len(imgInspect) > 1 { |
| 78 | + return fmt.Errorf("cannot annotate manifest list. Please pass an image (not list) name") |
| 79 | + } |
| 80 | + |
| 81 | + mf := imgInspect[0] |
| 82 | + |
| 83 | + newMf, err := unmarshalIntoManifestInspect(imgID, transactionID) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + // Update the mf |
| 89 | + if opts.os != "" { |
| 90 | + newMf.OS = opts.os |
| 91 | + } |
| 92 | + if opts.arch != "" { |
| 93 | + newMf.Architecture = opts.arch |
| 94 | + } |
| 95 | + for _, osFeature := range opts.osFeatures { |
| 96 | + newMf.OSFeatures = appendIfUnique(mf.OSFeatures, osFeature) |
| 97 | + } |
| 98 | + if opts.variant != "" { |
| 99 | + newMf.Variant = opts.variant |
| 100 | + } |
| 101 | + |
| 102 | + // validate os/arch input |
| 103 | + if !isValidOSArch(newMf.OS, newMf.Architecture) { |
| 104 | + return fmt.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch) |
| 105 | + } |
| 106 | + |
| 107 | + if err := updateMfFile(newMf, imgID, transactionID); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + logrus.Debugf("annotated %s with options %v", mf.RefName, opts) |
| 112 | + return nil |
| 113 | +} |
| 114 | +func appendIfUnique(list []string, str string) []string { |
| 115 | + for _, s := range list { |
| 116 | + if s == str { |
| 117 | + return list |
| 118 | + } |
| 119 | + } |
| 120 | + return append(list, str) |
| 121 | +} |
0 commit comments