2017-06-15 14:41:54 -04:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/manifest/store"
|
2018-06-28 20:41:47 -04:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-06-15 14:41:54 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type annotateOptions struct {
|
|
|
|
target string // the target manifest list name (also transaction ID)
|
|
|
|
image string // the manifest to annotate within the list
|
|
|
|
variant string // an architecture variant
|
|
|
|
os string
|
|
|
|
arch string
|
|
|
|
osFeatures []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAnnotateCommand creates a new `docker manifest annotate` command
|
|
|
|
func newAnnotateCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
var opts annotateOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "annotate [OPTIONS] MANIFEST_LIST MANIFEST",
|
|
|
|
Short: "Add additional information to a local image manifest",
|
|
|
|
Args: cli.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.target = args[0]
|
|
|
|
opts.image = args[1]
|
|
|
|
return runManifestAnnotate(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.StringVar(&opts.os, "os", "", "Set operating system")
|
|
|
|
flags.StringVar(&opts.arch, "arch", "", "Set architecture")
|
|
|
|
flags.StringSliceVar(&opts.osFeatures, "os-features", []string{}, "Set operating system feature")
|
|
|
|
flags.StringVar(&opts.variant, "variant", "", "Set architecture variant")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runManifestAnnotate(dockerCli command.Cli, opts annotateOptions) error {
|
|
|
|
targetRef, err := normalizeReference(opts.target)
|
|
|
|
if err != nil {
|
2017-09-26 18:15:04 -04:00
|
|
|
return errors.Wrapf(err, "annotate: error parsing name for manifest list %s", opts.target)
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
imgRef, err := normalizeReference(opts.image)
|
|
|
|
if err != nil {
|
2017-09-26 18:15:04 -04:00
|
|
|
return errors.Wrapf(err, "annotate: error parsing name for manifest %s", opts.image)
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
manifestStore := dockerCli.ManifestStore()
|
|
|
|
imageManifest, err := manifestStore.Get(targetRef, imgRef)
|
|
|
|
switch {
|
|
|
|
case store.IsNotFound(err):
|
|
|
|
return fmt.Errorf("manifest for image %s does not exist in %s", opts.image, opts.target)
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the mf
|
2018-06-28 20:41:47 -04:00
|
|
|
if imageManifest.Descriptor.Platform == nil {
|
|
|
|
imageManifest.Descriptor.Platform = new(ocispec.Platform)
|
|
|
|
}
|
2017-06-15 14:41:54 -04:00
|
|
|
if opts.os != "" {
|
2018-06-28 20:41:47 -04:00
|
|
|
imageManifest.Descriptor.Platform.OS = opts.os
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
if opts.arch != "" {
|
2018-06-28 20:41:47 -04:00
|
|
|
imageManifest.Descriptor.Platform.Architecture = opts.arch
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
for _, osFeature := range opts.osFeatures {
|
2018-06-28 20:41:47 -04:00
|
|
|
imageManifest.Descriptor.Platform.OSFeatures = appendIfUnique(imageManifest.Descriptor.Platform.OSFeatures, osFeature)
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
if opts.variant != "" {
|
2018-06-28 20:41:47 -04:00
|
|
|
imageManifest.Descriptor.Platform.Variant = opts.variant
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2018-06-28 20:41:47 -04:00
|
|
|
if !isValidOSArch(imageManifest.Descriptor.Platform.OS, imageManifest.Descriptor.Platform.Architecture) {
|
2017-06-15 14:41:54 -04:00
|
|
|
return errors.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch)
|
|
|
|
}
|
|
|
|
return manifestStore.Save(targetRef, imgRef, imageManifest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendIfUnique(list []string, str string) []string {
|
|
|
|
for _, s := range list {
|
|
|
|
if s == str {
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(list, str)
|
|
|
|
}
|