storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/env/env.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package env
    19  
    20  import (
    21  	"strconv"
    22  	"strings"
    23  	"sync"
    24  )
    25  
    26  var (
    27  	privateMutex sync.RWMutex
    28  	lockEnvMutex sync.Mutex
    29  	envOff       bool
    30  )
    31  
    32  // LockSetEnv locks modifications to environment.
    33  // Call returned function to unlock.
    34  func LockSetEnv() func() {
    35  	lockEnvMutex.Lock()
    36  	return lockEnvMutex.Unlock
    37  }
    38  
    39  // SetEnvOff - turns off env lookup
    40  // A global lock above this MUST ensure that
    41  func SetEnvOff() {
    42  	privateMutex.Lock()
    43  	defer privateMutex.Unlock()
    44  
    45  	envOff = true
    46  }
    47  
    48  // SetEnvOn - turns on env lookup
    49  func SetEnvOn() {
    50  	privateMutex.Lock()
    51  	defer privateMutex.Unlock()
    52  
    53  	envOff = false
    54  }
    55  
    56  // IsSet returns if the given env key is set.
    57  func IsSet(key string) bool {
    58  	_, _, _, ok := LookupEnv(key)
    59  	return ok
    60  }
    61  
    62  // Get retrieves the value of the environment variable named
    63  // by the key. If the variable is present in the environment the
    64  // value (which may be empty) is returned. Otherwise it returns
    65  // the specified default value.
    66  func Get(key, defaultValue string) string {
    67  	privateMutex.RLock()
    68  	ok := envOff
    69  	privateMutex.RUnlock()
    70  	if ok {
    71  		return defaultValue
    72  	}
    73  	if v, _, _, ok := LookupEnv(key); ok {
    74  		return v
    75  	}
    76  	return defaultValue
    77  }
    78  
    79  // GetInt returns an integer if found in the environment
    80  // and returns the default value otherwise.
    81  func GetInt(key string, defaultValue int) (int, error) {
    82  	v := Get(key, "")
    83  	if v == "" {
    84  		return defaultValue, nil
    85  	}
    86  	return strconv.Atoi(v)
    87  }
    88  
    89  // List all envs with a given prefix.
    90  func List(prefix string) (envs []string) {
    91  	for _, env := range Environ() {
    92  		if strings.HasPrefix(env, prefix) {
    93  			values := strings.SplitN(env, "=", 2)
    94  			if len(values) == 2 {
    95  				envs = append(envs, values[0])
    96  			}
    97  		}
    98  	}
    99  	return envs
   100  }