github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/tag.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  
     6  	Cli "github.com/docker/docker/cli"
     7  	flag "github.com/docker/docker/pkg/mflag"
     8  	"github.com/docker/docker/reference"
     9  	"github.com/docker/engine-api/types"
    10  )
    11  
    12  // CmdTag tags an image into a repository.
    13  //
    14  // Usage: docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
    15  func (cli *DockerCli) CmdTag(args ...string) error {
    16  	cmd := Cli.Subcmd("tag", []string{"IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]"}, Cli.DockerCommands["tag"].Description, true)
    17  	force := cmd.Bool([]string{"#f", "#-force"}, false, "Force the tagging even if there's a conflict")
    18  	cmd.Require(flag.Exact, 2)
    19  
    20  	cmd.ParseFlags(args, true)
    21  
    22  	ref, err := reference.ParseNamed(cmd.Arg(1))
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	if _, isCanonical := ref.(reference.Canonical); isCanonical {
    28  		return errors.New("refusing to create a tag with a digest reference")
    29  	}
    30  
    31  	var tag string
    32  	if tagged, isTagged := ref.(reference.NamedTagged); isTagged {
    33  		tag = tagged.Tag()
    34  	}
    35  
    36  	options := types.ImageTagOptions{
    37  		ImageID:        cmd.Arg(0),
    38  		RepositoryName: ref.Name(),
    39  		Tag:            tag,
    40  		Force:          *force,
    41  	}
    42  
    43  	return cli.client.ImageTag(options)
    44  }