2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
distreference "github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/docker/api/types/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 {
|
|
|
|
if _, err := distreference.ParseNamed(source); err != nil {
|
|
|
|
return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", source)
|
|
|
|
}
|
|
|
|
|
|
|
|
distributionRef, err := distreference.ParseNamed(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
|
|
|
}
|
|
|
|
|
|
|
|
if _, isCanonical := distributionRef.(distreference.Canonical); isCanonical {
|
|
|
|
return errors.New("refusing to create a tag with a digest reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := reference.GetTagFromNamedRef(distributionRef)
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("repo", distributionRef.Name())
|
|
|
|
query.Set("tag", tag)
|
|
|
|
|
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
|
|
|
|
}
|