mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-22 14:07:10 +00:00
While not best practice, underscore can be used and so should be allowed.
73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
// Copyright 2020-2021 Clastix Labs
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package pod
|
|
|
|
import (
|
|
"regexp"
|
|
)
|
|
|
|
const defaultRegistryName = "docker.io"
|
|
|
|
type registry map[string]string
|
|
|
|
func (r registry) Registry() string {
|
|
res, ok := r["registry"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
if len(res) == 0 {
|
|
return defaultRegistryName
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (r registry) Repository() string {
|
|
res, ok := r["repository"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
if res == defaultRegistryName {
|
|
return ""
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (r registry) Image() string {
|
|
res, ok := r["image"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (r registry) Tag() string {
|
|
res, ok := r["tag"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
if len(res) == 0 {
|
|
res = "latest"
|
|
}
|
|
return res
|
|
}
|
|
|
|
func NewRegistry(value string) Registry {
|
|
reg := make(registry)
|
|
r := regexp.MustCompile(`(((?P<registry>[a-zA-Z0-9-._]+)\/)?((?P<repository>[a-zA-Z0-9-._]+)\/))?(?P<image>[a-zA-Z0-9-._]+)(:(?P<tag>[a-zA-Z0-9-._]+))?`)
|
|
match := r.FindStringSubmatch(value)
|
|
for i, name := range r.SubexpNames() {
|
|
if i > 0 && i <= len(match) {
|
|
reg[name] = match[i]
|
|
}
|
|
}
|
|
return reg
|
|
}
|
|
|
|
type Registry interface {
|
|
Registry() string
|
|
Repository() string
|
|
Image() string
|
|
Tag() string
|
|
}
|