sweet/common: invoke go env with -json flag The go command supports the -json flag to make it convenient to parse its output programmatically. Since human-readable output isn't needed here, switch to using the -json flag. Change-Id: I2e25a96a020cff5d8a99c152bd929f06f6920c42 Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/758680 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Michael Matloob <matloob@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/sweet/common/gotool.go b/sweet/common/gotool.go index 086f439..4248292 100644 --- a/sweet/common/gotool.go +++ b/sweet/common/gotool.go
@@ -5,7 +5,7 @@ package common import ( - "bytes" + "encoding/json" "fmt" "io" "os" @@ -78,15 +78,20 @@ return g.Do("", "build", "-o", out, pkg) } +// Version returns the 'go env GOVERSION' value by invoking the go command. func (g *Go) Version() (string, error) { - cmd := exec.Command(g.Tool, "env", "GOVERSION") + cmd := exec.Command(g.Tool, "env", "-json", "GOVERSION") cmd.Env = g.Env.Collapse() log.TraceCommand(cmd, false) out, err := cmd.Output() if err != nil { - return "", fmt.Errorf("error running 'go env GOVERSION': %w", err) + return "", fmt.Errorf("error running 'go env -json GOVERSION': %w", err) } - return string(bytes.TrimSpace(out)), nil + var env struct{ GOVERSION string } + if err := json.Unmarshal(out, &env); err != nil { + return "", fmt.Errorf("error JSON unmarshaling output from 'go env -json GOVERSION': %v", err) + } + return env.GOVERSION, nil } func (g *Go) BuildPath(path, out string, args ...string) error {