forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocatortype.go
85 lines (69 loc) · 1.88 KB
/
locatortype.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
package buildpack
import (
"fmt"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
)
type LocatorType int
const (
InvalidLocator = iota
FromBuilderLocator
URILocator
IDLocator
PackageLocator
RegistryLocator
)
const fromBuilderPrefix = "from=builder"
const fromRegistryPrefix = "urn:cnb:registry"
func (l LocatorType) String() string {
return []string{
"InvalidLocator",
"FromBuilderLocator",
"URILocator",
"IDLocator",
"PackageLocator",
}[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, buildpacksFromBuilder []dist.BuildpackInfo) (LocatorType, error) {
if locator == fromBuilderPrefix {
return FromBuilderLocator, nil
}
if strings.HasPrefix(locator, fromBuilderPrefix+":") {
if !builderMatchFound(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) {
return URILocator, nil
}
if _, err := os.Stat(locator); err == nil {
return URILocator, nil
}
if builderMatchFound(locator, buildpacksFromBuilder) {
return IDLocator, nil
}
if _, err := name.ParseReference(locator); err == nil {
return PackageLocator, nil
}
return InvalidLocator, nil
}
func builderMatchFound(locator string, candidates []dist.BuildpackInfo) bool {
id, version := ParseIDLocator(locator)
for _, c := range candidates {
if id == c.ID && (version == "" || version == c.Version) {
return true
}
}
return false
}