-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
package.go
394 lines (363 loc) · 11.3 KB
/
package.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package config
import (
"fmt"
"net/url"
"path"
"path/filepath"
"strings"
texttemplate "text/template"
"github.com/aquaproj/aqua/v2/pkg/asset"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/aquaproj/aqua/v2/pkg/template"
"github.com/aquaproj/aqua/v2/pkg/unarchive"
)
type Package struct {
Package *aqua.Package
PackageInfo *registry.PackageInfo
Registry *aqua.Registry
}
func (p *Package) ExePath(rootDir string, file *registry.File, rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
if pkgInfo.Type == "go_build" {
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, p.Package.Version, "bin", file.Name), nil
}
pkgPath, err := p.PkgPath(rootDir, rt)
if err != nil {
return "", err
}
fileSrc, err := p.fileSrc(file, rt)
if err != nil {
return "", fmt.Errorf("get a file path: %w", err)
}
return filepath.Join(pkgPath, fileSrc), nil
}
func (p *Package) RenderAsset(rt *runtime.Runtime) (string, error) {
asset, err := p.renderAsset(rt)
if err != nil {
return "", err
}
if asset == "" {
return "", nil
}
if p.PackageInfo.GetAppendExt() {
asset = appendExt(asset, p.PackageInfo.Format)
}
if !isWindows(rt.GOOS) {
return asset, nil
}
return p.completeWindowsExtToAsset(asset), nil
}
func (p *Package) TemplateArtifact(rt *runtime.Runtime, asset string) *template.Artifact {
pkg := p.Package
pkgInfo := p.PackageInfo
return &template.Artifact{
Version: pkg.Version,
SemVer: p.semVer(),
OS: replace(rt.GOOS, pkgInfo.Replacements),
Arch: getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
Format: pkgInfo.GetFormat(),
Asset: asset,
}
}
func (p *Package) RenderPath() (string, error) {
pkgInfo := p.PackageInfo
return p.RenderTemplateString(pkgInfo.GetPath(), &runtime.Runtime{})
}
func (p *Package) PkgPath(rootDir string, rt *runtime.Runtime) (string, error) { //nolint:cyclop
pkgInfo := p.PackageInfo
pkg := p.Package
assetName, err := p.RenderAsset(rt)
if err != nil {
return "", fmt.Errorf("render the asset name: %w", err)
}
switch pkgInfo.Type {
case PkgInfoTypeGitHubArchive:
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version), nil
case PkgInfoTypeGoBuild:
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, "src"), nil
case PkgInfoTypeGoInstall:
p, err := p.RenderPath()
if err != nil {
return "", fmt.Errorf("render Go Module Path: %w", err)
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, p, pkg.Version, "bin"), nil
case PkgInfoTypeCargo:
registry := "crates.io"
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, registry, pkgInfo.Crate, pkg.Version), nil
case PkgInfoTypeGitHubContent, PkgInfoTypeGitHubRelease:
if pkgInfo.RepoOwner == "aquaproj" && (pkgInfo.RepoName == "aqua" || pkgInfo.RepoName == "aqua-proxy") {
return filepath.Join(rootDir, "internal", "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
case PkgInfoTypeHTTP:
uS, err := p.RenderURL(rt)
if err != nil {
return "", fmt.Errorf("render URL: %w", err)
}
u, err := url.Parse(uS)
if err != nil {
return "", fmt.Errorf("parse the URL: %w", err)
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, u.Host, u.Path), nil
}
return "", nil
}
func (p *Package) RenderTemplateString(s string, rt *runtime.Runtime) (string, error) {
tpl, err := template.Compile(s)
if err != nil {
return "", fmt.Errorf("parse a template: %w", err)
}
return p.renderTemplate(tpl, rt)
}
func (p *Package) RenderURL(rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
s, err := p.RenderTemplateString(pkgInfo.URL, rt)
if err != nil {
return "", err
}
if p.PackageInfo.GetAppendExt() {
s = appendExt(s, p.PackageInfo.Format)
}
if !isWindows(rt.GOOS) {
return s, nil
}
return p.completeWindowsExtToURL(s), nil
}
type FileNotFoundError struct {
Err error
}
func (e *FileNotFoundError) Error() string {
return e.Err.Error()
}
func (e *FileNotFoundError) Unwrap() error {
return e.Err
}
func (p *Package) renderSrc(assetName string, file *registry.File, rt *runtime.Runtime) (string, error) {
pkg := p.Package
pkgInfo := p.PackageInfo
format := pkgInfo.GetFormat()
assetWithoutExt, _ := asset.RemoveExtFromAsset(assetName)
s, err := template.Execute(file.Src, map[string]interface{}{
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
"Format": format,
"FileName": file.Name,
"Asset": assetName,
"AssetWithoutExt": assetWithoutExt,
})
if err != nil {
return "", err //nolint:wrapcheck
}
return filepath.FromSlash(s), nil // FromSlash is needed for Windows. https://github.com/aquaproj/aqua/issues/2013
}
func replace(key string, replacements registry.Replacements) string {
a := replacements[key]
if a == "" {
return key
}
return a
}
func getArch(rosetta2, windowsARMEmulation bool, replacements registry.Replacements, rt *runtime.Runtime) string {
if rosetta2 && rt.GOOS == "darwin" && rt.GOARCH == "arm64" {
// Rosetta 2
return replace("amd64", replacements)
}
if windowsARMEmulation && rt.GOOS == "windows" && rt.GOARCH == "arm64" {
// Windows ARM Emulation
return replace("amd64", replacements)
}
return replace(rt.GOARCH, replacements)
}
func (p *Package) fileSrc(file *registry.File, rt *runtime.Runtime) (string, error) {
s, err := p.fileSrcWithoutWindowsExt(file, rt)
if err != nil {
return "", err
}
if !isWindows(rt.GOOS) {
return s, nil
}
return p.completeWindowsExtToFileSrc(s), nil
}
func (p *Package) fileSrcWithoutWindowsExt(file *registry.File, rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
if pkgInfo.Type == "cargo" {
return filepath.Join("bin", file.Name), nil
}
assetName, err := p.RenderAsset(rt)
if err != nil {
return "", fmt.Errorf("render the asset name: %w", err)
}
if unarchive.IsUnarchived(pkgInfo.GetFormat(), assetName) {
return filepath.Base(assetName), nil
}
if file.Src == "" {
return file.Name, nil
}
src, err := p.renderSrc(assetName, file, rt)
if err != nil {
return "", fmt.Errorf("render the template file.src: %w", err)
}
return src, nil
}
const (
PkgInfoTypeGitHubRelease = "github_release"
PkgInfoTypeGitHubContent = "github_content"
PkgInfoTypeGitHubArchive = "github_archive"
PkgInfoTypeHTTP = "http"
PkgInfoTypeGoInstall = "go_install"
PkgInfoTypeGoBuild = "go_build"
PkgInfoTypeCargo = "cargo"
)
type Param struct {
GlobalConfigFilePaths []string
ConfigFilePath string
LogLevel string
File string
AQUAVersion string
AquaCommitHash string
RootDir string
PWD string
InsertFile string
LogColor string
Dest string
HomeDir string
OutTestData string
Limit int
MaxParallelism int
Args []string
Tags map[string]struct{}
ExcludedTags map[string]struct{}
DisableLazyInstall bool
OnlyLink bool
All bool
Global bool
Insert bool
SelectVersion bool
ShowVersion bool
ProgressBar bool
Deep bool
SkipLink bool
Pin bool
Prune bool
Checksum bool
RequireChecksum bool
EnforceChecksum bool
EnforceRequireChecksum bool
DisablePolicy bool
Detail bool
OnlyPackage bool
OnlyRegistry bool
CosignDisabled bool
SLSADisabled bool
Installed bool
PolicyConfigFilePaths []string
Commands []string
}
func appendExt(s, format string) string {
if _, f := asset.RemoveExtFromAsset(s); f != "raw" {
return s
}
if format == formatRaw || format == "" {
return s
}
if strings.HasSuffix(s, "."+format) {
return s
}
return fmt.Sprintf("%s.%s", s, format)
}
func (p *Package) renderAsset(rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
switch pkgInfo.Type {
case PkgInfoTypeGitHubArchive, PkgInfoTypeGoBuild:
return "", nil
case PkgInfoTypeGoInstall:
return path.Base(pkgInfo.GetFiles()[0].Name), nil
case PkgInfoTypeGitHubContent:
s, err := p.RenderTemplateString(pkgInfo.Path, rt)
if err != nil {
return "", fmt.Errorf("render a package path: %w", err)
}
return s, nil
case PkgInfoTypeGitHubRelease:
return p.RenderTemplateString(pkgInfo.Asset, rt)
case PkgInfoTypeHTTP:
uS, err := p.RenderURL(rt)
if err != nil {
return "", fmt.Errorf("render URL: %w", err)
}
u, err := url.Parse(uS)
if err != nil {
return "", fmt.Errorf("parse the URL: %w", err)
}
return path.Base(u.Path), nil
}
return "", nil
}
func (p *Package) renderChecksumFile(asset string, rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
pkg := p.Package
tpl, err := template.Compile(pkgInfo.Checksum.Asset)
if err != nil {
return "", fmt.Errorf("parse a template: %w", err)
}
replacements := pkgInfo.GetChecksumReplacements()
uS, err := template.ExecuteTemplate(tpl, map[string]interface{}{
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, replacements, rt),
"Format": pkgInfo.GetFormat(),
"Asset": asset,
})
if err != nil {
return "", fmt.Errorf("render a template: %w", err)
}
return uS, nil
}
func (p *Package) renderTemplate(tpl *texttemplate.Template, rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
pkg := p.Package
uS, err := template.ExecuteTemplate(tpl, map[string]interface{}{
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
"Format": pkgInfo.GetFormat(),
})
if err != nil {
return "", fmt.Errorf("render a template: %w", err)
}
return uS, nil
}
func (p *Package) semVer() string {
v := p.Package.Version
prefix := p.PackageInfo.VersionPrefix
if prefix == "" {
return v
}
return strings.TrimPrefix(v, prefix)
}
func (p *Package) RenderDir(file *registry.File, rt *runtime.Runtime) (string, error) {
pkgInfo := p.PackageInfo
pkg := p.Package
return template.Execute(file.Dir, map[string]interface{}{ //nolint:wrapcheck
"Version": pkg.Version,
"SemVer": p.semVer(),
"GOOS": rt.GOOS,
"GOARCH": rt.GOARCH,
"OS": replace(rt.GOOS, pkgInfo.Replacements),
"Arch": getArch(pkgInfo.Rosetta2, pkgInfo.WindowsARMEmulation, pkgInfo.Replacements, rt),
"Format": pkgInfo.GetFormat(),
"FileName": file.Name,
})
}