Skip to content

Commit

Permalink
Fix modify env functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pigletfly committed Nov 4, 2020
1 parent 8027b72 commit c512f8b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
18 changes: 18 additions & 0 deletions env/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2020 The Kubernetes Authors.
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.
*/

// Package env provides functions for os environment operations.
package env // import "k8s.io/utils/env"
21 changes: 12 additions & 9 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ import (
"strconv"
)

// GetEnvAsStringOrFallback returns the env variable for the given key
// GetString returns the env variable for the given key
// and falls back to the given defaultValue if not set
func GetEnvAsStringOrFallback(key, defaultValue string) string {
if v := os.Getenv(key); v != "" {
func GetString(key, defaultValue string) string {
v, ok := os.LookupEnv(key)
if ok {
return v
}
return defaultValue
}

// GetEnvAsIntOrFallback returns the env variable (parsed as integer) for
// GetInt returns the env variable (parsed as integer) for
// the given key and falls back to the given defaultValue if not set
func GetEnvAsIntOrFallback(key string, defaultValue int) (int, error) {
if v := os.Getenv(key); v != "" {
func GetInt(key string, defaultValue int) (int, error) {
v, ok := os.LookupEnv(key)
if ok {
value, err := strconv.Atoi(v)
if err != nil {
return defaultValue, err
Expand All @@ -43,10 +45,11 @@ func GetEnvAsIntOrFallback(key string, defaultValue int) (int, error) {
return defaultValue, nil
}

// GetEnvAsFloat64OrFallback returns the env variable (parsed as float64) for
// GetFloat64 returns the env variable (parsed as float64) for
// the given key and falls back to the given defaultValue if not set
func GetEnvAsFloat64OrFallback(key string, defaultValue float64) (float64, error) {
if v := os.Getenv(key); v != "" {
func GetFloat64(key string, defaultValue float64) (float64, error) {
v, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(v, 64)
if err != nil {
return defaultValue, err
Expand Down
22 changes: 11 additions & 11 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,59 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetEnvAsStringOrFallback(t *testing.T) {
func TestGetString(t *testing.T) {
const expected = "foo"

assert := assert.New(t)

key := "FLOCKER_SET_VAR"
os.Setenv(key, expected)
assert.Equal(expected, GetEnvAsStringOrFallback(key, "~"+expected))
assert.Equal(expected, GetString(key, "~"+expected))

key = "FLOCKER_UNSET_VAR"
assert.Equal(expected, GetEnvAsStringOrFallback(key, expected))
assert.Equal(expected, GetString(key, expected))
}

func TestGetEnvAsIntOrFallback(t *testing.T) {
func TestGetInt(t *testing.T) {
const expected = 1

assert := assert.New(t)

key := "FLOCKER_SET_VAR"
os.Setenv(key, strconv.Itoa(expected))
returnVal, _ := GetEnvAsIntOrFallback(key, 1)
returnVal, _ := GetInt(key, 1)
assert.Equal(expected, returnVal)

key = "FLOCKER_UNSET_VAR"
returnVal, _ = GetEnvAsIntOrFallback(key, expected)
returnVal, _ = GetInt(key, expected)
assert.Equal(expected, returnVal)

key = "FLOCKER_SET_VAR"
os.Setenv(key, "not-an-int")
returnVal, err := GetEnvAsIntOrFallback(key, 1)
returnVal, err := GetInt(key, 1)
assert.Equal(expected, returnVal)
if err == nil {
t.Error("expected error")
}
}

func TestGetEnvAsFloat64OrFallback(t *testing.T) {
func TestGetFloat64(t *testing.T) {
const expected = 1.0

assert := assert.New(t)

key := "FLOCKER_SET_VAR"
os.Setenv(key, "1.0")
returnVal, _ := GetEnvAsFloat64OrFallback(key, 2.0)
returnVal, _ := GetFloat64(key, 2.0)
assert.Equal(expected, returnVal)

key = "FLOCKER_UNSET_VAR"
returnVal, _ = GetEnvAsFloat64OrFallback(key, 1.0)
returnVal, _ = GetFloat64(key, 1.0)
assert.Equal(expected, returnVal)

key = "FLOCKER_SET_VAR"
os.Setenv(key, "not-a-float")
returnVal, err := GetEnvAsFloat64OrFallback(key, 1.0)
returnVal, err := GetFloat64(key, 1.0)
assert.Equal(expected, returnVal)
assert.EqualError(err, "strconv.ParseFloat: parsing \"not-a-float\": invalid syntax")
}

0 comments on commit c512f8b

Please sign in to comment.