github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/api/server/backend/build/tag.go (about)

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