github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/api/server/backend/build/tag.go (about)

     1  package build // import "github.com/docker/docker/api/server/backend/build"
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/docker/distribution/reference"
     8  	"github.com/docker/docker/image"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // tagImages creates image tags for the imageID.
    13  func tagImages(ic ImageComponent, stdout io.Writer, imageID image.ID, repoAndTags []reference.Named) error {
    14  	for _, rt := range repoAndTags {
    15  		if err := ic.TagImageWithReference(imageID, rt); err != nil {
    16  			return err
    17  		}
    18  		_, _ = fmt.Fprintln(stdout, "Successfully tagged", reference.FamiliarString(rt))
    19  	}
    20  	return nil
    21  }
    22  
    23  // sanitizeRepoAndTags parses the raw "t" parameter received from the client
    24  // to a slice of repoAndTag.
    25  // It also validates each repoName and tag.
    26  func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
    27  	var (
    28  		repoAndTags []reference.Named
    29  		// This map is used for deduplicating the "-t" parameter.
    30  		uniqNames = make(map[string]struct{})
    31  	)
    32  	for _, repo := range names {
    33  		if repo == "" {
    34  			continue
    35  		}
    36  
    37  		ref, err := reference.ParseNormalizedNamed(repo)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		if _, isCanonical := ref.(reference.Canonical); isCanonical {
    43  			return nil, errors.New("build tag cannot contain a digest")
    44  		}
    45  
    46  		ref = reference.TagNameOnly(ref)
    47  
    48  		nameWithTag := ref.String()
    49  
    50  		if _, exists := uniqNames[nameWithTag]; !exists {
    51  			uniqNames[nameWithTag] = struct{}{}
    52  			repoAndTags = append(repoAndTags, ref)
    53  		}
    54  	}
    55  	return repoAndTags, nil
    56  }