-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports.go
59 lines (50 loc) · 1.39 KB
/
imports.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
package gvtunused
import (
"log"
"os/exec"
"strings"
"github.com/pkg/errors"
)
func UsedDependencies() ([]string, error) {
log.Println(" ↳ Looking in the sources")
cmd := exec.Command("go", "list", "-f", "{{ join .Deps \"\\n\" }}", ".")
deps, err := GoListImports(cmd)
if err != nil {
return nil, errors.Wrap(err, "cmd0")
}
log.Println(" ↳ Looking in the test sources")
cmd = exec.Command("go", "list", "-f", "{{ join .TestImports \"\\n\" }}", ".")
testDeps, err := GoListImports(cmd)
if err != nil {
return nil, errors.Wrap(err, "cmd0")
}
deps = append(deps, testDeps...)
log.Println(" ↳ Filtering non-standard imports...")
var used []string
for _, d := range deps {
cmd = exec.Command("go", "list", "-f", "{{if .Standard}}{{else}}{{.ImportPath}}{{end}}", d)
imps, err := GoListImports(cmd)
if err != nil {
return nil, err
}
used = append(used, imps...)
}
rootPath, err := projectRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to find project root path")
}
var importPaths []string
for _, u := range used {
importPath := strings.Replace(u, rootPath, "", -1)
importPaths = append(importPaths, importPath)
}
return importPaths, nil
}
func projectRoot() (string, error) {
cmd := exec.Command("go", "list", "./...")
imps, err := GoListImports(cmd)
if err != nil {
return "", errors.Wrap(err, "failed to list imports")
}
return imps[0], nil
}