github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/go/env.go (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"runtime"
    11  	"strings"
    12  )
    13  
    14  var cmdEnv = &Command{
    15  	Run:       runEnv,
    16  	UsageLine: "env [var ...]",
    17  	Short:     "print Go environment information",
    18  	Long: `
    19  Env prints Go environment information.
    20  
    21  By default env prints information as a shell script
    22  (on Windows, a batch file).  If one or more variable
    23  names is given as arguments,  env prints the value of
    24  each named variable on its own line.
    25  	`,
    26  }
    27  
    28  type envVar struct {
    29  	name, value string
    30  }
    31  
    32  func mkEnv() []envVar {
    33  	var b builder
    34  	b.init()
    35  
    36  	env := []envVar{
    37  		{"GOARCH", goarch},
    38  		{"GOBIN", gobin},
    39  		{"GOCHAR", archChar},
    40  		{"GOEXE", exeSuffix},
    41  		{"GOHOSTARCH", runtime.GOARCH},
    42  		{"GOHOSTOS", runtime.GOOS},
    43  		{"GOOS", goos},
    44  		{"GOPATH", os.Getenv("GOPATH")},
    45  		{"GORACE", os.Getenv("GORACE")},
    46  		{"GOROOT", goroot},
    47  		{"GOTOOLDIR", toolDir},
    48  
    49  		// disable escape codes in clang errors
    50  		{"TERM", "dumb"},
    51  	}
    52  
    53  	if goos != "plan9" {
    54  		cmd := b.gccCmd(".")
    55  		env = append(env, envVar{"CC", cmd[0]})
    56  		env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})
    57  		cmd = b.gxxCmd(".")
    58  		env = append(env, envVar{"CXX", cmd[0]})
    59  	}
    60  
    61  	if buildContext.CgoEnabled {
    62  		env = append(env, envVar{"CGO_ENABLED", "1"})
    63  	} else {
    64  		env = append(env, envVar{"CGO_ENABLED", "0"})
    65  	}
    66  
    67  	return env
    68  }
    69  
    70  func findEnv(env []envVar, name string) string {
    71  	for _, e := range env {
    72  		if e.name == name {
    73  			return e.value
    74  		}
    75  	}
    76  	return ""
    77  }
    78  
    79  func runEnv(cmd *Command, args []string) {
    80  	env := mkEnv()
    81  	if len(args) > 0 {
    82  		for _, name := range args {
    83  			fmt.Printf("%s\n", findEnv(env, name))
    84  		}
    85  		return
    86  	}
    87  
    88  	for _, e := range env {
    89  		if e.name != "TERM" {
    90  			switch runtime.GOOS {
    91  			default:
    92  				fmt.Printf("%s=\"%s\"\n", e.name, e.value)
    93  			case "plan9":
    94  				if strings.IndexByte(e.value, '\x00') < 0 {
    95  					fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1))
    96  				} else {
    97  					v := strings.Split(e.value, "\x00")
    98  					fmt.Printf("%s=(", e.name)
    99  					for x, s := range v {
   100  						if x > 0 {
   101  							fmt.Printf(" ")
   102  						}
   103  						fmt.Printf("%s", s)
   104  					}
   105  					fmt.Printf(")\n")
   106  				}
   107  			case "windows":
   108  				fmt.Printf("set %s=%s\n", e.name, e.value)
   109  			}
   110  		}
   111  	}
   112  }