-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults.go
More file actions
50 lines (41 loc) · 1.09 KB
/
Copy pathdefaults.go
File metadata and controls
50 lines (41 loc) · 1.09 KB
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
package purl
import (
"net/url"
"strings"
)
// IsDefaultRegistry returns true if the registryURL matches the default registry for the type.
func IsDefaultRegistry(purlType, registryURL string) bool {
if registryURL == "" {
return true
}
cfg := TypeInfo(purlType)
if cfg == nil || cfg.DefaultRegistry == nil {
return false
}
defaultURL := *cfg.DefaultRegistry
if defaultURL == "" {
return false
}
// Compare hosts
defaultHost := extractHost(defaultURL)
givenHost := extractHost(registryURL)
if defaultHost == "" || givenHost == "" {
return false
}
return givenHost == defaultHost || strings.HasSuffix(givenHost, "."+defaultHost)
}
// IsNonDefaultRegistry returns true if the registryURL is not the default registry for the type.
func IsNonDefaultRegistry(purlType, registryURL string) bool {
if registryURL == "" {
return false
}
return !IsDefaultRegistry(purlType, registryURL)
}
// extractHost extracts the hostname from a URL string using net/url.Parse.
func extractHost(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return ""
}
return u.Hostname()
}