Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "os" |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 22 | "strconv" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | // Environment adds a number of useful manipulation functions to the list of |
| 27 | // strings returned by os.Environ() and used in exec.Cmd.Env. |
| 28 | type Environment []string |
| 29 | |
| 30 | // OsEnvironment wraps the current environment returned by os.Environ() |
| 31 | func OsEnvironment() *Environment { |
| 32 | env := Environment(os.Environ()) |
| 33 | return &env |
| 34 | } |
| 35 | |
| 36 | // Get returns the value associated with the key, and whether it exists. |
| 37 | // It's equivalent to the os.LookupEnv function, but with this copy of the |
| 38 | // Environment. |
| 39 | func (e *Environment) Get(key string) (string, bool) { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 40 | for _, envVar := range *e { |
| 41 | if k, v, ok := decodeKeyValue(envVar); ok && k == key { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 42 | return v, true |
| 43 | } |
| 44 | } |
| 45 | return "", false |
| 46 | } |
| 47 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 48 | // Get returns the int value associated with the key, and whether it exists |
| 49 | // and is a valid int. |
| 50 | func (e *Environment) GetInt(key string) (int, bool) { |
| 51 | if v, ok := e.Get(key); ok { |
| 52 | if i, err := strconv.Atoi(v); err == nil { |
| 53 | return i, true |
| 54 | } |
| 55 | } |
| 56 | return 0, false |
| 57 | } |
| 58 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 59 | // Set sets the value associated with the key, overwriting the current value |
| 60 | // if it exists. |
| 61 | func (e *Environment) Set(key, value string) { |
| 62 | e.Unset(key) |
| 63 | *e = append(*e, key+"="+value) |
| 64 | } |
| 65 | |
| 66 | // Unset removes the specified keys from the Environment. |
| 67 | func (e *Environment) Unset(keys ...string) { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 68 | newEnv := (*e)[:0] |
| 69 | for _, envVar := range *e { |
| 70 | if key, _, ok := decodeKeyValue(envVar); ok && inList(key, keys) { |
| 71 | // Delete this key. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 72 | continue |
| 73 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 74 | newEnv = append(newEnv, envVar) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 75 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 76 | *e = newEnv |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 79 | // UnsetWithPrefix removes all keys that start with prefix. |
| 80 | func (e *Environment) UnsetWithPrefix(prefix string) { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 81 | newEnv := (*e)[:0] |
| 82 | for _, envVar := range *e { |
| 83 | if key, _, ok := decodeKeyValue(envVar); ok && strings.HasPrefix(key, prefix) { |
| 84 | // Delete this key. |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 85 | continue |
| 86 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 87 | newEnv = append(newEnv, envVar) |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 88 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 89 | *e = newEnv |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 92 | // Allow removes all keys that are not present in the input list |
| 93 | func (e *Environment) Allow(keys ...string) { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 94 | newEnv := (*e)[:0] |
| 95 | for _, envVar := range *e { |
| 96 | if key, _, ok := decodeKeyValue(envVar); ok && inList(key, keys) { |
| 97 | // Keep this key. |
| 98 | newEnv = append(newEnv, envVar) |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 101 | *e = newEnv |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 104 | // Environ returns the []string required for exec.Cmd.Env |
| 105 | func (e *Environment) Environ() []string { |
| 106 | return []string(*e) |
| 107 | } |
| 108 | |
| 109 | // Copy returns a copy of the Environment so that independent changes may be made. |
| 110 | func (e *Environment) Copy() *Environment { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 111 | envCopy := Environment(make([]string, len(*e))) |
| 112 | for i, envVar := range *e { |
| 113 | envCopy[i] = envVar |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 114 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 115 | return &envCopy |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // IsTrue returns whether an environment variable is set to a positive value (1,y,yes,on,true) |
| 119 | func (e *Environment) IsEnvTrue(key string) bool { |
| 120 | if value, ok := e.Get(key); ok { |
| 121 | return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" |
| 122 | } |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | // IsFalse returns whether an environment variable is set to a negative value (0,n,no,off,false) |
| 127 | func (e *Environment) IsFalse(key string) bool { |
| 128 | if value, ok := e.Get(key); ok { |
| 129 | return value == "0" || value == "n" || value == "no" || value == "off" || value == "false" |
| 130 | } |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | // AppendFromKati reads a shell script written by Kati that exports or unsets |
| 135 | // environment variables, and applies those to the local Environment. |
| 136 | func (e *Environment) AppendFromKati(filename string) error { |
| 137 | file, err := os.Open(filename) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | defer file.Close() |
| 142 | |
| 143 | return e.appendFromKati(file) |
| 144 | } |
| 145 | |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 146 | // Helper function for AppendFromKati. Accepts an io.Reader to make testing easier. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 147 | func (e *Environment) appendFromKati(reader io.Reader) error { |
| 148 | scanner := bufio.NewScanner(reader) |
| 149 | for scanner.Scan() { |
| 150 | text := strings.TrimSpace(scanner.Text()) |
| 151 | |
| 152 | if len(text) == 0 || text[0] == '#' { |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 153 | // Skip blank lines and comments. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 154 | continue |
| 155 | } |
| 156 | |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 157 | // We expect two space-delimited strings, like: |
| 158 | // unset 'HOME' |
| 159 | // export 'BEST_PIZZA_CITY'='NYC' |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 160 | cmd := strings.SplitN(text, " ", 2) |
| 161 | if len(cmd) != 2 { |
| 162 | return fmt.Errorf("Unknown kati environment line: %q", text) |
| 163 | } |
| 164 | |
| 165 | if cmd[0] == "unset" { |
| 166 | str, ok := singleUnquote(cmd[1]) |
| 167 | if !ok { |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 168 | return fmt.Errorf("Failed to unquote kati line: %q", text) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 169 | } |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 170 | |
| 171 | // Actually unset it. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 172 | e.Unset(str) |
| 173 | } else if cmd[0] == "export" { |
| 174 | key, value, ok := decodeKeyValue(cmd[1]) |
| 175 | if !ok { |
| 176 | return fmt.Errorf("Failed to parse export: %v", cmd) |
| 177 | } |
| 178 | |
| 179 | key, ok = singleUnquote(key) |
| 180 | if !ok { |
| 181 | return fmt.Errorf("Failed to unquote kati line: %q", text) |
| 182 | } |
| 183 | value, ok = singleUnquote(value) |
| 184 | if !ok { |
| 185 | return fmt.Errorf("Failed to unquote kati line: %q", text) |
| 186 | } |
| 187 | |
Rupert Shuttleworth | 80115fa | 2020-11-24 13:10:12 +0000 | [diff] [blame^] | 188 | // Actually set it. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 189 | e.Set(key, value) |
| 190 | } else { |
| 191 | return fmt.Errorf("Unknown kati environment command: %q", text) |
| 192 | } |
| 193 | } |
| 194 | if err := scanner.Err(); err != nil { |
| 195 | return err |
| 196 | } |
| 197 | return nil |
| 198 | } |