2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2017-01-03 15:02:58 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageTag tags an image in the docker host
|
2017-01-03 15:02:58 +00:00
|
|
|
func (cli *Client) ImageTag(ctx context.Context, source, target string) error {
|
2017-04-11 20:37:04 +00:00
|
|
|
if _, err := reference.ParseAnyReference(source); err != nil {
|
2017-01-03 15:02:58 +00:00
|
|
|
return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", source)
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref, err := reference.ParseNormalizedNamed(target)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
2017-01-03 15:02:58 +00:00
|
|
|
return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", target)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2016-09-06 18:46:37 +00:00
|
|
|
return errors.New("refusing to create a tag with a digest reference")
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref = reference.TagNameOnly(ref)
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
query := url.Values{}
|
2017-01-26 00:54:18 +00:00
|
|
|
query.Set("repo", reference.FamiliarName(ref))
|
|
|
|
if tagged, ok := ref.(reference.Tagged); ok {
|
|
|
|
query.Set("tag", tagged.Tag())
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2017-01-03 15:02:58 +00:00
|
|
|
resp, err := cli.post(ctx, "/images/"+source+"/tag", query, nil, nil)
|
2016-09-06 18:46:37 +00:00
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return err
|
|
|
|
}
|