github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/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 goos != "plan9" {
    53  		cmd := b.gccCmd(".")
    54  		env = append(env, envVar{"CC", cmd[0]})
    55  		env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})
    56  		cmd = b.gxxCmd(".")
    57  		env = append(env, envVar{"CXX", cmd[0]})
    58  	}
    59  
    60  	if buildContext.CgoEnabled {
    61  		env = append(env, envVar{"CGO_ENABLED", "1"})
    62  	} else {
    63  		env = append(env, envVar{"CGO_ENABLED", "0"})
    64  	}
    65  
    66  	return env
    67  }
    68  
    69  func findEnv(env []envVar, name string) string {
    70  	for _, e := range env {
    71  		if e.name == name {
    72  			return e.value
    73  		}
    74  	}
    75  	return ""
    76  }
    77  
    78  func runEnv(cmd *Command, args []string) {
    79  	env := mkEnv()
    80  	if len(args) > 0 {
    81  		for _, name := range args {
    82  			fmt.Printf("%s\n", findEnv(env, name))
    83  		}
    84  		return
    85  	}
    86  
    87  	for _, e := range env {
    88  		if e.name != "TERM" {
    89  			switch runtime.GOOS {
    90  			default:
    91  				fmt.Printf("%s=\"%s\"\n", e.name, e.value)
    92  			case "plan9":
    93  				if strings.IndexByte(e.value, '\x00') < 0 {
    94  					fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1))
    95  				} else {
    96  					v := strings.Split(e.value, "\x00")
    97  					fmt.Printf("%s=(", e.name)
    98  					for x, s := range v {
    99  						if x > 0 {
   100  							fmt.Printf(" ")
   101  						}
   102  						fmt.Printf("%s", s)
   103  					}
   104  					fmt.Printf(")\n")
   105  				}
   106  			case "windows":
   107  				fmt.Printf("set %s=%s\n", e.name, e.value)
   108  			}
   109  		}
   110  	}
   111  }