-
Notifications
You must be signed in to change notification settings - Fork 2
/
go_list.go
90 lines (79 loc) · 2.07 KB
/
go_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package internal
import (
"bytes"
"encoding/json"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
)
func NewGoListExecutor(useCache bool, cacheFilePath string) (*GoListExecutor, error) {
var cache modulesCache
if useCache {
var err error
cache, err = NewCache(cacheFilePath)
if err != nil {
return nil, err
}
}
return &GoListExecutor{cache: cache}, nil
}
type GoListExecutor struct {
cache modulesCache
}
func (e *GoListExecutor) GetVersions(path string) ([]*semver.Version, error) {
out, err := e.exec("-versions", path)
if err != nil {
return nil, err
}
var versions struct {
Path string `json:"Path"`
Versions []*semver.Version `json:"Versions"`
}
if err = json.NewDecoder(out).Decode(&versions); err != nil {
return nil, err
}
return versions.Versions, nil
}
func (e *GoListExecutor) GetInfo(path string, version *semver.Version) (*Module, error) {
return e.getInfo(path, version, false)
}
func (e *GoListExecutor) GetLatestInfo(path string) (*Module, error) {
return e.getInfo(path, nil, true)
}
// Fetch module details.
func (e *GoListExecutor) getInfo(path string, version *semver.Version, latest bool) (*Module, error) {
var versionStr string
if latest {
versionStr = "latest"
} else {
versionStr = "v" + version.String()
}
// Try loading from cache.
if version != nil && e.cache != nil {
m, loaded := e.cache.Load(path, version)
if loaded {
return m, nil
}
}
// Fetch module details.
out, err := e.exec(path + "@" + versionStr)
if err != nil {
return nil, err
}
var m Module
if err = json.NewDecoder(out).Decode(&m); err != nil {
return nil, err
}
// Save to cache.
if e.cache != nil {
if err = e.cache.Save(&m); err != nil {
return nil, err
}
}
return &m, nil
}
func (e *GoListExecutor) GetModFile(_ string, _ *semver.Version) ([]byte, error) {
return nil, errors.New("retrieving go.mod file using GoListExecutor is not supported")
}
func (e *GoListExecutor) exec(args ...string) (*bytes.Buffer, error) {
return execCmd("go", append([]string{"list", "-json", "-m", "-mod=readonly"}, args...)...)
}