github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/image/reference/reference.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package reference
    16  
    17  import (
    18  	"errors"
    19  	"strings"
    20  )
    21  
    22  type Named struct {
    23  	domain  string // like ***.com, won't be empty
    24  	raw     string // this name is going to be local tag name
    25  	repo    string // k8s, sealer/k8s
    26  	repoTag string // sealer/k8s:v1.6
    27  	tag     string // v1.6
    28  }
    29  
    30  // ParseToNamed build a ImageNamed
    31  func ParseToNamed(name string) (Named, error) {
    32  	name = strings.TrimSpace(name)
    33  	if err := validate(name); err != nil {
    34  		return Named{}, err
    35  	}
    36  
    37  	var named Named
    38  	named.raw = buildRaw(name)
    39  	named.domain, named.repoTag = normalizeDomainRepoTag(name)
    40  	named.repo, named.tag = buildRepoAndTag(named.repoTag)
    41  	if strings.ToLower(named.repo) != named.repo {
    42  		return named, errors.New("uppercase is not allowed in image name")
    43  	}
    44  	return named, nil
    45  }
    46  
    47  func (n Named) String() string {
    48  	return n.Name()
    49  }
    50  
    51  func (n Named) Name() string {
    52  	if n.domain == "" {
    53  		return n.Repo()
    54  	}
    55  	return n.domain + "/" + n.Repo()
    56  }
    57  
    58  func (n Named) Domain() string {
    59  	return n.domain
    60  }
    61  
    62  func (n Named) RepoTag() string {
    63  	return n.repoTag
    64  }
    65  
    66  func (n Named) Raw() string {
    67  	return n.raw
    68  }
    69  
    70  func (n Named) Repo() string {
    71  	return n.repo
    72  }
    73  
    74  func (n Named) Tag() string {
    75  	return n.tag
    76  }
    77  
    78  func (n Named) CompleteName() string {
    79  	return n.domain + "/" + n.repoTag
    80  }