Skip to content

Commit 9b1c67d

Browse files
committed
feat(goenv): add wrapper to read information from go env
1 parent ebe612e commit 9b1c67d

File tree

3 files changed

+346
-0
lines changed

3 files changed

+346
-0
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ issues:
7474
- linters:
7575
- usestdlibvars
7676
text: '"100" can be replaced by http.StatusContinue'
77+
- path: goenv/names.go
78+
linters:
79+
- revive
80+
- stylecheck
81+
text: "use ALL_CAPS in Go names; use CamelCase"

goenv/goenv.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Package goenv A set of functions to get information from `go env`.
2+
package goenv
3+
4+
import (
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"os/exec"
9+
"strings"
10+
)
11+
12+
// GetAll gets information from "go env".
13+
func GetAll() (map[string]string, error) {
14+
cmd := exec.Command("go", "env", "-json")
15+
16+
out, err := cmd.Output()
17+
if err != nil {
18+
return nil, fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
19+
}
20+
21+
v := map[string]string{}
22+
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(&v)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
return v, nil
28+
}
29+
30+
// GetOne gets information from "go env" for one environment variable.
31+
func GetOne(name string) (string, error) {
32+
cmd := exec.Command("go", "env", "-json", name)
33+
34+
out, err := cmd.Output()
35+
if err != nil {
36+
return "", fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
37+
}
38+
39+
v := map[string]string{}
40+
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(&v)
41+
if err != nil {
42+
return "", err
43+
}
44+
45+
return v[name], nil
46+
}
47+
48+
// Get gets information from "go env" for one or several environment variables.
49+
func Get(name ...string) (map[string]string, error) {
50+
args := append([]string{"env", "-json"}, name...)
51+
cmd := exec.Command("go", args...) //nolint:gosec // The env var names must be checked by the user.
52+
53+
out, err := cmd.Output()
54+
if err != nil {
55+
return nil, fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
56+
}
57+
58+
v := map[string]string{}
59+
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(&v)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return v, nil
65+
}

