Merge "Minor cleanup of environment.go."
This commit is contained in:
commit
b21906cd70
1 changed files with 32 additions and 21 deletions
|
@ -37,8 +37,8 @@ func OsEnvironment() *Environment {
|
||||||
// It's equivalent to the os.LookupEnv function, but with this copy of the
|
// It's equivalent to the os.LookupEnv function, but with this copy of the
|
||||||
// Environment.
|
// Environment.
|
||||||
func (e *Environment) Get(key string) (string, bool) {
|
func (e *Environment) Get(key string) (string, bool) {
|
||||||
for _, env := range *e {
|
for _, envVar := range *e {
|
||||||
if k, v, ok := decodeKeyValue(env); ok && k == key {
|
if k, v, ok := decodeKeyValue(envVar); ok && k == key {
|
||||||
return v, true
|
return v, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,37 +65,40 @@ func (e *Environment) Set(key, value string) {
|
||||||
|
|
||||||
// Unset removes the specified keys from the Environment.
|
// Unset removes the specified keys from the Environment.
|
||||||
func (e *Environment) Unset(keys ...string) {
|
func (e *Environment) Unset(keys ...string) {
|
||||||
out := (*e)[:0]
|
newEnv := (*e)[:0]
|
||||||
for _, env := range *e {
|
for _, envVar := range *e {
|
||||||
if key, _, ok := decodeKeyValue(env); ok && inList(key, keys) {
|
if key, _, ok := decodeKeyValue(envVar); ok && inList(key, keys) {
|
||||||
|
// Delete this key.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
out = append(out, env)
|
newEnv = append(newEnv, envVar)
|
||||||
}
|
}
|
||||||
*e = out
|
*e = newEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsetWithPrefix removes all keys that start with prefix.
|
// UnsetWithPrefix removes all keys that start with prefix.
|
||||||
func (e *Environment) UnsetWithPrefix(prefix string) {
|
func (e *Environment) UnsetWithPrefix(prefix string) {
|
||||||
out := (*e)[:0]
|
newEnv := (*e)[:0]
|
||||||
for _, env := range *e {
|
for _, envVar := range *e {
|
||||||
if key, _, ok := decodeKeyValue(env); ok && strings.HasPrefix(key, prefix) {
|
if key, _, ok := decodeKeyValue(envVar); ok && strings.HasPrefix(key, prefix) {
|
||||||
|
// Delete this key.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
out = append(out, env)
|
newEnv = append(newEnv, envVar)
|
||||||
}
|
}
|
||||||
*e = out
|
*e = newEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow removes all keys that are not present in the input list
|
// Allow removes all keys that are not present in the input list
|
||||||
func (e *Environment) Allow(keys ...string) {
|
func (e *Environment) Allow(keys ...string) {
|
||||||
out := (*e)[:0]
|
newEnv := (*e)[:0]
|
||||||
for _, env := range *e {
|
for _, envVar := range *e {
|
||||||
if key, _, ok := decodeKeyValue(env); ok && inList(key, keys) {
|
if key, _, ok := decodeKeyValue(envVar); ok && inList(key, keys) {
|
||||||
out = append(out, env)
|
// Keep this key.
|
||||||
|
newEnv = append(newEnv, envVar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*e = out
|
*e = newEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
// Environ returns the []string required for exec.Cmd.Env
|
// Environ returns the []string required for exec.Cmd.Env
|
||||||
|
@ -105,11 +108,11 @@ func (e *Environment) Environ() []string {
|
||||||
|
|
||||||
// Copy returns a copy of the Environment so that independent changes may be made.
|
// Copy returns a copy of the Environment so that independent changes may be made.
|
||||||
func (e *Environment) Copy() *Environment {
|
func (e *Environment) Copy() *Environment {
|
||||||
ret := Environment(make([]string, len(*e)))
|
envCopy := Environment(make([]string, len(*e)))
|
||||||
for i, v := range *e {
|
for i, envVar := range *e {
|
||||||
ret[i] = v
|
envCopy[i] = envVar
|
||||||
}
|
}
|
||||||
return &ret
|
return &envCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTrue returns whether an environment variable is set to a positive value (1,y,yes,on,true)
|
// IsTrue returns whether an environment variable is set to a positive value (1,y,yes,on,true)
|
||||||
|
@ -140,15 +143,20 @@ func (e *Environment) AppendFromKati(filename string) error {
|
||||||
return e.appendFromKati(file)
|
return e.appendFromKati(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function for AppendFromKati. Accepts an io.Reader to make testing easier.
|
||||||
func (e *Environment) appendFromKati(reader io.Reader) error {
|
func (e *Environment) appendFromKati(reader io.Reader) error {
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
text := strings.TrimSpace(scanner.Text())
|
text := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
if len(text) == 0 || text[0] == '#' {
|
if len(text) == 0 || text[0] == '#' {
|
||||||
|
// Skip blank lines and comments.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We expect two space-delimited strings, like:
|
||||||
|
// unset 'HOME'
|
||||||
|
// export 'BEST_PIZZA_CITY'='NYC'
|
||||||
cmd := strings.SplitN(text, " ", 2)
|
cmd := strings.SplitN(text, " ", 2)
|
||||||
if len(cmd) != 2 {
|
if len(cmd) != 2 {
|
||||||
return fmt.Errorf("Unknown kati environment line: %q", text)
|
return fmt.Errorf("Unknown kati environment line: %q", text)
|
||||||
|
@ -159,6 +167,8 @@ func (e *Environment) appendFromKati(reader io.Reader) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Failed to unquote kati line: %q", text)
|
return fmt.Errorf("Failed to unquote kati line: %q", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actually unset it.
|
||||||
e.Unset(str)
|
e.Unset(str)
|
||||||
} else if cmd[0] == "export" {
|
} else if cmd[0] == "export" {
|
||||||
key, value, ok := decodeKeyValue(cmd[1])
|
key, value, ok := decodeKeyValue(cmd[1])
|
||||||
|
@ -175,6 +185,7 @@ func (e *Environment) appendFromKati(reader io.Reader) error {
|
||||||
return fmt.Errorf("Failed to unquote kati line: %q", text)
|
return fmt.Errorf("Failed to unquote kati line: %q", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actually set it.
|
||||||
e.Set(key, value)
|
e.Set(key, value)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Unknown kati environment command: %q", text)
|
return fmt.Errorf("Unknown kati environment command: %q", text)
|
||||||
|
|
Loading…
Reference in a new issue