-
Notifications
You must be signed in to change notification settings - Fork 11
/
golist.go
59 lines (53 loc) · 893 Bytes
/
golist.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
package main
import (
"bytes"
"encoding/json"
"time"
)
type (
Package struct {
Path string
Version string
Time time.Time
}
Module struct {
Package
Update *Package
Indirect bool
Dir string
GoMod string
GoVersion string
}
)
func getDepPackages() ([]Module, error) {
output, err := getCmdOutput("go", "list", "-u", "-m", "-json", "-mod=readonly", "all")
if err != nil {
return nil, err
}
var modules []Module
var b bytes.Buffer
var depth int32
for _, ch := range output {
switch ch {
case '{':
depth++
b.WriteByte(ch)
case '}':
depth--
if depth == 0 {
b.WriteByte(ch)
var m Module
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
return nil, err
}
b.Reset()
modules = append(modules, m)
} else {
b.WriteByte(ch)
}
default:
b.WriteByte(ch)
}
}
return modules, nil
}