github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/release/uniqname/uniqname.go (about) 1 package uniqname 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 ) 8 9 // Separator is a separator between release name and namespace. 10 const Separator = "@" 11 12 var validateRegexp = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])?") 13 14 // UniqName is an alias for string. 15 type UniqName string 16 17 // Generate returns uniqname for provided release name and namespace. 18 func Generate(name, namespace string) (UniqName, error) { 19 u := UniqName(fmt.Sprintf("%s%s%s", name, Separator, namespace)) 20 21 return u, u.Validate() 22 } 23 24 // GenerateWithDefaultNamespace parses uniqname out of provided line. 25 // If there is no namespace in line, default namespace will be used. 26 func GenerateWithDefaultNamespace(line, namespace string) (UniqName, error) { 27 s := strings.Split(line, Separator) 28 29 name := s[0] 30 31 if len(s) > 1 && s[1] != "" { 32 namespace = s[1] 33 } 34 35 return Generate(name, namespace) 36 } 37 38 // Equal checks whether uniqnames are equal. 39 func (n UniqName) Equal(a UniqName) bool { 40 return n == a 41 } 42 43 // Validate validates this object. 44 func (n UniqName) Validate() error { 45 s := strings.Split(n.String(), Separator) 46 if len(s) != 2 { 47 return NewValidationError(n.String()) 48 } 49 50 if !validateRegexp.MatchString(s[0]) { 51 return NewValidationError(n.String()) 52 } 53 54 if !validateRegexp.MatchString(s[1]) { 55 return NewValidationError(n.String()) 56 } 57 58 return nil 59 } 60 61 func (n UniqName) String() string { 62 return string(n) 63 }