mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-03 07:06:30 +00:00
f2c33a0d89
closes #1837
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package utils
|
|
|
|
import "github.com/docker/distribution/reference"
|
|
|
|
// trimImage returns the short image name without tag.
|
|
func trimImage(name string) string {
|
|
ref, err := reference.ParseAnyReference(name)
|
|
if err != nil {
|
|
return name
|
|
}
|
|
named, err := reference.ParseNamed(ref.String())
|
|
if err != nil {
|
|
return name
|
|
}
|
|
named = reference.TrimNamed(named)
|
|
return reference.FamiliarName(named)
|
|
}
|
|
|
|
// expandImage returns the fully qualified image name.
|
|
func expandImage(name string) string {
|
|
ref, err := reference.ParseAnyReference(name)
|
|
if err != nil {
|
|
return name
|
|
}
|
|
named, err := reference.ParseNamed(ref.String())
|
|
if err != nil {
|
|
return name
|
|
}
|
|
named = reference.TagNameOnly(named)
|
|
return named.String()
|
|
}
|
|
|
|
// MatchImage returns true if the image name matches
|
|
// an image in the list. Note the image tag is not used
|
|
// in the matching logic.
|
|
func MatchImage(from string, to ...string) bool {
|
|
from = trimImage(from)
|
|
for _, match := range to {
|
|
if from == trimImage(match) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MatchHostname returns true if the image hostname
|
|
// matches the specified hostname.
|
|
func MatchHostname(image, hostname string) bool {
|
|
ref, err := reference.ParseAnyReference(image)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
named, err := reference.ParseNamed(ref.String())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if hostname == "index.docker.io" {
|
|
hostname = "docker.io"
|
|
}
|
|
return reference.Domain(named) == hostname
|
|
}
|