Skip to content

Commit

Permalink
Adding support to getenv for a default value
Browse files Browse the repository at this point in the history
Fixes #14

Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Mar 22, 2016
1 parent 2176307 commit 1c64448
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ func init() {
}

// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
func Getenv(key string) string {
return os.Getenv(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]
} else {
return os.Getenv(key)
}
}

// Bool converts a string to a boolean value, using strconv.ParseBool under the covers.
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestGetenv(t *testing.T) {
assert.Empty(t, testTemplate(`{{getenv "BLAHBLAHBLAH"}}`))
assert.Equal(t, Getenv("USER"), os.Getenv("USER"))
assert.Equal(t, os.Getenv("USER"), testTemplate(`{{getenv "USER"}}`))
assert.Equal(t, "default value", testTemplate(`{{getenv "BLAHBLAHBLAH" "default value"}}`))
}

func TestBool(t *testing.T) {
Expand Down

0 comments on commit 1c64448

Please sign in to comment.