]> Cypherpunks repositories - gostls13.git/commitdiff
os: remove Getenverror
authorBrad Fitzpatrick <bradfitz@golang.org>
Sun, 19 Feb 2012 05:18:13 +0000 (21:18 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 19 Feb 2012 05:18:13 +0000 (21:18 -0800)
Fixes #3065

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5675094

doc/go1.html
doc/go1.tmpl
misc/dashboard/builder/main.go
src/pkg/os/env.go
test/env.go

index 59d8e252462db7cdf0a302bbb433ecd48eb2d644..9e98a9782f089326847f5a41ea12818aa62ae359 100644 (file)
@@ -1451,7 +1451,14 @@ with more Go-like names, such as
 <a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
 and
 <a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>.
+</p>
 
+<p>
+The <code>Getenverror</code> function has been removed. To distinguish
+between a non-existent environment variable and an empty string,
+use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
+<a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
+</p>
 
 <p>
 <em>Updating</em>:
index 58eb1073bd5a3ee6344687c7eae7464ddf9a5235..6155fb41cfb34e2cc366f9ab1727ee4905d593d2 100644 (file)
@@ -1354,7 +1354,14 @@ with more Go-like names, such as
 <a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
 and
 <a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>.
+</p>
 
+<p>
+The <code>Getenverror</code> function has been removed. To distinguish
+between a non-existent environment variable and an empty string,
+use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
+<a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
+</p>
 
 <p>
 <em>Updating</em>:
index 7ca627670b5e301e8f06d983a701944f1808b301..5d0d6b2960d4bb2da13d5eba614e47bcb0b962ec 100644 (file)
@@ -480,8 +480,7 @@ func (b *Builder) envv() []string {
                "GOROOT_FINAL=/usr/local/go",
        }
        for _, k := range extraEnv {
-               s, err := os.Getenverror(k)
-               if err == nil {
+               if s, ok := getenvOk(k); ok {
                        e = append(e, k+"="+s)
                }
        }
@@ -497,8 +496,7 @@ func (b *Builder) envvWindows() []string {
                "GOBUILDEXIT":  "1", // exit all.bat with completion status.
        }
        for _, name := range extraEnv {
-               s, err := os.Getenverror(name)
-               if err == nil {
+               if s, ok := getenvOk(name); ok {
                        start[name] = s
                }
        }
@@ -782,3 +780,17 @@ func defaultSuffix() string {
        }
        return ".bash"
 }
+
+func getenvOk(k string) (v string, ok bool) {
+       v = os.Getenv(k)
+       if v != "" {
+               return v, true
+       }
+       keq := k + "="
+       for _, kv := range os.Environ() {
+               if kv == keq {
+                       return "", true
+               }
+       }
+       return "", false
+}
index 207e0a0ec7ae9b3c9e3da50c3f499e6e508ec055..eb265f24138f2337028a602e9e58c4a7c0e19793 100644 (file)
@@ -6,10 +6,7 @@
 
 package os
 
-import (
-       "errors"
-       "syscall"
-)
+import "syscall"
 
 // Expand replaces ${var} or $var in the string based on the mapping function.
 // Invocations of undefined variables are replaced with the empty string.
@@ -77,26 +74,10 @@ func getShellName(s string) (string, int) {
        return s[:i], i
 }
 
-// ENOENV is the error indicating that an environment variable does not exist.
-var ENOENV = errors.New("no such environment variable")
-
-// Getenverror retrieves the value of the environment variable named by the key.
-// It returns the value and an error, if any.
-func Getenverror(key string) (value string, err error) {
-       if len(key) == 0 {
-               return "", ErrInvalid
-       }
-       val, found := syscall.Getenv(key)
-       if !found {
-               return "", ENOENV
-       }
-       return val, nil
-}
-
 // Getenv retrieves the value of the environment variable named by the key.
 // It returns the value, which will be empty if the variable is not present.
 func Getenv(key string) string {
-       v, _ := Getenverror(key)
+       v, _ := syscall.Getenv(key)
        return v
 }
 
index 4dcf4443a70ff5e8838256cf71db66bf4b3f99d8..972374679ac83d387bb97dfe3f0a714e14ba3830 100644 (file)
@@ -15,18 +15,14 @@ import (
 )
 
 func main() {
-       ga, e0 := os.Getenverror("GOARCH")
-       if e0 != nil {
-               print("$GOARCH: ", e0.Error(), "\n")
-               os.Exit(1)
-       }
+       ga := os.Getenv("GOARCH")
        if ga != runtime.GOARCH {
                print("$GOARCH=", ga, "!= runtime.GOARCH=", runtime.GOARCH, "\n")
                os.Exit(1)
        }
-       xxx, e1 := os.Getenverror("DOES_NOT_EXIST")
-       if e1 != os.ENOENV {
-               print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.Error(), "\n")
+       xxx := os.Getenv("DOES_NOT_EXIST")
+       if xxx != "" {
+               print("$DOES_NOT_EXIST=", xxx, "\n")
                os.Exit(1)
        }
 }