2017-06-27 13:45:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/containers/image/docker/reference"
|
|
|
|
"github.com/containers/storage"
|
2017-09-06 16:51:29 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libpod"
|
2017-06-27 13:45:25 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tagDescription = "Adds one or more additional names to locally-stored image"
|
|
|
|
tagCommand = cli.Command{
|
|
|
|
Name: "tag",
|
|
|
|
Usage: "Add an additional name to a local image",
|
|
|
|
Description: tagDescription,
|
|
|
|
Action: tagCmd,
|
|
|
|
ArgsUsage: "IMAGE-NAME [IMAGE-NAME ...]",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func tagCmd(c *cli.Context) error {
|
|
|
|
args := c.Args()
|
|
|
|
if len(args) < 2 {
|
|
|
|
return errors.Errorf("image name and at least one new name must be specified")
|
|
|
|
}
|
2017-09-06 16:51:29 +00:00
|
|
|
runtime, err := getRuntime(c)
|
2017-07-27 17:18:07 +00:00
|
|
|
if err != nil {
|
2017-09-06 16:51:29 +00:00
|
|
|
return errors.Wrapf(err, "could not create runtime")
|
2017-07-27 17:18:07 +00:00
|
|
|
}
|
2017-09-09 11:14:40 +00:00
|
|
|
defer runtime.Shutdown(false)
|
|
|
|
|
2017-09-06 16:51:29 +00:00
|
|
|
img, err := runtime.GetImage(args[0])
|
2017-06-27 13:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if img == nil {
|
|
|
|
return errors.New("null image")
|
|
|
|
}
|
2017-09-06 16:51:29 +00:00
|
|
|
err = addImageNames(runtime, img, args[1:])
|
2017-06-27 13:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error adding names %v to image %q", args[1:], args[0])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 16:51:29 +00:00
|
|
|
func addImageNames(runtime *libpod.Runtime, image *storage.Image, addNames []string) error {
|
2017-06-27 13:45:25 +00:00
|
|
|
// Add tags to the names if applicable
|
|
|
|
names, err := expandedTags(addNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-06 16:51:29 +00:00
|
|
|
for _, name := range names {
|
|
|
|
if err := runtime.TagImage(image, name); err != nil {
|
2017-09-09 11:14:40 +00:00
|
|
|
return errors.Wrapf(err, "error adding name (%v) to image %q", name, image.ID)
|
2017-09-06 16:51:29 +00:00
|
|
|
}
|
2017-06-27 13:45:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandedTags(tags []string) ([]string, error) {
|
|
|
|
expandedNames := []string{}
|
|
|
|
for _, tag := range tags {
|
2017-09-05 19:36:49 +00:00
|
|
|
var labelName string
|
|
|
|
name, err := reference.Parse(tag)
|
2017-06-27 13:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing tag %q", name)
|
|
|
|
}
|
2017-09-05 19:36:49 +00:00
|
|
|
if _, ok := name.(reference.NamedTagged); ok {
|
|
|
|
labelName = name.String()
|
|
|
|
} else {
|
|
|
|
labelName = name.String() + ":latest"
|
2017-06-27 13:45:25 +00:00
|
|
|
}
|
2017-09-05 19:36:49 +00:00
|
|
|
expandedNames = append(expandedNames, labelName)
|
2017-06-27 13:45:25 +00:00
|
|
|
}
|
|
|
|
return expandedNames, nil
|
|
|
|
}
|