Commit 846f9254 authored by haya14busa's avatar haya14busa Committed by Brad Fitzpatrick

cmd/go: add -json flag to go env

"go env" prints Go environment information as a shell script format by
default but it's difficult for some tools (e.g. editor packages) to
interpret it.

The -json flag prints the environment in JSON format which
can be easily interpreted by a lot of tools.

$ go env -json
{
        "CC": "gcc",
        "CGO_CFLAGS": "-g -O2",
        "CGO_CPPFLAGS": "",
        "CGO_CXXFLAGS": "-g -O2",
        "CGO_ENABLED": "1",
        "CGO_FFLAGS": "-g -O2",
        "CGO_LDFLAGS": "-g -O2",
        "CXX": "g++",
        "GCCGO": "gccgo",
        "GOARCH": "amd64",
        "GOBIN": "/home/haya14busa/go/bin",
        "GOEXE": "",
        "GOGCCFLAGS": "-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build498013955=/tmp/go-build -gno-record-gcc-switches",
        "GOHOSTARCH": "amd64",
        "GOHOSTOS": "linux",
        "GOOS": "linux",
        "GOPATH": "/home/haya14busa",
        "GORACE": "",
        "GOROOT": "/home/haya14busa/src/go.googlesource.com/go",
        "GOTOOLDIR": "/home/haya14busa/src/go.googlesource.com/go/pkg/tool/linux_amd64",
        "PKG_CONFIG": "pkg-config"
}

Also, it supports arguments with -json flag.

$ go env -json GOROOT GOPATH GOBIN
{
        "GOBIN": "/home/haya14busa/go/bin",
        "GOPATH": "/home/haya14busa",
        "GOROOT": "/home/haya14busa/src/go.googlesource.com/go"
}

Fixes #12567

Change-Id: I75db3780f14a8ab8c7fa58cc3c9cc488ef7b66a1
Reviewed-on: https://go-review.googlesource.com/38757Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 9f232c17
This diff is collapsed.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
package envcmd package envcmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
...@@ -18,8 +19,7 @@ import ( ...@@ -18,8 +19,7 @@ import (
) )
var CmdEnv = &base.Command{ var CmdEnv = &base.Command{
Run: runEnv, UsageLine: "env [-json] [var ...]",
UsageLine: "env [var ...]",
Short: "print Go environment information", Short: "print Go environment information",
Long: ` Long: `
Env prints Go environment information. Env prints Go environment information.
...@@ -28,9 +28,18 @@ By default env prints information as a shell script ...@@ -28,9 +28,18 @@ By default env prints information as a shell script
(on Windows, a batch file). If one or more variable (on Windows, a batch file). If one or more variable
names is given as arguments, env prints the value of names is given as arguments, env prints the value of
each named variable on its own line. each named variable on its own line.
The -json flag prints the environment in JSON format
instead of as a shell script.
`, `,
} }
func init() {
CmdEnv.Run = runEnv // break init cycle
}
var envJson = CmdEnv.Flag.Bool("json", false, "")
func MkEnv() []cfg.EnvVar { func MkEnv() []cfg.EnvVar {
var b work.Builder var b work.Builder
b.Init() b.Init()
...@@ -107,12 +116,26 @@ func runEnv(cmd *base.Command, args []string) { ...@@ -107,12 +116,26 @@ func runEnv(cmd *base.Command, args []string) {
env := cfg.CmdEnv env := cfg.CmdEnv
env = append(env, ExtraEnvVars()...) env = append(env, ExtraEnvVars()...)
if len(args) > 0 { if len(args) > 0 {
for _, name := range args { if *envJson {
fmt.Printf("%s\n", findEnv(env, name)) var es []cfg.EnvVar
for _, name := range args {
e := cfg.EnvVar{Name: name, Value: findEnv(env, name)}
es = append(es, e)
}
printEnvAsJSON(es)
} else {
for _, name := range args {
fmt.Printf("%s\n", findEnv(env, name))
}
} }
return return
} }
if *envJson {
printEnvAsJSON(env)
return
}
for _, e := range env { for _, e := range env {
if e.Name != "TERM" { if e.Name != "TERM" {
switch runtime.GOOS { switch runtime.GOOS {
...@@ -138,3 +161,18 @@ func runEnv(cmd *base.Command, args []string) { ...@@ -138,3 +161,18 @@ func runEnv(cmd *base.Command, args []string) {
} }
} }
} }
func printEnvAsJSON(env []cfg.EnvVar) {
m := make(map[string]string)
for _, e := range env {
if e.Name == "TERM" {
continue
}
m[e.Name] = e.Value
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
if err := enc.Encode(m); err != nil {
base.Fatalf("%s", err)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment