-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_bin.go
More file actions
449 lines (417 loc) · 12.7 KB
/
cmd_bin.go
File metadata and controls
449 lines (417 loc) · 12.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main
import (
"context"
"fmt"
"os"
"strings"
binpkg "github.com/pun/vex/internal/bin"
)
// --- helpers ---
func lookupManagedTool(name string) (binpkg.ToolSpec, error) {
spec, ok := binpkg.GetTool(name)
if !ok {
return binpkg.ToolSpec{}, fmt.Errorf("unknown managed tool %q (available: %s)", name, supportedToolsString())
}
return spec, nil
}
func supportedToolsString() string {
tools := binpkg.AllTools()
names := make([]string, 0, len(tools))
for _, tool := range tools {
names = append(names, tool.Name)
}
return strings.Join(names, ", ")
}
func yesNo(value bool) string {
if value {
return "yes"
}
return "no"
}
func truncate(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen-3] + "..."
}
func toolStatusSummary(status *binpkg.ToolStatus, installedVersion, latestVersion string) string {
if !status.Available {
if status.UnavailableReason != "" {
return "unavailable: " + status.UnavailableReason
}
return "unavailable"
}
if status.ResolutionExactVersion && status.VersionChangeRequired && installedVersion != "unknown" && latestVersion != "unknown" {
switch comparison := binpkg.CompareVersions(installedVersion, latestVersion); {
case comparison > 0:
return "compatibility downgrade required"
case comparison < 0:
return "update required for compat stack"
default:
return "version alignment required"
}
}
if status.LatestVersion != "" && installedVersion != "unknown" {
if status.UpdateAvailable {
return "update available"
}
return "up to date"
}
if status.Exists {
return "installed"
}
return "not installed"
}
// --- install ---
func cmdBinInstall(toolName string, force bool) {
spec, err := lookupManagedTool(toolName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
result, err := binpkg.InstallTool(context.Background(), spec, force)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Printf("✓ Installed %s %s to %s\n", spec.Name, result.State.InstalledVersion, result.State.Path)
}
// --- ls ---
func cmdBinLs() {
tools := binpkg.AllTools()
if len(tools) == 0 {
fmt.Println("No managed binaries are configured")
return
}
fmt.Printf("%-12s %-12s %-12s %s\n", "TOOL", "STATE", "VERSION", "DETAIL")
for _, spec := range tools {
status, err := binpkg.LocalToolStatus(spec)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
state := "available"
detail := status.Path
switch {
case !status.Available:
state = "unavailable"
detail = status.UnavailableReason
case status.Managed && status.Exists:
state = "installed"
case status.Managed && !status.Exists:
state = "missing"
case !status.Managed && status.Exists:
state = "unmanaged"
}
if detail == "" {
detail = "-"
}
version := status.EffectiveInstalledVersion()
if version == "" {
version = "-"
}
fmt.Printf("%-12s %-12s %-12s %s\n",
spec.Name,
state,
version,
truncate(detail, 60),
)
}
}
// --- status ---
func cmdBinStatus(toolName string) {
spec, err := lookupManagedTool(toolName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
status, err := binpkg.InspectTool(context.Background(), spec)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if status.LatestError != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to check latest version for %s: %v\n", spec.Name, status.LatestError)
}
installedVersion := status.EffectiveInstalledVersion()
if installedVersion == "" {
installedVersion = "unknown"
}
latestVersion := status.LatestVersion
if latestVersion == "" {
latestVersion = "unknown"
}
fmt.Printf("Tool: %s\n", spec.Name)
fmt.Printf("Managed: %s\n", yesNo(status.Managed))
fmt.Printf("Path: %s\n", status.Path)
fmt.Printf("Exists: %s\n", yesNo(status.Exists))
fmt.Printf("Executable: %s\n", yesNo(status.Executable))
fmt.Printf("Available: %s\n", yesNo(status.Available))
if !status.Available && status.UnavailableReason != "" {
fmt.Printf("Unavailable: %s\n", status.UnavailableReason)
}
fmt.Printf("Installed version: %s\n", installedVersion)
if status.RuntimeVersion != "" && status.StoredVersion != "" && status.RuntimeVersion != status.StoredVersion {
fmt.Printf("Stored version: %s\n", status.StoredVersion)
}
fmt.Printf("Latest version: %s\n", latestVersion)
if status.LatestReleaseTag != "" {
fmt.Printf("Latest tag: %s\n", status.LatestReleaseTag)
}
if status.ResolutionStrategy != "" {
fmt.Printf("Resolution: %s\n", status.ResolutionStrategy)
}
if status.ResolutionReason != "" {
fmt.Printf("Resolution note: %s\n", status.ResolutionReason)
}
if status.UpstreamLatestVersion != "" && status.UpstreamLatestVersion != latestVersion {
fmt.Printf("Upstream latest: %s\n", status.UpstreamLatestVersion)
if status.UpstreamLatestReleaseTag != "" {
fmt.Printf("Upstream tag: %s\n", status.UpstreamLatestReleaseTag)
}
if len(status.CompatibleTools) > 0 {
fmt.Printf("Capped by: %s\n", strings.Join(status.CompatibleTools, ", "))
}
}
if status.SelectedNushellMinor != "" {
fmt.Printf("Compat minor: %s.x\n", status.SelectedNushellMinor)
}
fmt.Printf("Status: %s\n", toolStatusSummary(status, installedVersion, latestVersion))
if status.Available && status.LatestVersion != "" && installedVersion != "unknown" {
if status.ResolutionExactVersion {
fmt.Printf("Change required: %s\n", yesNo(status.VersionChangeRequired))
} else {
fmt.Printf("Update available: %s\n", yesNo(status.UpdateAvailable))
}
}
}
// --- sync ---
func cmdBinSync(dryRun bool) {
ctx := context.Background()
tools := binpkg.AllTools()
var installed, updated, skipped, failed int
rateLimitFailures := githubRateLimitFailureCollector{}
for _, spec := range tools {
wasUpdate, wasSkipped, err := syncTool(ctx, spec, dryRun)
if err != nil {
failed++
if rateLimitFailures.Record(spec.Name, err) {
continue
}
fmt.Fprintf(os.Stderr, "✗ %s: %v\n", spec.Name, err)
continue
}
if wasSkipped {
skipped++
continue
}
if wasUpdate {
updated++
} else {
installed++
}
}
rateLimitFailures.PrintSummary()
fmt.Printf("\nSummary: %d installed, %d updated, %d skipped, %d failed%s\n", installed, updated, skipped, failed, rateLimitFailures.SummarySuffix())
if failed > 0 {
os.Exit(1)
}
}
func syncTool(ctx context.Context, spec binpkg.ToolSpec, dryRun bool) (bool, bool, error) {
status, err := binpkg.InspectTool(ctx, spec)
if err != nil {
return false, false, fmt.Errorf("failed to inspect %s: %w", spec.Name, err)
}
if !status.Available {
if dryRun {
fmt.Printf("• %s: would skip (%s)\n", spec.Name, status.UnavailableReason)
} else {
fmt.Printf("• %s: skipped (%s)\n", spec.Name, status.UnavailableReason)
}
return false, true, nil
}
if status.LatestError != nil {
return false, false, fmt.Errorf("failed to check latest version: %w", status.LatestError)
}
latestVersion := status.LatestVersion
if latestVersion == "" {
latestVersion = "unknown"
}
// Case 1: Not installed
if !status.Exists {
if dryRun {
fmt.Printf("• %s: would install %s (not present)\n", spec.Name, latestVersion)
return false, false, nil
}
installResult, err := binpkg.InstallTool(ctx, spec, true)
if err != nil {
return false, false, err
}
fmt.Printf("✓ %s: installed %s\n", spec.Name, installResult.State.InstalledVersion)
return false, false, nil
}
// Case 2: Unmanaged binary exists - take it over
if !status.Managed {
if dryRun {
fmt.Printf("• %s: would take over and install %s\n", spec.Name, latestVersion)
return true, false, nil
}
installResult, err := binpkg.InstallTool(ctx, spec, true)
if err != nil {
return false, false, err
}
fmt.Printf("✓ %s: took over and installed %s\n", spec.Name, installResult.State.InstalledVersion)
return true, false, nil
}
// Case 3: Managed and installed - update/re-install with force
installedVersion := status.EffectiveInstalledVersion()
if installedVersion == "" {
installedVersion = "unknown"
}
if dryRun {
switch {
case status.ResolutionExactVersion && status.VersionChangeRequired:
fmt.Printf("• %s: would align %s → %s (compat stack)\n", spec.Name, installedVersion, latestVersion)
case status.UpdateAvailable:
fmt.Printf("• %s: would update %s → %s\n", spec.Name, installedVersion, latestVersion)
default:
fmt.Printf("• %s: would re-install %s\n", spec.Name, latestVersion)
}
return true, false, nil
}
updateResult, err := binpkg.UpdateTool(ctx, spec, true)
if err != nil {
return false, false, err
}
if updateResult.Updated {
if updateResult.PreviousVersion != "" && updateResult.PreviousVersion != updateResult.State.InstalledVersion {
fmt.Printf("↑ %s: %s → %s\n", spec.Name, updateResult.PreviousVersion, updateResult.State.InstalledVersion)
} else {
fmt.Printf("↑ %s: re-installed %s\n", spec.Name, updateResult.State.InstalledVersion)
}
} else {
fmt.Printf("↑ %s: re-installed %s\n", spec.Name, updateResult.State.InstalledVersion)
}
return true, false, nil
}
// --- update ---
func cmdBinUpdate(toolName string, updateAll, force bool) {
if updateAll && toolName != "" {
fmt.Fprintln(os.Stderr, "Error: cannot specify a tool name together with --all")
os.Exit(1)
}
if !updateAll && toolName == "" {
fmt.Fprintln(os.Stderr, "Error: specify either a tool name or --all")
os.Exit(1)
}
ctx := context.Background()
if updateAll {
updateAllManagedTools(ctx, force)
return
}
spec, err := lookupManagedTool(toolName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
result, err := binpkg.UpdateTool(ctx, spec, force)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if !result.Updated {
fmt.Printf("%s is already up to date (%s)\n", spec.Name, result.State.InstalledVersion)
return
}
if result.PreviousVersion != "" {
fmt.Printf("✓ Updated %s from %s to %s\n", spec.Name, result.PreviousVersion, result.State.InstalledVersion)
} else {
fmt.Printf("✓ Installed latest %s (%s)\n", spec.Name, result.State.InstalledVersion)
}
}
func updateAllManagedTools(ctx context.Context, force bool) {
tools := binpkg.AllTools()
updated := 0
current := 0
skipped := 0
failed := 0
rateLimitFailures := githubRateLimitFailureCollector{}
for _, spec := range tools {
status, err := binpkg.LocalToolStatus(spec)
if err != nil {
failed++
fmt.Fprintf(os.Stderr, "✗ %s: %v\n", spec.Name, err)
continue
}
if !status.Managed {
continue
}
if !status.Available {
skipped++
fmt.Printf("• %s: skipped (%s)\n", spec.Name, status.UnavailableReason)
continue
}
result, err := binpkg.UpdateTool(ctx, spec, force)
if err != nil {
failed++
if rateLimitFailures.Record(spec.Name, err) {
continue
}
fmt.Fprintf(os.Stderr, "✗ %s: %v\n", spec.Name, err)
continue
}
if result.Updated {
updated++
if result.PreviousVersion != "" {
fmt.Printf("✓ %s: %s -> %s\n", spec.Name, result.PreviousVersion, result.State.InstalledVersion)
} else {
fmt.Printf("✓ %s: installed %s\n", spec.Name, result.State.InstalledVersion)
}
continue
}
current++
fmt.Printf("• %s: already up to date (%s)\n", spec.Name, result.State.InstalledVersion)
}
rateLimitFailures.PrintSummary()
fmt.Printf("Summary: %d updated, %d already current, %d skipped, %d failed%s\n", updated, current, skipped, failed, rateLimitFailures.SummarySuffix())
if failed > 0 {
os.Exit(1)
}
}
// --- version ---
func cmdBinVersion(toolName string) {
spec, err := lookupManagedTool(toolName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
status, err := binpkg.InspectTool(context.Background(), spec)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if status.LatestError != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to check latest version for %s: %v\n", spec.Name, status.LatestError)
}
installedVersion := status.EffectiveInstalledVersion()
if installedVersion == "" {
installedVersion = "unknown"
}
latestVersion := status.LatestVersion
if latestVersion == "" {
latestVersion = "unknown"
}
fmt.Printf("%s\n", spec.Name)
fmt.Printf(" installed: %s\n", installedVersion)
fmt.Printf(" latest: %s\n", latestVersion)
if !status.Available && status.UnavailableReason != "" {
fmt.Printf(" note: %s\n", status.UnavailableReason)
}
if status.ResolutionStrategy != "" {
fmt.Printf(" resolve: %s\n", status.ResolutionStrategy)
}
if status.UpstreamLatestVersion != "" && status.UpstreamLatestVersion != latestVersion {
fmt.Printf(" upstream: %s\n", status.UpstreamLatestVersion)
}
fmt.Printf(" status: %s\n", toolStatusSummary(status, installedVersion, latestVersion))
}