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