2020-04-12 22:55:22 +02: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 19:35:33 +02:00
|
|
|
package env
|
|
|
|
|
2019-10-08 07:47:56 +02:00
|
|
|
import (
|
2021-01-12 07:48:09 +01:00
|
|
|
"strconv"
|
2019-10-08 07:47:56 +02:00
|
|
|
"strings"
|
2019-10-31 07:39:09 +01:00
|
|
|
"sync"
|
2019-10-08 07:47:56 +02:00
|
|
|
)
|
2019-10-04 19:35:33 +02:00
|
|
|
|
2019-10-31 07:39:09 +01:00
|
|
|
var (
|
|
|
|
privateMutex sync.RWMutex
|
2020-12-04 18:32:35 +01:00
|
|
|
lockEnvMutex sync.Mutex
|
2019-10-31 07:39:09 +01:00
|
|
|
envOff bool
|
|
|
|
)
|
|
|
|
|
2020-12-04 18:32:35 +01:00
|
|
|
// LockSetEnv locks modifications to environment.
|
|
|
|
// Call returned function to unlock.
|
|
|
|
func LockSetEnv() func() {
|
|
|
|
lockEnvMutex.Lock()
|
|
|
|
return lockEnvMutex.Unlock
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:39:09 +01:00
|
|
|
// SetEnvOff - turns off env lookup
|
2020-12-04 18:32:35 +01:00
|
|
|
// A global lock above this MUST ensure that
|
2019-10-31 07:39:09 +01: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-14 02:38:05 +01:00
|
|
|
// IsSet returns if the given env key is set.
|
|
|
|
func IsSet(key string) bool {
|
2020-09-09 21:20:49 +02:00
|
|
|
_, _, _, ok := LookupEnv(key)
|
2019-11-14 02:38:05 +01:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:35:33 +02: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 07:39:09 +01:00
|
|
|
privateMutex.RLock()
|
|
|
|
ok := envOff
|
|
|
|
privateMutex.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return defaultValue
|
|
|
|
}
|
2020-09-09 21:20:49 +02:00
|
|
|
if v, _, _, ok := LookupEnv(key); ok {
|
2019-10-04 19:35:33 +02:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2021-01-12 07:48:09 +01:00
|
|
|
// GetInt returns an integer if found in the environment
|
|
|
|
// and returns the default value otherwise.
|
|
|
|
func GetInt(key string, defaultValue int) (int, error) {
|
|
|
|
v := Get(key, "")
|
|
|
|
if v == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
return strconv.Atoi(v)
|
|
|
|
}
|
|
|
|
|
2019-10-08 07:47:56 +02:00
|
|
|
// List all envs with a given prefix.
|
|
|
|
func List(prefix string) (envs []string) {
|
2020-08-07 03:03:16 +02:00
|
|
|
for _, env := range Environ() {
|
2019-10-08 07:47:56 +02:00
|
|
|
if strings.HasPrefix(env, prefix) {
|
2019-10-12 03:21:04 +02:00
|
|
|
values := strings.SplitN(env, "=", 2)
|
|
|
|
if len(values) == 2 {
|
|
|
|
envs = append(envs, values[0])
|
|
|
|
}
|
2019-10-08 07:47:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return envs
|
|
|
|
}
|