-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathpurl.go
More file actions
33 lines (26 loc) · 890 Bytes
/
purl.go
File metadata and controls
33 lines (26 loc) · 890 Bytes
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
package git
import (
"fmt"
"net/url"
"strings"
packageurl "github.com/package-url/packageurl-go"
)
// BuildGenericRepoPURL returns an unversioned generic purl
// Example: pkg:generic/github.com/owner/repo
func BuildGenericRepoPURL(repoURL string) (string, error) {
u, err := url.Parse(repoURL)
if err != nil {
return "", fmt.Errorf("invalid repo url: %w", err)
}
host := strings.ToLower(u.Hostname())
path := strings.Trim(strings.TrimSuffix(u.EscapedPath(), ".git"), "/")
parts := strings.Split(path, "/")
if len(parts) < 2 {
return "", fmt.Errorf("invalid repo path in %q", repoURL)
}
// Namespace is host + all path segments except the last; name is the last segment.
ns := strings.Join(append([]string{host}, parts[:len(parts)-1]...), "/")
name := parts[len(parts)-1]
p := packageurl.NewPackageURL("generic", ns, name, "", nil, "")
return p.ToString(), nil
}