github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/client/image_tag.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  
     7  	"github.com/docker/distribution/reference"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // ImageTag tags an image in the docker host
    12  func (cli *Client) ImageTag(ctx context.Context, source, target string) error {
    13  	if _, err := reference.ParseAnyReference(source); err != nil {
    14  		return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", source)
    15  	}
    16  
    17  	ref, err := reference.ParseNormalizedNamed(target)
    18  	if err != nil {
    19  		return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", target)
    20  	}
    21  
    22  	if _, isCanonical := ref.(reference.Canonical); isCanonical {
    23  		return errors.New("refusing to create a tag with a digest reference")
    24  	}
    25  
    26  	ref = reference.TagNameOnly(ref)
    27  
    28  	query := url.Values{}
    29  	query.Set("repo", reference.FamiliarName(ref))
    30  	if tagged, ok := ref.(reference.Tagged); ok {
    31  		query.Set("tag", tagged.Tag())
    32  	}
    33  
    34  	resp, err := cli.post(ctx, "/images/"+source+"/tag", query, nil, nil)
    35  	ensureReaderClosed(resp)
    36  	return err
    37  }