Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
223 lines (198 loc) · 6.37 KB
/
Copy pathconfig.go
File metadata and controls
223 lines (198 loc) · 6.37 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package velox
import (
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
const (
defaultBranch = "master"
defaultGitHubBaseURL = "https://github.com"
// RefKey, LogLevelKey, and LogModeKey are the velox.toml keys of the Roadrunner and Log maps.
RefKey = "ref"
LogLevelKey = "level"
LogModeKey = "mode"
)
var ErrWindowsTarget = errors.New("velox v3 does not support Windows targets")
// ValidateTargetOS rejects target operating systems that velox v3 does not support.
func ValidateTargetOS(goos string) error {
if strings.EqualFold(goos, "windows") {
return ErrWindowsTarget
}
return nil
}
type Config struct {
Roadrunner map[string]string `mapstructure:"roadrunner"`
Debug *Debug `mapstructure:"debug"`
Log map[string]string `mapstructure:"log"`
TargetPlatform *TargetPlatform `mapstructure:"target_platform"`
GitHub *GitHub `mapstructure:"github"`
Plugins map[string]*Plugin `mapstructure:"plugins"`
Replaces []Replace `mapstructure:"replaces"`
Excludes []Exclude `mapstructure:"excludes"`
}
type Debug struct {
Enabled bool `mapstructure:"enabled"`
Race bool `mapstructure:"race"`
}
type TargetPlatform struct {
OS string `mapstructure:"os"`
Arch string `mapstructure:"arch"`
}
type GitHub struct {
Token *Token `mapstructure:"token"`
BaseURL string `mapstructure:"base_url"`
}
type Token struct {
Token string `mapstructure:"token"`
}
type Plugin struct {
Tag string `mapstructure:"tag"`
ModuleName string `mapstructure:"module_name"`
}
// Replace mirrors `replace old => new` in go.mod, with the version embedded inline as "module@v1.2.3".
type Replace struct {
New string `mapstructure:"new"`
Old string `mapstructure:"old"`
}
// Exclude mirrors `exclude module version` in go.mod.
type Exclude struct {
Module string `mapstructure:"module"`
Version string `mapstructure:"version"`
}
// IsLocalPath reports whether s denotes a local filesystem path, using the predicate `go mod edit -replace` applies to its operand.
func IsLocalPath(s string) bool { return modfile.IsDirectoryPath(s) }
func (r Replace) Validate() error {
if r.New == "" || r.Old == "" {
return errors.New("replace: new and old are required")
}
// "=" breaks the old=new split that `go mod edit -replace` performs on the operand.
if strings.Contains(r.Old, "=") {
return fmt.Errorf("replace: %q in `old` must not contain '='", r.Old)
}
if strings.Contains(r.New, "=") {
return fmt.Errorf("replace: %q in `new` must not contain '='", r.New)
}
if IsLocalPath(r.New) {
// Only a trailing "@<semver>" is a version; "@" inside a directory name is fine.
if _, ver, ok := strings.CutLast(r.New, "@"); ok && semver.IsValid(ver) {
return fmt.Errorf("replace: local path %q in `new` must not include @version", r.New)
}
}
return nil
}
// ValidateRef rejects refs carrying characters that are unsafe in the unquoted -ldflags value.
func ValidateRef(ref string) error {
if ref == "" {
return errors.New("roadrunner ref must not be empty")
}
for i := range len(ref) {
c := ref[i]
allowed := c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' ||
c == '.' || c == '_' || c == '/' || c == '+' || c == '-'
if !allowed {
return fmt.Errorf("roadrunner ref %q contains an unsupported character %q", ref, string(c))
}
}
return nil
}
func (e Exclude) Validate() error {
if e.Module == "" || e.Version == "" {
return errors.New("exclude: module and version are required")
}
// go mod edit writes the version as given, and the next go.mod parse accepts only the canonical form.
if module.CanonicalVersion(e.Version) != e.Version {
return fmt.Errorf("exclude: version %q must be of the form v1.2.3", e.Version)
}
if err := module.Check(e.Module, e.Version); err != nil {
return fmt.Errorf("exclude: %w", err)
}
return nil
}
// Validate checks the configuration, applies the defaults, and expands ${ENV} in the GitHub token.
func (c *Config) Validate() error {
if c.Roadrunner == nil {
c.Roadrunner = map[string]string{}
}
if _, ok := c.Roadrunner[RefKey]; !ok {
c.Roadrunner[RefKey] = defaultBranch
}
if err := ValidateRef(c.Roadrunner[RefKey]); err != nil {
return err
}
if c.TargetPlatform == nil {
c.TargetPlatform = &TargetPlatform{}
}
if c.TargetPlatform.OS == "" {
c.TargetPlatform.OS = runtime.GOOS
}
if c.TargetPlatform.Arch == "" {
c.TargetPlatform.Arch = runtime.GOARCH
}
if err := ValidateTargetOS(c.TargetPlatform.OS); err != nil {
return err
}
if c.GitHub == nil {
c.GitHub = &GitHub{}
}
if c.GitHub.Token != nil {
c.GitHub.Token.Token = os.ExpandEnv(c.GitHub.Token.Token)
}
if c.GitHub.BaseURL == "" {
c.GitHub.BaseURL = defaultGitHubBaseURL
}
if len(c.Plugins) == 0 {
return errors.New("plugins configuration is required")
}
modules := make(map[string]string, len(c.Plugins))
for _, name := range slices.Sorted(maps.Keys(c.Plugins)) {
plugin := c.Plugins[name]
if plugin == nil {
return fmt.Errorf("plugin %q is empty", name)
}
if plugin.ModuleName == "" {
return fmt.Errorf("plugin %q module name is required", name)
}
if plugin.Tag == "" {
return fmt.Errorf("plugin %q (%s) tag is required", name, plugin.ModuleName)
}
if first, dup := modules[plugin.ModuleName]; dup {
return fmt.Errorf("plugin %q: module %s is already listed under %q", name, plugin.ModuleName, first)
}
modules[plugin.ModuleName] = name
}
seen := make(map[string]struct{}, len(c.Replaces))
for i, r := range c.Replaces {
if err := r.Validate(); err != nil {
return fmt.Errorf("replaces[%d]: %w", i, err)
}
if _, dup := seen[r.Old]; dup {
return fmt.Errorf("replaces[%d]: duplicate old %q", i, r.Old)
}
seen[r.Old] = struct{}{}
// go.mod resolves a relative replacement against its own directory, which is the extracted RoadRunner tree; the user means the working directory.
if IsLocalPath(r.New) && !filepath.IsAbs(r.New) {
abs, err := filepath.Abs(r.New)
if err != nil {
return fmt.Errorf("replaces[%d]: %w", i, err)
}
c.Replaces[i].New = abs
}
}
for i, e := range c.Excludes {
if err := e.Validate(); err != nil {
return fmt.Errorf("excludes[%d]: %w", i, err)
}
}
if len(c.Log) == 0 {
c.Log = map[string]string{LogLevelKey: "debug", LogModeKey: "development"}
}
return nil
}