-
Notifications
You must be signed in to change notification settings - Fork 4
/
tidy.go
155 lines (128 loc) · 2.71 KB
/
tidy.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
// Package gomodrun is the forgotten go tool that executes and caches binaries included in go.mod files.
// This makes it easy to version cli tools in your projects such as `golangci-lint`
// and `ginkgo` that are versioned locked to what you specify in `go.mod`.
// Binaries are cached by go version and package version.
package gomodrun
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
func cleanEmptyDirectory(root string) error {
dirs := []string{}
err := filepath.Walk(root, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
dirs = append(dirs, filePath)
}
return nil
})
if err != nil {
return err
}
for i := len(dirs) - 1; i >= 0; i-- {
files, readErr := ioutil.ReadDir(dirs[i])
if readErr != nil {
return err
}
if len(files) != 0 {
continue
}
err = os.Remove(dirs[i])
if err != nil {
return err
}
}
return err
}
func getAllBins(root string) ([]string, error) {
binPaths := []string{}
err := filepath.Walk(root, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
binPaths = append(binPaths, filePath)
}
return nil
})
return binPaths, err
}
// Tidy cleans .gomodrun of any outdated binaries.
func Tidy(pkgRoot string) error {
var err error
if pkgRoot == "" {
pkgRoot, err = GetPkgRoot()
if err != nil {
return err
}
}
gmrRoot := path.Join(pkgRoot, ".gomodrun")
if _, err = os.Stat(gmrRoot); os.IsNotExist(err) {
return nil
}
goVersion, err := getGoVersion()
if err != nil {
return err
}
gmrRootFiles, err := ioutil.ReadDir(gmrRoot)
if err != nil {
return err
}
for _, file := range gmrRootFiles {
if file.Name() != goVersion {
err = os.RemoveAll(path.Join(gmrRoot, file.Name()))
if err != nil {
return err
}
}
}
binPaths, err := getAllBins(gmrRoot)
if err != nil {
return err
}
if len(binPaths) == 0 {
return nil
}
pkg, err := getToolsPkg(pkgRoot)
if err != nil {
return err
}
mod, err := getGoMod(pkgRoot)
if err != nil {
return err
}
versionedImports := []string{}
for _, modulePath := range pkg.Imports {
for _, req := range mod.Require {
if strings.HasPrefix(modulePath, req.Mod.Path) {
versionedImports = append(versionedImports, req.Mod.Path+"@"+req.Mod.Version)
break
}
}
}
for _, binPath := range binPaths {
validBin := false
for _, versionedImport := range versionedImports {
if strings.Contains(binPath, versionedImport) {
validBin = true
break
}
}
if !validBin {
err = os.Remove(binPath)
if err != nil {
return err
}
}
}
err = cleanEmptyDirectory(pkgRoot)
if err != nil {
return err
}
return nil
}