diff --git a/main.go b/main.go index 70015e307..7496c0d4c 100644 --- a/main.go +++ b/main.go @@ -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. diff --git a/main_test.go b/main_test.go index 36a1460b5..9a13a5ca4 100644 --- a/main_test.go +++ b/main_test.go @@ -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) {