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