-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_env.go
47 lines (38 loc) · 988 Bytes
/
go_env.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
package report
import (
"encoding/json"
"fmt"
"github.com/lucor/goinfo"
"golang.org/x/sys/execabs"
)
// GoEnv collects the info about the Go environment using the go env tool
type GoEnv struct {
Filter []string // Filter defines a subset of vars to returns. Leave empty to return all.
}
// Summary return the summary
func (i *GoEnv) Summary() string {
return "Go environment info"
}
// Info returns the collected info
func (i *GoEnv) Info() (goinfo.Info, error) {
var info goinfo.Info
cmd := execabs.Command("go", "env", "-json")
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("could not detect go env info: %w", err)
}
err = json.Unmarshal(out, &info)
if err != nil {
return nil, fmt.Errorf("could not decode the go env json info: %w", err)
}
if len(i.Filter) == 0 {
return info, nil
}
filteredInfo := goinfo.Info{}
for _, key := range i.Filter {
if v, ok := info[key]; ok {
filteredInfo[key] = v
}
}
return filteredInfo, nil
}