diff options
Diffstat (limited to 'env')
| -rw-r--r-- | env/env.go | 15 | ||||
| -rw-r--r-- | env/env_test.go | 14 |
2 files changed, 29 insertions, 0 deletions
diff --git a/env/env.go b/env/env.go new file mode 100644 index 00000000..6966150a --- /dev/null +++ b/env/env.go @@ -0,0 +1,15 @@ +package env + +import "os" + +// Getenv retrieves the value of the environment variable named by the key. +// It returns the value, or the default (or an emptry string) if the variable is +// not set. +func Getenv(key string, def ...string) string { + val := os.Getenv(key) + if val == "" && len(def) > 0 { + return def[0] + } + + return val +} diff --git a/env/env_test.go b/env/env_test.go new file mode 100644 index 00000000..ba971d82 --- /dev/null +++ b/env/env_test.go @@ -0,0 +1,14 @@ +package env + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetenv(t *testing.T) { + assert.Empty(t, Getenv("FOOBARBAZ")) + assert.Equal(t, os.Getenv("USER"), Getenv("USER")) + assert.Equal(t, "default value", Getenv("BLAHBLAHBLAH", "default value")) +} |
