This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
deplist.go
284 lines (247 loc) · 6.44 KB
/
deplist.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
package deplist
import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mcoops/deplist/internal/scan"
"github.com/mcoops/deplist/internal/utils"
)
// enums start at 1 to allow us to specify found languages 0 = nil
const (
LangGolang = 1 << iota
LangJava
LangNodeJS
LangPython
LangRuby
)
func init() {
// check for the library required binaries
if _, err := exec.LookPath("yarn"); err != nil {
log.Fatal("yarn is required in PATH")
}
if _, err := exec.LookPath("npm"); err != nil {
log.Fatal("npm is required in PATH")
}
if _, err := exec.LookPath("go"); err != nil {
log.Fatal("go is required")
}
if _, err := exec.LookPath("mvn"); err != nil {
log.Fatal("maven is required")
}
if _, err := exec.LookPath("bundle"); err != nil {
log.Fatal("bundler gem is required")
}
}
// GetLanguageStr returns from a bitmask return the ecosystem name
func GetLanguageStr(bm Bitmask) string {
if bm&LangGolang != 0 {
return "go"
} else if bm&LangJava != 0 {
return "mvn"
} else if bm&LangNodeJS != 0 {
return "npm"
} else if bm&LangPython != 0 {
return "pypi"
} else if bm&LangRuby != 0 {
return "gem"
}
return "unknown"
}
// GetDeps scans a given repository and returns all dependencies found in a DependencyList struct.
func GetDeps(fullPath string) ([]Dependency, Bitmask, error) {
// var deps DependencyList
var deps []Dependency
var foundTypes Bitmask = 0
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return nil, 0, os.ErrNotExist
}
pomPath := filepath.Join(fullPath, "pom.xml")
goPath := filepath.Join(fullPath, "go.mod")
goPkgPath := filepath.Join(fullPath, "Gopkg.lock")
glidePath := filepath.Join(fullPath, "glide.lock")
rubyPath := filepath.Join(fullPath, "Gemfile.lock")
pythonPath := filepath.Join(fullPath, "requirements.txt")
// point at the parent repo, but can't assume where the indicators will be
err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
// prevent panic by handling failure https://golang.org/pkg/path/filepath/#Walk
return err
}
if info.IsDir() {
// prevent walking down the vendors, docs, etc
if utils.BelongsToIgnoreList(info.Name()) {
return filepath.SkipDir
}
} else {
// Two checks, one for filenames and the second switch for full
// paths. Useful if we're looking for top of repo
switch filename := info.Name(); filename {
// for now only go for yarn and npm
case "package-lock.json":
// if theres not a yarn.lock fall thru
if _, err := os.Stat(
filepath.Join(
filepath.Dir(path),
"yarn.lock")); err == nil {
return nil
}
fallthrough
case "yarn.lock":
pkgs, err := scan.GetNodeJSDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangNodeJS)
}
for _, p := range pkgs {
deps = append(deps,
Dependency{
DepType: LangNodeJS,
Path: p.Name,
Version: p.Version,
Files: []string{},
})
}
default:
ext := filepath.Ext(filename)
// java
if ext == ".jar" || ext == ".war" || ext == ".ear" || ext == ".adm" || ext == ".hpi" || ext == ".zip" {
file := strings.Replace(filepath.Base(path), ext, "", 1) // get filename, check if we can ignore
if strings.HasSuffix(file, "-sources") || strings.HasSuffix(file, "-javadoc") {
return nil
}
pkgs, err := scan.GetJarDeps(path)
if err == nil {
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangJava)
}
for name, version := range pkgs {
// just in case we report the full path to the dep
name = strings.Replace(name, fullPath, "", 1)
// if the dep ends with -javadoc or -sources, not really interested
if !strings.HasSuffix(version, "-javadoc") && !strings.HasSuffix(version, "-sources") {
deps = append(deps,
Dependency{
DepType: LangJava,
Path: name,
Version: version,
Files: []string{},
})
}
}
}
}
}
switch path {
case goPath: // just support the top level go.mod for now
pkgs, err := scan.GetGolangDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangGolang)
}
for path, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: path,
Files: goPkg.Gofiles,
Version: goPkg.Version,
}
deps = append(deps, d)
}
case goPkgPath:
pkgs, err := scan.GetGoPkgDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangGolang)
}
for _, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: goPkg.Name,
Version: goPkg.Version,
}
deps = append(deps, d)
}
case glidePath:
pkgs, err := scan.GetGlideDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangGolang)
}
for _, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: goPkg.Name,
Version: goPkg.Version,
}
deps = append(deps, d)
}
case pomPath:
pkgs, err := scan.GetMvnDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangJava)
}
for name, version := range pkgs {
deps = append(deps,
Dependency{
DepType: LangJava,
Path: name,
Version: strings.Replace(version, "v", "", 1),
Files: []string{},
})
}
case rubyPath:
pkgs, err := scan.GetRubyDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangRuby)
}
for name, version := range pkgs {
deps = append(deps,
Dependency{
DepType: LangRuby,
Path: strings.TrimSuffix(name, "\n"),
Version: strings.Replace(version, "v", "", 1),
Files: []string{},
})
}
case pythonPath:
pkgs, err := scan.GetPythonDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
foundTypes.DepFoundAddFlag(LangPython)
}
for name, version := range pkgs {
deps = append(deps,
Dependency{
DepType: LangPython,
Path: name,
Version: version,
Files: []string{},
})
}
}
}
return nil
})
if err != nil {
return nil, 0, err // should't matter
}
return deps, foundTypes, nil
}