-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_go.go
More file actions
67 lines (57 loc) · 1.55 KB
/
Copy pathadapter_go.go
File metadata and controls
67 lines (57 loc) · 1.55 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
package main
import (
"encoding/json"
"os/exec"
"strings"
)
// GoAdapter inspects Go ecosystem
type GoAdapter struct{}
func (a *GoAdapter) Name() string {
return "go"
}
func (a *GoAdapter) Inspect() (interface{}, error) {
info := &EcosystemInfo{
Metadata: make(map[string]string),
}
// Get Go version
cmd := exec.Command("go", "version")
if output, err := cmd.Output(); err == nil {
info.Version = strings.TrimSpace(string(output))
} else {
return nil, err
}
// Get GOPATH
cmd = exec.Command("go", "env", "GOPATH")
if output, err := cmd.Output(); err == nil {
info.Metadata["GOPATH"] = strings.TrimSpace(string(output))
}
// Get GOROOT
cmd = exec.Command("go", "env", "GOROOT")
if output, err := cmd.Output(); err == nil {
info.Metadata["GOROOT"] = strings.TrimSpace(string(output))
}
// Get Go modules mode
cmd = exec.Command("go", "env", "GO111MODULE")
if output, err := cmd.Output(); err == nil {
info.Metadata["GO111MODULE"] = strings.TrimSpace(string(output))
}
// Try to list installed Go tools (in GOPATH/bin or GOBIN)
cmd = exec.Command("go", "list", "-m", "-json", "all")
if output, err := cmd.Output(); err == nil {
// Parse Go modules
decoder := json.NewDecoder(strings.NewReader(string(output)))
for decoder.More() {
var mod struct {
Path string `json:"Path"`
Version string `json:"Version"`
}
if err := decoder.Decode(&mod); err == nil && mod.Version != "" {
info.Packages = append(info.Packages, PackageInfo{
Name: mod.Path,
Version: mod.Version,
})
}
}
}
return info, nil
}