github.com/AbsaOSS/env-binder@v1.0.1/env/env.go (about)

     1  /*
     2  Copyright 2021 The k8gb Contributors.
     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  Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
    17  */
    18  
    19  package env
    20  
    21  import (
    22  	"os"
    23  	"strconv"
    24  	"strings"
    25  )
    26  
    27  // GetEnvAsStringOrFallback returns the env variable for the given key
    28  // and falls back to the given defaultValue if not set
    29  func GetEnvAsStringOrFallback(key, defaultValue string) string {
    30  	if v, ex := os.LookupEnv(key); ex {
    31  		return v
    32  	}
    33  	return defaultValue
    34  }
    35  
    36  // GetEnvAsArrayOfStringsOrFallback returns the env variable for the given key
    37  // and falls back to the given defaultValue if not set
    38  // GetEnvAsArrayOfStringsOrFallback trims all whitespaces from input i.e. "us, fr, au" -> {"us","fr","au"}
    39  func GetEnvAsArrayOfStringsOrFallback(key string, defaultValue []string) []string {
    40  	if v, ex := os.LookupEnv(key); ex {
    41  		if v == "" {
    42  			return []string{}
    43  		}
    44  		arr := strings.Split(strings.ReplaceAll(v, " ", ""), ",")
    45  		if len(arr) != 0 {
    46  			return arr
    47  		}
    48  	}
    49  	return defaultValue
    50  }
    51  
    52  // GetEnvAsArrayOfIntsOrFallback returns the env variable for the given key
    53  // and falls back to the given defaultValue if not set
    54  func GetEnvAsArrayOfIntsOrFallback(key string, defaultValue []int) (ints []int, err error) {
    55  	if v, ex := os.LookupEnv(key); ex {
    56  		if v == "" {
    57  			return []int{}, nil
    58  		}
    59  		slice := strings.Split(strings.ReplaceAll(v, " ", ""), ",")
    60  		for _, s := range slice {
    61  			var i int
    62  			i, err = strconv.Atoi(s)
    63  			if err != nil {
    64  				return defaultValue, err
    65  			}
    66  			ints = append(ints, i)
    67  		}
    68  		return ints, nil
    69  	}
    70  	return defaultValue, nil
    71  }
    72  
    73  // GetEnvAsArrayOfFloat64OrFallback returns the env variable for the given key
    74  // and falls back to the given defaultValue if not set
    75  func GetEnvAsArrayOfFloat64OrFallback(key string, defaultValue []float64) (floats []float64, err error) {
    76  	if v, ex := os.LookupEnv(key); ex {
    77  		if v == "" {
    78  			return []float64{}, nil
    79  		}
    80  		slice := strings.Split(strings.ReplaceAll(v, " ", ""), ",")
    81  		for _, s := range slice {
    82  			var f float64
    83  			f, err = strconv.ParseFloat(s, 64)
    84  			if err != nil {
    85  				return defaultValue, err
    86  			}
    87  			floats = append(floats, f)
    88  		}
    89  		return floats, nil
    90  	}
    91  	return defaultValue, nil
    92  }
    93  
    94  // GetEnvAsArrayOfBoolOrFallback returns the env variable for the given key
    95  // and falls back to the given defaultValue if not set
    96  func GetEnvAsArrayOfBoolOrFallback(key string, defaultValue []bool) (bools []bool, err error) {
    97  	if v, ex := os.LookupEnv(key); ex {
    98  		if v == "" {
    99  			return []bool{}, nil
   100  		}
   101  		slice := strings.Split(strings.ReplaceAll(v, " ", ""), ",")
   102  		for _, s := range slice {
   103  			var b bool
   104  			b, err = strconv.ParseBool(s)
   105  			if err != nil {
   106  				return defaultValue, err
   107  			}
   108  			bools = append(bools, b)
   109  		}
   110  		return bools, nil
   111  	}
   112  	return defaultValue, nil
   113  }
   114  
   115  // GetEnvAsIntOrFallback returns the env variable (parsed as integer) for
   116  // the given key and falls back to the given defaultValue if not set
   117  func GetEnvAsIntOrFallback(key string, defaultValue int) (int, error) {
   118  	if v, ex := os.LookupEnv(key); ex {
   119  		value, err := strconv.Atoi(v)
   120  		if err != nil {
   121  			return defaultValue, err
   122  		}
   123  		return value, nil
   124  	}
   125  	return defaultValue, nil
   126  }
   127  
   128  // GetEnvAsFloat64OrFallback returns the env variable (parsed as float64) for
   129  // the given key and falls back to the given defaultValue if not set
   130  func GetEnvAsFloat64OrFallback(key string, defaultValue float64) (float64, error) {
   131  	if v, ex := os.LookupEnv(key); ex {
   132  		value, err := strconv.ParseFloat(v, 64)
   133  		if err != nil {
   134  			return defaultValue, err
   135  		}
   136  		return value, nil
   137  	}
   138  	return defaultValue, nil
   139  }
   140  
   141  // GetEnvAsBoolOrFallback returns the env variable for the given key,
   142  // parses it as boolean and falls back to the given defaultValue if not set
   143  func GetEnvAsBoolOrFallback(key string, defaultValue bool) (val bool, err error) {
   144  	if v, ex := os.LookupEnv(key); ex {
   145  		val, err = strconv.ParseBool(v)
   146  		if err != nil {
   147  			return
   148  		}
   149  		return
   150  	}
   151  	return defaultValue, nil
   152  }