1
0
mirror of https://github.com/golang/go synced 2024-11-25 03:07:56 -07:00

os: remove Getenverror

Fixes #3065

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5675094
This commit is contained in:
Brad Fitzpatrick 2012-02-18 21:18:13 -08:00
parent 83feedf7bf
commit efacb2a1b4
5 changed files with 36 additions and 33 deletions

View File

@ -1451,7 +1451,14 @@ with more Go-like names, such as
<a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a> <a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
and and
<a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>. <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> <p>
<em>Updating</em>: <em>Updating</em>:

View File

@ -1354,7 +1354,14 @@ with more Go-like names, such as
<a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a> <a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
and and
<a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>. <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> <p>
<em>Updating</em>: <em>Updating</em>:

View File

@ -480,8 +480,7 @@ func (b *Builder) envv() []string {
"GOROOT_FINAL=/usr/local/go", "GOROOT_FINAL=/usr/local/go",
} }
for _, k := range extraEnv { for _, k := range extraEnv {
s, err := os.Getenverror(k) if s, ok := getenvOk(k); ok {
if err == nil {
e = append(e, k+"="+s) e = append(e, k+"="+s)
} }
} }
@ -497,8 +496,7 @@ func (b *Builder) envvWindows() []string {
"GOBUILDEXIT": "1", // exit all.bat with completion status. "GOBUILDEXIT": "1", // exit all.bat with completion status.
} }
for _, name := range extraEnv { for _, name := range extraEnv {
s, err := os.Getenverror(name) if s, ok := getenvOk(name); ok {
if err == nil {
start[name] = s start[name] = s
} }
} }
@ -782,3 +780,17 @@ func defaultSuffix() string {
} }
return ".bash" 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
}

View File

@ -6,10 +6,7 @@
package os package os
import ( import "syscall"
"errors"
"syscall"
)
// Expand replaces ${var} or $var in the string based on the mapping function. // Expand replaces ${var} or $var in the string based on the mapping function.
// Invocations of undefined variables are replaced with the empty string. // Invocations of undefined variables are replaced with the empty string.
@ -77,26 +74,10 @@ func getShellName(s string) (string, int) {
return s[:i], i 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. // 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. // It returns the value, which will be empty if the variable is not present.
func Getenv(key string) string { func Getenv(key string) string {
v, _ := Getenverror(key) v, _ := syscall.Getenv(key)
return v return v
} }

View File

@ -15,18 +15,14 @@ import (
) )
func main() { func main() {
ga, e0 := os.Getenverror("GOARCH") ga := os.Getenv("GOARCH")
if e0 != nil {
print("$GOARCH: ", e0.Error(), "\n")
os.Exit(1)
}
if ga != runtime.GOARCH { if ga != runtime.GOARCH {
print("$GOARCH=", ga, "!= runtime.GOARCH=", runtime.GOARCH, "\n") print("$GOARCH=", ga, "!= runtime.GOARCH=", runtime.GOARCH, "\n")
os.Exit(1) os.Exit(1)
} }
xxx, e1 := os.Getenverror("DOES_NOT_EXIST") xxx := os.Getenv("DOES_NOT_EXIST")
if e1 != os.ENOENV { if xxx != "" {
print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.Error(), "\n") print("$DOES_NOT_EXIST=", xxx, "\n")
os.Exit(1) os.Exit(1)
} }
} }