-
Notifications
You must be signed in to change notification settings - Fork 18
/
module_fs.go
133 lines (113 loc) · 3.18 KB
/
module_fs.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
package resolver
import (
"context"
"errors"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/git"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/github"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/scopedlayer"
"path"
"regexp"
)
type relativeLocation struct {
Path string
}
type gitLocation struct {
URL string
Revision string
Path string
}
type gitHubLocation struct {
Owner string
Name string
Revision string
Path string
}
var (
ErrRetrievalFailed = errors.New("failed to retrieve a module")
ErrUnsupportedLocation = errors.New("unsupported location")
)
const (
// Captures the path after / in non-greedy manner.
optionalPath = `(?:/(?P<path>.*?))?`
// Captures the revision after @ in non-greedy manner.
optionalRevision = `(?:@(?P<revision>.*))?`
)
var (
githubRegexVariant = regexp.MustCompile(
`^(?P<root>github\.com/(?P<owner>.*?)/(?P<name>.*?))` + optionalPath + optionalRevision + `$`,
)
genericGitRegexVariant = regexp.MustCompile(`^(?P<root>.*?)\.git` + optionalPath + optionalRevision + `$`)
)
func parseLocation(module string) interface{} {
matches := githubRegexVariant.FindStringSubmatch(module)
if matches != nil {
owner := matches[githubRegexVariant.SubexpIndex("owner")]
name := matches[githubRegexVariant.SubexpIndex("name")]
revision := matches[githubRegexVariant.SubexpIndex("revision")]
if revision == "" {
revision = "main"
}
modulePath := matches[githubRegexVariant.SubexpIndex("path")]
if modulePath == "" {
modulePath = "lib.star"
}
return gitHubLocation{
Owner: owner,
Name: name,
Revision: revision,
Path: modulePath,
}
}
matches = genericGitRegexVariant.FindStringSubmatch(module)
if matches != nil {
revision := matches[genericGitRegexVariant.SubexpIndex("revision")]
if revision == "" {
revision = "main"
}
modulePath := matches[genericGitRegexVariant.SubexpIndex("path")]
if modulePath == "" {
modulePath = "lib.star"
}
return gitLocation{
URL: "https://" + matches[genericGitRegexVariant.SubexpIndex("root")] + ".git",
Revision: revision,
Path: modulePath,
}
}
return relativeLocation{Path: module}
}
func FindModuleFS(
ctx context.Context,
currentFS fs.FileSystem,
env map[string]string,
module string,
) (fs.FileSystem, string, error) {
return findLocatorFS(ctx, currentFS, env, parseLocation(module))
}
func findLocatorFS(
ctx context.Context,
currentFS fs.FileSystem,
env map[string]string,
location interface{},
) (fs.FileSystem, string, error) {
switch typedLocation := location.(type) {
case gitHubLocation:
token := env["CIRRUS_REPO_CLONE_TOKEN"]
ghFS, err := github.New(typedLocation.Owner, typedLocation.Name, typedLocation.Revision, token)
if err != nil {
return nil, "", err
}
return ghFS, typedLocation.Path, nil
case gitLocation:
gitFS, err := git.New(ctx, typedLocation.URL, typedLocation.Revision)
if err != nil {
return nil, "", err
}
return gitFS, typedLocation.Path, nil
case relativeLocation:
return scopedlayer.New(currentFS, path.Dir(typedLocation.Path)), path.Base(typedLocation.Path), nil
default:
return nil, "", ErrUnsupportedLocation
}
}