blob: 6d8a28fdfadb81daa211f2b5651e731b344380ec [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
18 "bufio"
19 "fmt"
20 "io"
21 "os"
Colin Cross8b8bec32019-11-15 13:18:43 -080022 "strconv"
Dan Willemsen1e704462016-08-21 15:17:17 -070023 "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.
28type Environment []string
29
30// OsEnvironment wraps the current environment returned by os.Environ()
31func 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.
39func (e *Environment) Get(key string) (string, bool) {
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000040 for _, envVar := range *e {
41 if k, v, ok := decodeKeyValue(envVar); ok && k == key {
Dan Willemsen1e704462016-08-21 15:17:17 -070042 return v, true
43 }
44 }
45 return "", false
46}
47
Colin Cross8b8bec32019-11-15 13:18:43 -080048// Get returns the int value associated with the key, and whether it exists
49// and is a valid int.
50func (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 Willemsen1e704462016-08-21 15:17:17 -070059// Set sets the value associated with the key, overwriting the current value
60// if it exists.
61func (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.
67func (e *Environment) Unset(keys ...string) {
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000068 newEnv := (*e)[:0]
69 for _, envVar := range *e {
70 if key, _, ok := decodeKeyValue(envVar); ok && inList(key, keys) {
71 // Delete this key.
Dan Willemsen1e704462016-08-21 15:17:17 -070072 continue
73 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000074 newEnv = append(newEnv, envVar)
Dan Willemsen1e704462016-08-21 15:17:17 -070075 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000076 *e = newEnv
Dan Willemsen1e704462016-08-21 15:17:17 -070077}
78
Dan Willemsened869522018-01-08 14:58:46 -080079// UnsetWithPrefix removes all keys that start with prefix.
80func (e *Environment) UnsetWithPrefix(prefix string) {
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000081 newEnv := (*e)[:0]
82 for _, envVar := range *e {
83 if key, _, ok := decodeKeyValue(envVar); ok && strings.HasPrefix(key, prefix) {
84 // Delete this key.
Dan Willemsened869522018-01-08 14:58:46 -080085 continue
86 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000087 newEnv = append(newEnv, envVar)
Dan Willemsened869522018-01-08 14:58:46 -080088 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000089 *e = newEnv
Dan Willemsened869522018-01-08 14:58:46 -080090}
91
Dan Willemsenfb1271a2018-09-26 15:00:42 -070092// Allow removes all keys that are not present in the input list
93func (e *Environment) Allow(keys ...string) {
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +000094 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 Willemsenfb1271a2018-09-26 15:00:42 -070099 }
100 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +0000101 *e = newEnv
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700102}
103
Dan Willemsen1e704462016-08-21 15:17:17 -0700104// Environ returns the []string required for exec.Cmd.Env
105func (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.
110func (e *Environment) Copy() *Environment {
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +0000111 envCopy := Environment(make([]string, len(*e)))
112 for i, envVar := range *e {
113 envCopy[i] = envVar
Dan Willemsen1e704462016-08-21 15:17:17 -0700114 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +0000115 return &envCopy
Dan Willemsen1e704462016-08-21 15:17:17 -0700116}
117
118// IsTrue returns whether an environment variable is set to a positive value (1,y,yes,on,true)
119func (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)
127func (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.
136func (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 Shuttleworth80115fa2020-11-24 13:10:12 +0000146// Helper function for AppendFromKati. Accepts an io.Reader to make testing easier.
Dan Willemsen1e704462016-08-21 15:17:17 -0700147func (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 Shuttleworth80115fa2020-11-24 13:10:12 +0000153 // Skip blank lines and comments.
Dan Willemsen1e704462016-08-21 15:17:17 -0700154 continue
155 }
156
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +0000157 // We expect two space-delimited strings, like:
158 // unset 'HOME'
159 // export 'BEST_PIZZA_CITY'='NYC'
Dan Willemsen1e704462016-08-21 15:17:17 -0700160 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 Willemsen59339a22018-07-22 21:18:45 -0700168 return fmt.Errorf("Failed to unquote kati line: %q", text)
Dan Willemsen1e704462016-08-21 15:17:17 -0700169 }
Rupert Shuttleworth80115fa2020-11-24 13:10:12 +0000170
171 // Actually unset it.
Dan Willemsen1e704462016-08-21 15:17:17 -0700172 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 Shuttleworth80115fa2020-11-24 13:10:12 +0000188 // Actually set it.
Dan Willemsen1e704462016-08-21 15:17:17 -0700189 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}