-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
which.go
166 lines (150 loc) · 4.67 KB
/
which.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
package which
import (
"context"
"fmt"
"github.com/aquaproj/aqua/v2/pkg/checksum"
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)
type FindResult struct {
Package *config.Package
File *registry.File
Config *aqua.Config
ExePath string
ConfigFilePath string
EnableChecksum bool
}
func (c *Controller) Which(ctx context.Context, logE *logrus.Entry, param *config.Param, exeName string) (*FindResult, error) {
for _, cfgFilePath := range c.configFinder.Finds(param.PWD, param.ConfigFilePath) {
findResult, err := c.findExecFile(ctx, logE, param, cfgFilePath, exeName)
if err != nil {
return nil, err
}
if findResult != nil {
return findResult, nil
}
}
for _, cfgFilePath := range param.GlobalConfigFilePaths {
logE := logE.WithField("config_file_path", cfgFilePath)
logE.Debug("checking a global configuration file")
if _, err := c.fs.Stat(cfgFilePath); err != nil {
continue
}
findResult, err := c.findExecFile(ctx, logE, param, cfgFilePath, exeName)
if err != nil {
return nil, err
}
if findResult != nil {
return findResult, nil
}
}
if exePath := c.lookPath(c.osenv.Getenv("PATH"), exeName); exePath != "" {
return &FindResult{
ExePath: exePath,
}, nil
}
return nil, logerr.WithFields(errCommandIsNotFound, logrus.Fields{ //nolint:wrapcheck
"exe_name": exeName,
"doc": "https://aquaproj.github.io/docs/reference/codes/004",
})
}
func (c *Controller) getExePath(findResult *FindResult) (string, error) {
pkg := findResult.Package
file := findResult.File
if pkg.Package.Version == "" {
return "", errVersionIsRequired
}
return pkg.ExePath(c.rootDir, file, c.runtime) //nolint:wrapcheck
}
func (c *Controller) findExecFile(ctx context.Context, logE *logrus.Entry, param *config.Param, cfgFilePath, exeName string) (*FindResult, error) {
cfg := &aqua.Config{}
if err := c.configReader.Read(logE, cfgFilePath, cfg); err != nil {
return nil, err //nolint:wrapcheck
}
var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return nil, err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return nil, fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
}
registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
return nil, err //nolint:wrapcheck
}
for _, pkg := range cfg.Packages {
if findResult := c.findExecFileFromPkg(registryContents, exeName, pkg, logE); findResult != nil {
findResult.Config = cfg
findResult.ConfigFilePath = cfgFilePath
findResult.Package.Registry = cfg.Registries[pkg.Registry]
return findResult, nil
}
}
return nil, nil //nolint:nilnil
}
func (c *Controller) findExecFileFromPkg(registries map[string]*registry.Config, exeName string, pkg *aqua.Package, logE *logrus.Entry) *FindResult { //nolint:cyclop
if pkg.Registry == "" || pkg.Name == "" {
logE.Debug("ignore a package because the package name or package registry name is empty")
return nil
}
logE = logE.WithFields(logrus.Fields{
"registry_name": pkg.Registry,
"package_name": pkg.Name,
})
registry, ok := registries[pkg.Registry]
if !ok {
logE.Warn("registry isn't found")
return nil
}
m := registry.PackageInfos.ToMap(logE)
pkgInfo, ok := m[pkg.Name]
if !ok {
logE.Warn("package isn't found")
return nil
}
pkgInfo, err := pkgInfo.Override(logE, pkg.Version, c.runtime)
if err != nil {
logerr.WithError(logE, err).Warn("version constraint is invalid")
return nil
}
supported, err := pkgInfo.CheckSupported(c.runtime, c.runtime.GOOS+"/"+c.runtime.GOARCH)
if err != nil {
logerr.WithError(logE, err).Error("check if the package is supported")
return nil
}
if !supported {
logE.Debug("the package isn't supported on this environment")
return nil
}
for _, file := range pkgInfo.GetFiles() {
if file.Name == exeName {
findResult := &FindResult{
Package: &config.Package{
Package: pkg,
PackageInfo: pkgInfo,
},
File: file,
}
exePath, err := c.getExePath(findResult)
if err != nil {
logE.WithError(err).Error("get the execution file path")
return nil
}
findResult.ExePath = exePath
return findResult
}
}
return nil
}