/
locator_type.go
146 lines (120 loc) · 3.78 KB
/
locator_type.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package buildpack
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/dist"
)
type LocatorType int
const (
InvalidLocator LocatorType = iota
FromBuilderLocator
URILocator
IDLocator
PackageLocator
RegistryLocator
// added entries here should also be added to `String()`
)
const (
fromBuilderPrefix = "urn:cnb:builder"
deprecatedFromBuilderPrefix = "from=builder"
fromRegistryPrefix = "urn:cnb:registry"
fromDockerPrefix = "docker:/"
)
var (
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
semverPattern = `(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`
registryPattern = regexp.MustCompile(`^[a-z0-9\-\.]+\/[a-z0-9\-\.]+(?:@` + semverPattern + `)?$`)
)
func (l LocatorType) String() string {
return []string{
"InvalidLocator",
"FromBuilderLocator",
"URILocator",
"IDLocator",
"PackageLocator",
"RegistryLocator",
}[l]
}
// GetLocatorType determines which type of locator is designated by the given input.
// If a type cannot be determined, `INVALID_LOCATOR` will be returned. If an error
// is encountered, it will be returned.
func GetLocatorType(locator string, relativeBaseDir string, buildpacksFromBuilder []dist.ModuleInfo) (LocatorType, error) {
if locator == deprecatedFromBuilderPrefix {
return FromBuilderLocator, nil
}
if strings.HasPrefix(locator, fromBuilderPrefix+":") || strings.HasPrefix(locator, deprecatedFromBuilderPrefix+":") {
if !isFoundInBuilder(locator, buildpacksFromBuilder) {
return InvalidLocator, fmt.Errorf("%s is not a valid identifier", style.Symbol(locator))
}
return IDLocator, nil
}
if strings.HasPrefix(locator, fromRegistryPrefix+":") {
return RegistryLocator, nil
}
if paths.IsURI(locator) {
if HasDockerLocator(locator) {
if _, err := name.ParseReference(locator); err == nil {
return PackageLocator, nil
}
}
return URILocator, nil
}
return parseNakedLocator(locator, relativeBaseDir, buildpacksFromBuilder), nil
}
func HasDockerLocator(locator string) bool {
return strings.HasPrefix(locator, fromDockerPrefix)
}
func parseNakedLocator(locator, relativeBaseDir string, buildpacksFromBuilder []dist.ModuleInfo) LocatorType {
// from here on, we're dealing with a naked locator, and we try to figure out what it is. To do this we check
// the following characteristics in order:
// 1. Does it match a path on the file system
// 2. Does it match a buildpack ID in the builder
// 3. Does it look like a Buildpack Registry ID
// 4. Does it look like a Docker ref
if isLocalFile(locator, relativeBaseDir) {
return URILocator
}
if isFoundInBuilder(locator, buildpacksFromBuilder) {
return IDLocator
}
if canBeRegistryRef(locator) {
return RegistryLocator
}
if canBePackageRef(locator) {
return PackageLocator
}
return InvalidLocator
}
func canBePackageRef(locator string) bool {
if _, err := name.ParseReference(locator); err == nil {
return true
}
return false
}
func canBeRegistryRef(locator string) bool {
return registryPattern.MatchString(locator)
}
func isFoundInBuilder(locator string, candidates []dist.ModuleInfo) bool {
id, version := ParseIDLocator(locator)
for _, c := range candidates {
if id == c.ID && (version == "" || version == c.Version) {
return true
}
}
return false
}
func isLocalFile(locator, relativeBaseDir string) bool {
if !filepath.IsAbs(locator) {
locator = filepath.Join(relativeBaseDir, locator)
}
if _, err := os.Stat(locator); err == nil {
return true
}
return false
}