github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/util/buildflags/context.go (about)

     1  package buildflags
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/distribution/reference"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  func ParseContextNames(values []string) (map[string]string, error) {
    11  	if len(values) == 0 {
    12  		return nil, nil
    13  	}
    14  	result := make(map[string]string, len(values))
    15  	for _, value := range values {
    16  		kv := strings.SplitN(value, "=", 2)
    17  		if len(kv) != 2 {
    18  			return nil, errors.Errorf("invalid context value: %s, expected key=value", value)
    19  		}
    20  		named, err := reference.ParseNormalizedNamed(kv[0])
    21  		if err != nil {
    22  			return nil, errors.Wrapf(err, "invalid context name %s", kv[0])
    23  		}
    24  		name := strings.TrimSuffix(reference.FamiliarString(named), ":latest")
    25  		result[name] = kv[1]
    26  	}
    27  	return result, nil
    28  }