-
Notifications
You must be signed in to change notification settings - Fork 582
/
container.go
56 lines (47 loc) · 1.63 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package docker
import (
"fmt"
"net/url"
"regexp"
"strings"
containerName "github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
)
// ContainerRegistryFromURL provides the registry part of a complete registry url including the port
func ContainerRegistryFromURL(registryURL string) (string, error) {
u, err := url.ParseRequestURI(registryURL)
if err != nil {
return "", errors.Wrap(err, "invalid registry url")
}
if len(u.Host) == 0 {
return "", fmt.Errorf("invalid registry url")
}
return u.Host, nil
}
// ContainerRegistryFromImage provides the registry part of a full image name
func ContainerRegistryFromImage(fullImage string) (string, error) {
ref, err := containerName.ParseReference(strings.ToLower(fullImage))
if err != nil {
return "", errors.Wrap(err, "failed to parse image name")
}
return ref.Context().RegistryStr(), nil
}
// ContainerImageNameTagFromImage provides the name & tag part of a full image name
func ContainerImageNameTagFromImage(fullImage string) (string, error) {
ref, err := containerName.ParseReference(strings.ToLower(fullImage))
if err != nil {
return "", errors.Wrap(err, "failed to parse image name")
}
registryOnly := fmt.Sprintf("%v/", ref.Context().RegistryStr())
return strings.ReplaceAll(fullImage, registryOnly, ""), nil
}
// ContainerImageNameFromImage returns the image name of a given docker reference
func ContainerImageNameFromImage(fullImage string) (string, error) {
imageNameTag, err := ContainerImageNameTagFromImage(fullImage)
if err != nil {
return "", err
}
r := regexp.MustCompile(`([^:@]+)`)
m := r.FindStringSubmatch(imageNameTag)
return m[0], nil
}