...

Source file src/k8s.io/kubernetes/pkg/util/env/env.go

Documentation: k8s.io/kubernetes/pkg/util/env

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     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  package env
    18  
    19  import (
    20  	"os"
    21  	"strconv"
    22  )
    23  
    24  // GetEnvAsStringOrFallback returns the env variable for the given key
    25  // and falls back to the given defaultValue if not set
    26  func GetEnvAsStringOrFallback(key, defaultValue string) string {
    27  	if v := os.Getenv(key); v != "" {
    28  		return v
    29  	}
    30  	return defaultValue
    31  }
    32  
    33  // GetEnvAsIntOrFallback returns the env variable (parsed as integer) for
    34  // the given key and falls back to the given defaultValue if not set
    35  func GetEnvAsIntOrFallback(key string, defaultValue int) (int, error) {
    36  	if v := os.Getenv(key); v != "" {
    37  		value, err := strconv.Atoi(v)
    38  		if err != nil {
    39  			return defaultValue, err
    40  		}
    41  		return value, nil
    42  	}
    43  	return defaultValue, nil
    44  }
    45  
    46  // GetEnvAsFloat64OrFallback returns the env variable (parsed as float64) for
    47  // the given key and falls back to the given defaultValue if not set
    48  func GetEnvAsFloat64OrFallback(key string, defaultValue float64) (float64, error) {
    49  	if v := os.Getenv(key); v != "" {
    50  		value, err := strconv.ParseFloat(v, 64)
    51  		if err != nil {
    52  			return defaultValue, err
    53  		}
    54  		return value, nil
    55  	}
    56  	return defaultValue, nil
    57  }
    58  

View as plain text