Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to getenv for a default value #16

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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