goenv/names.go

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package goenv
2+
3+
// General-purpose environment variables.
4+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L490
5+
const (
6+
// GCCGO The gccgo command to run for 'go build -compiler=gccgo'.
7+
GCCGO = "GCCGO"
8+
// GO111MODULE Controls whether the go command runs in module-aware mode or GOPATH mode.
9+
// May be "off", "on", or "auto".
10+
// See https://golang.org/ref/mod#mod-commands.
11+
GO111MODULE = "GO111MODULE"
12+
// GOARCH The architecture, or processor, for which to compile code.
13+
// Examples are amd64, 386, arm, ppc64.
14+
GOARCH = "GOARCH"
15+
// GOAUTH Controls authentication for go-import and HTTPS module mirror interactions.
16+
// See 'go help goauth'.
17+
GOAUTH = "GOAUTH"
18+
// GOBIN The directory where 'go install' will install a command.
19+
GOBIN = "GOBIN"
20+
// GOCACHE The directory where the go command will store cached
21+
// information for reuse in future builds.
22+
GOCACHE = "GOCACHE"
23+
// GOCACHEPROG A command (with optional space-separated flags) that implements an
24+
// external go command build cache.
25+
// See 'go doc cmd/go/internal/cacheprog'.
26+
GOCACHEPROG = "GOCACHEPROG"
27+
// GODEBUG Enable various debugging facilities. See https://go.dev/doc/godebug
28+
// for details.
29+
GODEBUG = "GODEBUG"
30+
// GOENV The location of the Go environment configuration file.
31+
// Cannot be set using 'go env -w'.
32+
// Setting GOENV=off in the environment disables the use of the
33+
// default configuration file.
34+
GOENV = "GOENV"
35+
// GOFLAGS A space-separated list of -flag=value settings to apply
36+
// to go commands by default, when the given flag is known by
37+
// the current command. Each entry must be a standalone flag.
38+
// Because the entries are space-separated, flag values must
39+
// not contain spaces. Flags listed on the command line
40+
// are applied after this list and therefore override it.
41+
GOFLAGS = "GOFLAGS"
42+
// GOINSECURE Comma-separated list of glob patterns (in the syntax of Go's path.Match)
43+
// of module path prefixes that should always be fetched in an insecure
44+
// manner. Only applies to dependencies that are being fetched directly.
45+
// GOINSECURE does not disable checksum database validation. GOPRIVATE or
46+
// GONOSUMDB may be used to achieve that.
47+
GOINSECURE = "GOINSECURE"
48+
// GOMODCACHE The directory where the go command will store downloaded modules.
49+
GOMODCACHE = "GOMODCACHE"
50+
// GOOS The operating system for which to compile code.
51+
// Examples are linux, darwin, windows, netbsd.
52+
GOOS = "GOOS"
53+
// GOPATH Controls where various files are stored. See: 'go help gopath'.
54+
GOPATH = "GOPATH"
55+
// GOPROXY URL of Go module proxy. See https://golang.org/ref/mod#environment-variables
56+
// and https://golang.org/ref/mod#module-proxy for details.
57+
GOPROXY = "GOPROXY"
58+
// GOROOT The root of the go tree.
59+
GOROOT = "GOROOT"
60+
// GOSUMDB The name of checksum database to use and optionally its public key and
61+
// URL. See https://golang.org/ref/mod#authenticating.
62+
GOSUMDB = "GOSUMDB"
63+
// GOTMPDIR The directory where the go command will write
64+
// temporary source files, packages, and binaries.
65+
GOTMPDIR = "GOTMPDIR"
66+
// GOTOOLCHAIN Controls which Go toolchain is used. See https://go.dev/doc/toolchain.
67+
GOTOOLCHAIN = "GOTOOLCHAIN"
68+
// GOVCS Lists version control commands that may be used with matching servers.
69+
// See 'go help vcs'.
70+
GOVCS = "GOVCS"
71+
// GOWORK In module aware mode, use the given go.work file as a workspace file.
72+
// By default or when GOWORK is "auto", the go command searches for a
73+
// file named go.work in the current directory and then containing directories
74+
// until one is found. If a valid go.work file is found, the modules
75+
// specified will collectively be used as the main modules. If GOWORK
76+
// is "off", or a go.work file is not found in "auto" mode, workspace
77+
// mode is disabled.
78+
GOWORK = "GOWORK"
79+
80+
// GOPRIVATE Comma-separated list of glob patterns (in the syntax of Go's path.Match)
81+
// of module path prefixes that should always be fetched directly
82+
// or that should not be compared against the checksum database.
83+
// See https://golang.org/ref/mod#private-modules.
84+
GOPRIVATE = "GOPRIVATE"
85+
// GONOPROXY Comma-separated list of glob patterns (in the syntax of Go's path.Match)
86+
// of module path prefixes that should always be fetched directly
87+
// or that should not be compared against the checksum database.
88+
// See https://golang.org/ref/mod#private-modules.
89+
GONOPROXY = "GONOPROXY"
90+
// GONOSUMDB Comma-separated list of glob patterns (in the syntax of Go's path.Match)
91+
// of module path prefixes that should always be fetched directly
92+
// or that should not be compared against the checksum database.
93+
// See https://golang.org/ref/mod#private-modules.
94+
GONOSUMDB = "GONOSUMDB"
95+
)
96+
97+
// Environment variables for use with cgo.
98+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L571
99+
const (
100+
// AR The command to use to manipulate library archives when
101+
// building with the gccgo compiler.
102+
// The default is 'ar'.
103+
AR = "AR"
104+
// CC The command to use to compile C code.
105+
CC = "CC"
106+
// CGO_CFLAGS Flags that cgo will pass to the compiler when compiling
107+
// C code.
108+
CGO_CFLAGS = "CGO_CFLAGS"
109+
// CGO_CFLAGS_ALLOW A regular expression specifying additional flags to allow
110+
// to appear in #cgo CFLAGS source code directives.
111+
// Does not apply to the CGO_CFLAGS environment variable.
112+
CGO_CFLAGS_ALLOW = "CGO_CFLAGS_ALLOW"
113+
// CGO_CFLAGS_DISALLOW A regular expression specifying flags that must be disallowed
114+
// from appearing in #cgo CFLAGS source code directives.
115+
// Does not apply to the CGO_CFLAGS environment variable.
116+
CGO_CFLAGS_DISALLOW = "CGO_CFLAGS_DISALLOW"
117+
// CGO_ENABLED Whether the cgo command is supported. Either 0 or 1.
118+
CGO_ENABLED = "CGO_ENABLED"
119+
// CXX The command to use to compile C++ code.
120+
CXX = "CXX"
121+
// FC The command to use to compile Fortran code.
122+
FC = "FC"
123+
// PKG_CONFIG Path to pkg-config tool.
124+
PKG_CONFIG = "PKG_CONFIG"
125+
126+
// CGO_CPPFLAGS Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
127+
// but for the C preprocessor.
128+
CGO_CPPFLAGS = "CGO_CPPFLAGS"
129+
// CGO_CPPFLAGS_ALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
130+
// but for the C preprocessor.
131+
CGO_CPPFLAGS_ALLOW = "CGO_CPPFLAGS_ALLOW"
132+
// CGO_CPPFLAGS_DISALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
133+
// but for the C preprocessor.
134+
CGO_CPPFLAGS_DISALLOW = "CGO_CPPFLAGS_DISALLOW"
135+
136+
// CGO_CXXFLAGS Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
137+
// but for the C++ compiler.
138+
CGO_CXXFLAGS = "CGO_CXXFLAGS"
139+
// CGO_CXXFLAGS_ALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
140+
// but for the C++ compiler.
141+
CGO_CXXFLAGS_ALLOW = "CGO_CXXFLAGS_ALLOW"
142+
// CGO_CXXFLAGS_DISALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
143+
// but for the C++ compiler.
144+
CGO_CXXFLAGS_DISALLOW = "CGO_CXXFLAGS_DISALLOW"
145+
146+
// CGO_FFLAGS Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
147+
// but for the Fortran compiler.
148+
CGO_FFLAGS = "CGO_FFLAGS"
149+
// CGO_FFLAGS_ALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
150+
// but for the Fortran compiler.
151+
CGO_FFLAGS_ALLOW = "CGO_FFLAGS_ALLOW"
152+
// CGO_FFLAGS_DISALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
153+
// but for the Fortran compiler.
154+
CGO_FFLAGS_DISALLOW = "CGO_FFLAGS_DISALLOW"
155+
156+
// CGO_LDFLAGS Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
157+
// but for the linker.
158+
CGO_LDFLAGS = "CGO_LDFLAGS"
159+
// CGO_LDFLAGS_ALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
160+
// but for the linker.
161+
CGO_LDFLAGS_ALLOW = "CGO_LDFLAGS_ALLOW"
162+
// CGO_LDFLAGS_DISALLOW Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
163+
// but for the linker.
164+
CGO_LDFLAGS_DISALLOW = "CGO_LDFLAGS_DISALLOW"
165+
)
166+
167+
// Architecture-specific environment variables.
168+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L611
169+
const (
170+
// GO386 For GOARCH=386, how to implement floating point instructions.
171+
// Valid values are sse2 (default), softfloat.
172+
GO386 = "GO386"
173+
// GOAMD64 For GOARCH=amd64, the microarchitecture level for which to compile.
174+
// Valid values are v1 (default), v2, v3, v4.
175+
// See https://golang.org/wiki/MinimumRequirements#amd64
176+
GOAMD64 = "GOAMD64"
177+
// GOARM For GOARCH=arm, the ARM architecture for which to compile.
178+
// Valid values are 5, 6, 7.
179+
// When the Go tools are built on an arm system,
180+
// the default value is set based on what the build system supports.
181+
// When the Go tools are not built on an arm system
182+
// (that is, when building a cross-compiler),
183+
// the default value is 7.
184+
// The value can be followed by an option specifying how to implement floating point instructions.
185+
// Valid options are ,softfloat (default for 5) and ,hardfloat (default for 6 and 7).
186+
GOARM = "GOARM"
187+
// GOARM64 For GOARCH=arm64, the ARM64 architecture for which to compile.
188+
// Valid values are v8.0 (default), v8.{1-9}, v9.{0-5}.
189+
// The value can be followed by an option specifying extensions implemented by target hardware.
190+
// Valid options are ,lse and ,crypto.
191+
// Note that some extensions are enabled by default starting from a certain GOARM64 version;
192+
// for example, lse is enabled by default starting from v8.1.
193+
GOARM64 = "GOARM64"
194+
// GOMIPS For GOARCH=mips{,le}, whether to use floating point instructions.
195+
// Valid values are hardfloat (default), softfloat.
196+
GOMIPS = "GOMIPS"
197+
// GOMIPS64 For GOARCH=mips64{,le}, whether to use floating point instructions.
198+
// Valid values are hardfloat (default), softfloat.
199+
GOMIPS64 = "GOMIPS64"
200+
// GOPPC64 For GOARCH=ppc64{,le}, the target ISA (Instruction Set Architecture).
201+
// Valid values are power8 (default), power9, power10.
202+
GOPPC64 = "GOPPC64"
203+
// GORISCV64 For GOARCH=riscv64, the RISC-V user-mode application profile for which
204+
// to compile. Valid values are rva20u64 (default), rva22u64.
205+
// See https://github.com/riscv/riscv-profiles/blob/main/src/profiles.adoc
206+
GORISCV64 = "GORISCV64"
207+
// GOWASM For GOARCH=wasm, comma-separated list of experimental WebAssembly features to use.
208+
// Valid values are satconv, signext.
209+
GOWASM = "GOWASM"
210+
)
211+
212+
// Environment variables for use with code coverage.
213+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L654
214+
const (
215+
// GOCOVERDIR Directory into which to write code coverage data files
216+
// generated by running a "go build -cover" binary.
217+
// Requires that GOEXPERIMENT=coverageredesign is enabled.
218+
GOCOVERDIR = "GOCOVERDIR"
219+
)
220+
221+
// Special-purpose environment variables.
222+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L661
223+
const (
224+
// GCCGOTOOLDIR If set, where to find gccgo tools, such as cgo.
225+
// The default is based on how gccgo was configured.
226+
GCCGOTOOLDIR = "GCCGOTOOLDIR"
227+
// GOEXPERIMENT Comma-separated list of toolchain experiments to enable or disable.
228+
// The list of available experiments may change arbitrarily over time.
229+
// See GOROOT/src/internal/goexperiment/flags.go for currently valid values.
230+
// Warning: This variable is provided for the development and testing
231+
// of the Go toolchain itself. Use beyond that purpose is unsupported.
232+
GOEXPERIMENT = "GOEXPERIMENT"
233+
// GOFIPS140 The FIPS-140 cryptography mode to use when building binaries.
234+
// The default is GOFIPS140=off, which makes no FIPS-140 changes at all.
235+
// Other values enable FIPS-140 compliance measures and select alternate
236+
// versions of the cryptography source code.
237+
// See https://go.dev/security/fips140 for details.
238+
GOFIPS140 = "GOFIPS140"
239+
// GO_EXTLINK_ENABLED Whether the linker should use external linking mode
240+
// when using -linkmode=auto with code that uses cgo.
241+
// Set to 0 to disable external linking mode, 1 to enable it.
242+
GO_EXTLINK_ENABLED = "GO_EXTLINK_ENABLED"
243+
// GIT_ALLOW_PROTOCOL Defined by Git. A colon-separated list of schemes that are allowed
244+
// to be used with git fetch/clone. If set, any scheme not explicitly
245+
// mentioned will be considered insecure by 'go get'.
246+
// Because the variable is defined by Git, the default value cannot
247+
// be set using 'go env -w'.
248+
GIT_ALLOW_PROTOCOL = "GIT_ALLOW_PROTOCOL"
249+
)
250+
251+
// Additional information available from 'go env' but not read from the environment.
252+
// Reference: https://github.com/golang/go/blob/0afd7e85e5d7154161770f06a17d09bf1ffa3e94/src/cmd/go/internal/help/helpdoc.go#L689
253+
const (
254+
// GOEXE The executable file name suffix (".exe" on Windows, "" on other systems).
255+
GOEXE = "GOEXE"
256+
// GOGCCFLAGS A space-separated list of arguments supplied to the CC command.
257+
GOGCCFLAGS = "GOGCCFLAGS"
258+
// GOHOSTARCH The architecture (GOARCH) of the Go toolchain binaries.
259+
GOHOSTARCH = "GOHOSTARCH"
260+
// GOHOSTOS The operating system (GOOS) of the Go toolchain binaries.
261+
GOHOSTOS = "GOHOSTOS"
262+
// GOMOD The absolute path to the go.mod of the main module.
263+
// If module-aware mode is enabled, but there is no go.mod, GOMOD will be
264+
// os.DevNull ("/dev/null" on Unix-like systems, "NUL" on Windows).
265+
// If module-aware mode is disabled, GOMOD will be the empty string.
266+
GOMOD = "GOMOD"
267+
// GOTELEMETRY The current Go telemetry mode ("off", "local", or "on").
268+
// See "go help telemetry" for more information.
269+
GOTELEMETRY = "GOTELEMETRY"
270+
// GOTELEMETRYDIR The directory Go telemetry data is written is written to.
271+
GOTELEMETRYDIR = "GOTELEMETRYDIR"
272+
// GOTOOLDIR The directory where the go tools (compile, cover, doc, etc...) are installed.
273+
GOTOOLDIR = "GOTOOLDIR"
274+
// GOVERSION The version of the installed Go tree, as reported by runtime.Version.
275+
GOVERSION = "GOVERSION"
276+
)

0 commit comments

Comments
 (0)