Repository navigation
Expand file tree
/
Copy pathgo_packages.go
More file actions
282 lines (243 loc) · 6.61 KB
/
Copy pathgo_packages.go
File metadata and controls
282 lines (243 loc) · 6.61 KB
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
package fest
import "github.com/emad-elsaid/types"
import (
"bufio"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
)
const ResourceGoPackages ResourceName = "Go packages"
var (
wantedGoPackages []string
goListCache listCache
)
type goPackages struct{}
func (g goPackages) Wanted() []string { return wantedGoPackages }
func (g goPackages) ResourceName() string { return string(ResourceGoPackages) }
func (g goPackages) Match(want, have string) bool {
return matchWithVersion(want, have, splitVer)
}
func (g goPackages) getGoBin() (string, error) {
if gobin := os.Getenv("GOBIN"); gobin != "" {
return gobin, nil
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
gopath = filepath.Join(home, "go")
}
// The go command derives the default bin dir from the first entry of a
// path-list GOPATH (e.g. GOPATH=/a:/b -> /a/bin).
gopath = strings.Split(gopath, string(os.PathListSeparator))[0]
return filepath.Join(gopath, "bin"), nil
}
func (g goPackages) getBinaryModule(binPath string) (string, error) {
stdout, err := types.Cmd("go", "version", "-m", binPath).StdoutErr()
if err != nil {
return "", err
}
var path, version string
scanner := bufio.NewScanner(strings.NewReader(stdout))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
switch parts[0] {
case "path":
path = parts[1]
case "mod":
if len(parts) >= 3 {
version = parts[2]
}
}
}
if path == "" {
return "", fmt.Errorf("no valid module path found")
}
if version == "(devel)" || version == "" {
version = "latest"
}
return path + "@" + version, nil
}
func (g goPackages) ListInstalled() ([]string, error) {
return goListCache.getOrSet(func() ([]string, error) {
if _, err := types.Cmd("go", "version").StdoutErr(); err != nil {
slog.Debug("go is not installed or not available")
return []string{}, nil
}
goBin, err := g.getGoBin()
if err != nil {
return nil, err
}
entries, err := os.ReadDir(goBin)
if os.IsNotExist(err) {
return []string{}, nil
}
if err != nil {
return nil, err
}
var installed []string
for _, entry := range entries {
if entry.IsDir() {
continue
}
binPath := filepath.Join(goBin, entry.Name())
modulePath, err := g.getBinaryModule(binPath)
if err != nil {
continue
}
installed = append(installed, modulePath)
}
return installed, nil
})
}
func (g goPackages) ListExplicit() ([]string, error) {
return g.ListInstalled()
}
func (g goPackages) Install(pkgs []string) error {
defer goListCache.invalidate()
if _, err := types.Cmd("go", "version").StdoutErr(); err != nil {
slog.Warn("go is not installed, skipping Go package installation")
return nil
}
for _, pkg := range pkgs {
installPkg := pkg
if !strings.Contains(pkg, "@") {
installPkg = pkg + "@latest"
}
slog.Info("Installing Go package", "package", installPkg)
if err := types.Cmd("go", "install", installPkg).Interactive().Error(); err != nil {
return err
}
}
return nil
}
func (g goPackages) Uninstall(pkgs []string) error {
defer goListCache.invalidate()
if _, err := types.Cmd("go", "version").StdoutErr(); err != nil {
return nil
}
goBin, err := g.getGoBin()
if err != nil {
return err
}
entries, err := os.ReadDir(goBin)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
// Build a map of module paths to binary names
moduleToBinary := make(map[string]string)
for _, entry := range entries {
if entry.IsDir() {
continue
}
binPath := filepath.Join(goBin, entry.Name())
modulePath, err := g.getBinaryModule(binPath)
if err != nil {
continue
}
moduleToBinary[modulePath] = entry.Name()
}
// Uninstall each package by finding its binary
for _, pkg := range pkgs {
// Check if this exact module path matches a binary
if binaryName, found := moduleToBinary[pkg]; found {
binPath := filepath.Join(goBin, binaryName)
slog.Info("Removing Go binary", "binary", binPath, "module", pkg)
if err := os.Remove(binPath); err != nil && !os.IsNotExist(err) {
return err
}
continue
}
// If not found, try matching using our Match function (version flexibility)
for modulePath, binaryName := range moduleToBinary {
if g.Match(pkg, modulePath) {
binPath := filepath.Join(goBin, binaryName)
slog.Info("Removing Go binary", "binary", binPath, "module", modulePath)
if err := os.Remove(binPath); err != nil && !os.IsNotExist(err) {
return err
}
}
}
}
return nil
}
func (g goPackages) MarkExplicit([]string) error { return nil }
func (g goPackages) GetDependencies() (map[string][]string, error) { return nil, nil }
func (g goPackages) Update() error {
defer goListCache.invalidate()
if _, err := types.Cmd("go", "version").StdoutErr(); err != nil {
slog.Warn("go is not installed, skipping Go package update")
return nil
}
var toUpdate []string
for _, pkg := range g.Wanted() {
if _, ver := splitVer(pkg); isVersionLocked(ver) {
slog.Debug("skipping version-locked Go package", "package", pkg)
continue
}
toUpdate = append(toUpdate, pkg)
}
if len(toUpdate) == 0 {
slog.Info("No Go packages to update")
return nil
}
slog.Info("Updating Go packages", "count", len(toUpdate))
for _, pkg := range toUpdate {
installPkg := pkg
if !strings.Contains(pkg, "@") {
installPkg = pkg + "@latest"
}
slog.Info("Updating Go package", "package", installPkg)
if err := types.Cmd("go", "install", installPkg).Interactive().Error(); err != nil {
return err
}
}
return nil
}
func (g goPackages) SaveAsGo(wanted []string) error {
installed, err := g.ListExplicit()
if err != nil {
return err
}
diff := subtract(g, installed, wanted)
if len(diff) == 0 {
logSuccess("No new go packages to save")
return nil
}
if err := saveAsGoFile("go_packages.go", "GoPackage", diff); err != nil {
return err
}
logSuccess("go packages saved", "file", "go_packages.go", "count", len(diff))
return nil
}
// GoPackage declares Go packages to install using `go install`.
// Packages are installed to GOBIN or $GOPATH/bin.
// Supports version pinning using @version syntax.
//
// Example:
//
// fest.GoPackage(
// "github.com/golangci/golangci-lint/cmd/golangci-lint@latest",
// "golang.org/x/tools/cmd/goimports@v0.15.0",
// )
func GoPackage(pkgs ...string) {
normalized := make([]string, 0, len(pkgs))
for _, pkg := range pkgs {
if strings.HasSuffix(pkg, "@(devel)") {
pkg = strings.TrimSuffix(pkg, "@(devel)") + "@latest"
}
normalized = append(normalized, pkg)
}
addUnique(&wantedGoPackages, normalized...)
}