github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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 } 58 59 if buildContext.CgoEnabled { 60 env = append(env, envVar{"CGO_ENABLED", "1"}) 61 } else { 62 env = append(env, envVar{"CGO_ENABLED", "0"}) 63 } 64 65 return env 66 } 67 68 func findEnv(env []envVar, name string) string { 69 for _, e := range env { 70 if e.name == name { 71 return e.value 72 } 73 } 74 return "" 75 } 76 77 func runEnv(cmd *Command, args []string) { 78 env := mkEnv() 79 if len(args) > 0 { 80 for _, name := range args { 81 fmt.Printf("%s\n", findEnv(env, name)) 82 } 83 return 84 } 85 86 switch runtime.GOOS { 87 default: 88 for _, e := range env { 89 fmt.Printf("%s=\"%s\"\n", e.name, e.value) 90 } 91 case "plan9": 92 for _, e := range env { 93 fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1)) 94 } 95 case "windows": 96 for _, e := range env { 97 fmt.Printf("set %s=%s\n", e.name, e.value) 98 } 99 } 100 }