]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/bug: use envcmd instead of go env
authorManlio Perillo <manlio.perillo@gmail.com>
Tue, 27 Apr 2021 12:48:20 +0000 (14:48 +0200)
committerBryan C. Mills <bcmills@google.com>
Wed, 28 Apr 2021 15:47:34 +0000 (15:47 +0000)
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>

src/cmd/go/internal/bug/bug.go
src/cmd/go/internal/envcmd/env.go

index df63c579d9858fef4a77d83fbd42b442f1f77c1e..307527c695cbededab0ae1dfa4447e2fc3161c4b 100644 (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")
index aad5d704e58e8fa713d1f7220449551dd5271d38..8dbb8af1e75b5bcbaa3c9579ec97c8470ec3a425 100644 (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)
                        }
                }
        }