Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: Add -json flag to go env command #12567

Closed
mattfarina opened this issue Sep 10, 2015 · 8 comments
Closed

cmd/go: Add -json flag to go env command #12567

mattfarina opened this issue Sep 10, 2015 · 8 comments

Comments

@mattfarina
Copy link

Getting the go environment is useful for tools built to compliment the local environment. Being able to parse the Go environment easily would be useful. Currently, go env provides a listing of the Go environment (including defaults not always available such as $GOPATH) and go env [VAR] provides the value of an individual variable.

It would be useful to get the output of go env as a json string as well as a listing.

@joefitzgerald
Copy link

I would also very much like this flag to be available for go env. Currently, in go-plus, I use regular expressions to parse this information - and there is variance between platforms in the output - notably between windows and other platforms.

Would something like this be acceptable as a contribution? If so, I will go ahead and make the necessary changes and submit it.

@adg
Copy link
Contributor

adg commented Sep 28, 2015

What is the variance between platforms, and how would the -json flag remove that variance?

@joefitzgerald
Copy link

@adg: the variance:

The variance is explained by the fact that go env output is interpretable as a shell script (and this is a perfectly valid reason to have variance).

The rationale for JSON is that some tools (e.g. editor packages / plugins) aren't interested in shell scripts as output; they're interested in a structured format that does not vary by platform. Unsurprisingly, the output of go env can be used to detect many things about a user's go installation(s) that can then minimize the amount of explicit configuration they need to do it their tool(s) of choice.

In some respects, adding a json option will just add code to such tools (if they wish to function correctly with prior versions of go, they'll still need to handle the shell script output variant), but at least it makes future releases easier to work with.

@adg
Copy link
Contributor

adg commented Sep 28, 2015

Seems like a reasonable feature to me. Happy to review the change.

@mattfarina
Copy link
Author

@joefitzgerald did you want to propose the change to gerrit or shall I? I'm happy to review something if you'd like to.

@joefitzgerald
Copy link

@mattfarina It'll be a few days before I can get to this. If you have the time now, please go for it.

@haya14busa
Copy link
Contributor

I'm assuming that this feature is still worth considering to implement. sent a CL.

@gopherbot
Copy link

CL https://golang.org/cl/38757 mentions this issue.

lparth pushed a commit to lparth/go that referenced this issue Apr 13, 2017
"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 golang#12567

Change-Id: I75db3780f14a8ab8c7fa58cc3c9cc488ef7b66a1
Reviewed-on: https://go-review.googlesource.com/38757
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Mar 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants