1
0
mirror of https://github.com/golang/go synced 2024-09-29 03:24:29 -06:00

cmd/go/internal/bug: use envcmd instead of go env

Add the printGoEnv function to print the go environment variables, using
the envcmd package instead of invoking go env.

Add the PrintEnv function to the envcmd package, to avoid duplicating
code.

Updates #45803

Change-Id: I38d5b936c0ebb16e741ffbee4309b95d6d0ecc6c
Reviewed-on: https://go-review.googlesource.com/c/go/+/314230
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Manlio Perillo 2021-04-27 14:48:20 +02:00 committed by Bryan C. Mills
parent 5c69cb2a5b
commit 12af403624
2 changed files with 22 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/envcmd"
"cmd/go/internal/web"
)
@ -90,17 +91,20 @@ func printEnvDetails(w io.Writer) {
fmt.Fprintf(w, "### What operating system and processor architecture are you using (`go env`)?\n\n")
fmt.Fprintf(w, "<details><summary><code>go env</code> Output</summary><br><pre>\n")
fmt.Fprintf(w, "$ go env\n")
goexe, err := os.Executable()
if err != nil {
goexe = filepath.Join(runtime.GOROOT(), "bin/go")
}
printCmdOut(w, "", goexe, "env")
printGoEnv(w)
printGoDetails(w)
printOSDetails(w)
printCDetails(w)
fmt.Fprintf(w, "</pre></details>\n\n")
}
func printGoEnv(w io.Writer) {
env := envcmd.MkEnv()
env = append(env, envcmd.ExtraEnvVars()...)
env = append(env, envcmd.ExtraEnvVarsCostly()...)
envcmd.PrintEnv(w, env)
}
func printGoDetails(w io.Writer) {
printCmdOut(w, "GOROOT/bin/go version: ", filepath.Join(runtime.GOROOT(), "bin/go"), "version")
printCmdOut(w, "GOROOT/bin/go tool compile -V: ", filepath.Join(runtime.GOROOT(), "bin/go"), "tool", "compile", "-V")

View File

@ -10,6 +10,7 @@ import (
"encoding/json"
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"runtime"
@ -347,27 +348,32 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
return
}
PrintEnv(os.Stdout, env)
}
// PrintEnv prints the environment variables to w.
func PrintEnv(w io.Writer, env []cfg.EnvVar) {
for _, e := range env {
if e.Name != "TERM" {
switch runtime.GOOS {
default:
fmt.Printf("%s=\"%s\"\n", e.Name, e.Value)
fmt.Fprintf(w, "%s=\"%s\"\n", e.Name, e.Value)
case "plan9":
if strings.IndexByte(e.Value, '\x00') < 0 {
fmt.Printf("%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''"))
fmt.Fprintf(w, "%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''"))
} else {
v := strings.Split(e.Value, "\x00")
fmt.Printf("%s=(", e.Name)
fmt.Fprintf(w, "%s=(", e.Name)
for x, s := range v {
if x > 0 {
fmt.Printf(" ")
fmt.Fprintf(w, " ")
}
fmt.Printf("%s", s)
fmt.Fprintf(w, "%s", s)
}
fmt.Printf(")\n")
fmt.Fprintf(w, ")\n")
}
case "windows":
fmt.Printf("set %s=%s\n", e.Name, e.Value)
fmt.Fprintf(w, "set %s=%s\n", e.Name, e.Value)
}
}
}