-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (101 loc) · 2.75 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os/exec"
"time"
"github.com/fatih/color"
"golang.org/x/mod/semver"
)
func main() {
var verbose bool
flag.BoolVar(&verbose, "v", false, "include modules that are current")
flag.Parse()
if verbose {
println("verbose...")
}
println("running go list...")
out, err := exec.Command("go", "list", "-json", "-u", "-m", "all").Output()
if err != nil {
log.Fatal(err)
}
src := bytes.NewReader(out)
decoder := json.NewDecoder(src)
for decoder.More() {
var m Module
if err := decoder.Decode(&m); err != nil {
log.Fatal(err)
}
if m.Error != nil {
color.Red(fmt.Sprintf("module: %s error: %s\n", m.Path, m.Error.Err))
continue
}
if m.Main { // don't report the main module
continue
}
if !m.Indirect {
printmodule(m, verbose)
}
}
}
// printmodule may print a module update information
func printmodule(m Module, verbose bool) {
major := false
msg := m.Path
if verbose || m.Update != nil {
msg += fmt.Sprintf(" => %s", m.Version)
if m.Time != nil {
msg += fmt.Sprintf(" (%s)", m.Time.Format("2006-01-02"))
}
}
if m.Update != nil {
msg += fmt.Sprintf(" => %s", m.Update.Version)
if m.Update.Time != nil {
msg += fmt.Sprintf(" (%s)", m.Update.Time.Format("2006-01-02"))
}
existing := semver.Major(m.Version)
new := semver.Major(m.Update.Version)
if existing != new {
major = true
}
}
if m.Update == nil {
if verbose {
color.Green(msg)
}
} else {
if major {
color.Red(msg)
} else {
color.Yellow(msg)
}
}
}
// Module is module in a repository. This structure comes from the GO docs
// as of 25 June 2023.
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
}
// ModuleError holds any errors loading the module
type ModuleError struct {
Err string // the error itself
}