forked from nathanjcochran/upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
73 lines (66 loc) · 2.3 KB
/
list.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"time"
)
func list(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "go", "list", "-mod=mod", "./...")
if err := cmd.Run(); err != nil {
if err := err.(*exec.ExitError); err != nil {
fmt.Println(string(err.Stderr)) // TODO: Remove
}
return fmt.Errorf("error executing 'go list' command: %s", err)
}
return nil
}
// From "go help list" output
type Module struct {
Path string // module path
Query string // version query corresponding to this version
Version string // module version
Versions []string // available module versions
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update (with -u)
Main bool // is this the main module?
Indirect bool // module is only indirectly needed by main module
Dir string // directory holding local copy of files, if any
GoMod string // path to go.mod file describing module, if any
GoVersion string // go version used in module
Retracted []string // retraction information, if any (with -retracted or -u)
Deprecated string // deprecation message, if any (with -u)
Error *ModuleError // error loading module
Origin any // provenance of module
Reuse bool // reuse of old module info is safe
}
type ModuleError struct {
Err string // the error itself
}
func listModules(ctx context.Context, modulePaths ...string) ([]Module, error) {
cmd := exec.CommandContext(ctx,
"go", append([]string{"list", "-m", "-u", "-e", "-json", "-mod=readonly"},
modulePaths...,
)...,
)
out, err := cmd.Output()
if err != nil {
if err := err.(*exec.ExitError); err != nil {
fmt.Println(string(err.Stderr)) // TODO: Remove
}
return nil, fmt.Errorf("error executing 'go list -m -u -e -json -mod=readonly' command: %s", err)
}
var results []Module
decoder := json.NewDecoder(bytes.NewReader(out))
for decoder.More() {
var result Module
if err := decoder.Decode(&result); err != nil {
return nil, fmt.Errorf("error parsing results of 'go list -m -u -e -json -mod=readonly' command: %s", err)
}
results = append(results, result)
}
return results, nil
}