Commit 6bc9844b authored by Russ Cox's avatar Russ Cox

cmd/go: split out cmd/go/internal/env

This is one CL in a long sequence of changes to break up the
go command from one package into a plausible group of packages.

This sequence is concerned only with moving code, not changing
or cleaning up code. There will still be more cleanup after this sequence.

The entire sequence will be submitted together: it is not a goal
for the tree to build at every step.

For #18653.

Change-Id: I28b20d53d20dff06eede574eb5c20359db0d3991
Reviewed-on: https://go-review.googlesource.com/36200Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent 36ce197c
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
envcmd "cmd/go/internal/env"
) )
var cmdBug = &base.Command{ var cmdBug = &base.Command{
...@@ -42,7 +43,7 @@ func runBug(cmd *base.Command, args []string) { ...@@ -42,7 +43,7 @@ func runBug(cmd *base.Command, args []string) {
fmt.Fprintln(&buf, "```") fmt.Fprintln(&buf, "```")
fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
env := cfg.NewEnv env := cfg.NewEnv
env = append(env, extraEnvVars()...) env = append(env, envcmd.ExtraEnvVars()...)
for _, e := range env { for _, e := range env {
// Hide the TERM environment variable from "go bug". // Hide the TERM environment variable from "go bug".
// See issue #18128 // See issue #18128
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package env
import ( import (
"fmt" "fmt"
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
"cmd/go/internal/work" "cmd/go/internal/work"
) )
var cmdEnv = &base.Command{ var CmdEnv = &base.Command{
Run: runEnv, Run: runEnv,
UsageLine: "env [var ...]", UsageLine: "env [var ...]",
Short: "print Go environment information", Short: "print Go environment information",
...@@ -30,7 +30,7 @@ each named variable on its own line. ...@@ -30,7 +30,7 @@ each named variable on its own line.
`, `,
} }
func mkEnv() []cfg.EnvVar { func MkEnv() []cfg.EnvVar {
var b work.Builder var b work.Builder
b.Init() b.Init()
...@@ -87,8 +87,8 @@ func findEnv(env []cfg.EnvVar, name string) string { ...@@ -87,8 +87,8 @@ func findEnv(env []cfg.EnvVar, name string) string {
return "" return ""
} }
// extraEnvVars returns environment variables that should not leak into child processes. // ExtraEnvVars returns environment variables that should not leak into child processes.
func extraEnvVars() []cfg.EnvVar { func ExtraEnvVars() []cfg.EnvVar {
var b work.Builder var b work.Builder
b.Init() b.Init()
cppflags, cflags, cxxflags, fflags, ldflags := b.CFlags(&load.Package{}) cppflags, cflags, cxxflags, fflags, ldflags := b.CFlags(&load.Package{})
...@@ -104,7 +104,7 @@ func extraEnvVars() []cfg.EnvVar { ...@@ -104,7 +104,7 @@ func extraEnvVars() []cfg.EnvVar {
func runEnv(cmd *base.Command, args []string) { func runEnv(cmd *base.Command, args []string) {
env := cfg.NewEnv env := cfg.NewEnv
env = append(env, extraEnvVars()...) env = append(env, ExtraEnvVars()...)
if len(args) > 0 { if len(args) > 0 {
for _, name := range args { for _, name := range args {
fmt.Printf("%s\n", findEnv(env, name)) fmt.Printf("%s\n", findEnv(env, name))
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/env"
fmtcmd "cmd/go/internal/fmt" fmtcmd "cmd/go/internal/fmt"
"cmd/go/internal/help" "cmd/go/internal/help"
"cmd/go/internal/test" "cmd/go/internal/test"
...@@ -26,7 +27,7 @@ func init() { ...@@ -26,7 +27,7 @@ func init() {
work.CmdBuild, work.CmdBuild,
cmdClean, cmdClean,
cmdDoc, cmdDoc,
cmdEnv, env.CmdEnv,
cmdBug, cmdBug,
cmdFix, cmdFix,
fmtcmd.CmdFmt, fmtcmd.CmdFmt,
...@@ -100,7 +101,7 @@ func main() { ...@@ -100,7 +101,7 @@ func main() {
// but in practice there might be skew // but in practice there might be skew
// This makes sure we all agree. // This makes sure we all agree.
cfg.OrigEnv = os.Environ() cfg.OrigEnv = os.Environ()
cfg.NewEnv = mkEnv() cfg.NewEnv = env.MkEnv()
for _, env := range cfg.NewEnv { for _, env := range cfg.NewEnv {
if os.Getenv(env.Name) != env.Value { if os.Getenv(env.Name) != env.Value {
os.Setenv(env.Name, env.Value) os.Setenv(env.Name, env.Value)
......
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