diff --git a/components/engine/api/client/lib/image_tag.go b/components/engine/api/client/lib/image_tag.go new file mode 100644 index 0000000000..6ef5269cb0 --- /dev/null +++ b/components/engine/api/client/lib/image_tag.go @@ -0,0 +1,25 @@ +package lib + +import "net/url" + +// ImageTagOptions hold parameters to tag an image +type ImageTagOptions struct { + ImageID string + RepositoryName string + Tag string + Force bool +} + +// ImageTag tags an image in the docker host +func (cli *Client) ImageTag(options types.ImageTagOptions) error { + query := url.Values{} + query.Set("repo", options.RepositoryName) + query.Set("tag", options.Tag) + if options.Force { + query.Set("force", "1") + } + + resp, err := cli.POST("/images/"+options.ImageID+"/tag", query, nil, nil) + ensureReaderClosed(resp) + return err +} diff --git a/components/engine/api/client/tag.go b/components/engine/api/client/tag.go index a4a27cb4cd..27bf80cff8 100644 --- a/components/engine/api/client/tag.go +++ b/components/engine/api/client/tag.go @@ -2,9 +2,9 @@ package client import ( "errors" - "net/url" "github.com/docker/distribution/reference" + "github.com/docker/docker/api/client/lib" Cli "github.com/docker/docker/cli" flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/registry" @@ -20,7 +20,6 @@ func (cli *DockerCli) CmdTag(args ...string) error { cmd.ParseFlags(args, true) - v := url.Values{} ref, err := reference.ParseNamed(cmd.Arg(1)) if err != nil { return err @@ -41,15 +40,13 @@ func (cli *DockerCli) CmdTag(args ...string) error { if err := registry.ValidateRepositoryName(ref); err != nil { return err } - v.Set("repo", ref.Name()) - v.Set("tag", tag) - if *force { - v.Set("force", "1") + options := lib.ImageTagOptions{ + ImageID: cmd.Arg(0), + RepositoryName: ref.Name(), + Tag: tag, + Force: *force, } - if _, _, err := readBody(cli.call("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil, nil)); err != nil { - return err - } - return nil + return cli.client.ImageTag(options) }