2020-04-12 16:55:22 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
package env
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
2019-10-31 02:39:09 -04:00
|
|
|
"sync"
|
2019-10-08 01:47:56 -04:00
|
|
|
)
|
2019-10-04 13:35:33 -04:00
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
var (
|
|
|
|
privateMutex sync.RWMutex
|
2020-12-04 12:32:35 -05:00
|
|
|
lockEnvMutex sync.Mutex
|
2019-10-31 02:39:09 -04:00
|
|
|
envOff bool
|
|
|
|
)
|
|
|
|
|
2020-12-04 12:32:35 -05:00
|
|
|
// LockSetEnv locks modifications to environment.
|
|
|
|
// Call returned function to unlock.
|
|
|
|
func LockSetEnv() func() {
|
|
|
|
lockEnvMutex.Lock()
|
|
|
|
return lockEnvMutex.Unlock
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
// SetEnvOff - turns off env lookup
|
2020-12-04 12:32:35 -05:00
|
|
|
// A global lock above this MUST ensure that
|
2019-10-31 02:39:09 -04:00
|
|
|
func SetEnvOff() {
|
|
|
|
privateMutex.Lock()
|
|
|
|
defer privateMutex.Unlock()
|
|
|
|
|
|
|
|
envOff = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnvOn - turns on env lookup
|
|
|
|
func SetEnvOn() {
|
|
|
|
privateMutex.Lock()
|
|
|
|
defer privateMutex.Unlock()
|
|
|
|
|
|
|
|
envOff = false
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:38:05 -05:00
|
|
|
// IsSet returns if the given env key is set.
|
|
|
|
func IsSet(key string) bool {
|
2020-09-09 15:20:49 -04:00
|
|
|
_, _, _, ok := LookupEnv(key)
|
2019-11-13 20:38:05 -05:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
// Get retrieves the value of the environment variable named
|
|
|
|
// by the key. If the variable is present in the environment the
|
|
|
|
// value (which may be empty) is returned. Otherwise it returns
|
|
|
|
// the specified default value.
|
|
|
|
func Get(key, defaultValue string) string {
|
2019-10-31 02:39:09 -04:00
|
|
|
privateMutex.RLock()
|
|
|
|
ok := envOff
|
|
|
|
privateMutex.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return defaultValue
|
|
|
|
}
|
2020-09-09 15:20:49 -04:00
|
|
|
if v, _, _, ok := LookupEnv(key); ok {
|
2019-10-04 13:35:33 -04:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
// List all envs with a given prefix.
|
|
|
|
func List(prefix string) (envs []string) {
|
2020-08-06 21:03:16 -04:00
|
|
|
for _, env := range Environ() {
|
2019-10-08 01:47:56 -04:00
|
|
|
if strings.HasPrefix(env, prefix) {
|
2019-10-11 21:21:04 -04:00
|
|
|
values := strings.SplitN(env, "=", 2)
|
|
|
|
if len(values) == 2 {
|
|
|
|
envs = append(envs, values[0])
|
|
|
|
}
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return envs
|
|
|
|
}
|