rsc.io/go@v0.0.0-20150416155037-e040fd465409/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  		{"GOEXE", exeSuffix},
    40  		{"GOHOSTARCH", runtime.GOARCH},
    41  		{"GOHOSTOS", runtime.GOOS},
    42  		{"GOOS", goos},
    43  		{"GOPATH", os.Getenv("GOPATH")},
    44  		{"GORACE", os.Getenv("GORACE")},
    45  		{"GOROOT", goroot},
    46  		{"GOTOOLDIR", toolDir},
    47  
    48  		// disable escape codes in clang errors
    49  		{"TERM", "dumb"},
    50  	}
    51  
    52  	if archCharErr == nil {
    53  		env = append(env, envVar{"GOCHAR", archChar()})
    54  	}
    55  
    56  	if goos != "plan9" {
    57  		cmd := b.gccCmd(".")
    58  		env = append(env, envVar{"CC", cmd[0]})
    59  		env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})
    60  		cmd = b.gxxCmd(".")
    61  		env = append(env, envVar{"CXX", cmd[0]})
    62  	}
    63  
    64  	if buildContext.CgoEnabled {
    65  		env = append(env, envVar{"CGO_ENABLED", "1"})
    66  	} else {
    67  		env = append(env, envVar{"CGO_ENABLED", "0"})
    68  	}
    69  
    70  	return env
    71  }
    72  
    73  func findEnv(env []envVar, name string) string {
    74  	for _, e := range env {
    75  		if e.name == name {
    76  			return e.value
    77  		}
    78  	}
    79  	return ""
    80  }
    81  
    82  func runEnv(cmd *Command, args []string) {
    83  	env := mkEnv()
    84  	if len(args) > 0 {
    85  		for _, name := range args {
    86  			fmt.Printf("%s\n", findEnv(env, name))
    87  		}
    88  		return
    89  	}
    90  
    91  	for _, e := range env {
    92  		if e.name != "TERM" {
    93  			switch runtime.GOOS {
    94  			default:
    95  				fmt.Printf("%s=\"%s\"\n", e.name, e.value)
    96  			case "plan9":
    97  				if strings.IndexByte(e.value, '\x00') < 0 {
    98  					fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1))
    99  				} else {
   100  					v := strings.Split(e.value, "\x00")
   101  					fmt.Printf("%s=(", e.name)
   102  					for x, s := range v {
   103  						if x > 0 {
   104  							fmt.Printf(" ")
   105  						}
   106  						fmt.Printf("%s", s)
   107  					}
   108  					fmt.Printf(")\n")
   109  				}
   110  			case "windows":
   111  				fmt.Printf("set %s=%s\n", e.name, e.value)
   112  			}
   113  		}
   114  	}
   115  }