-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getenv wrapper to return a default value if an environment variable i…
…sn't there
- Loading branch information
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dry | ||
|
||
import "os" | ||
|
||
// GetenvDefault retrieves the value of the environment variable | ||
// named by the key. It returns the given defaultValue if the | ||
// variable is not present. | ||
func GetenvDefault(key, defaultValue string) string { | ||
ret := os.Getenv(key) | ||
if ret == "" { | ||
return defaultValue | ||
} | ||
|
||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dry | ||
|
||
import "testing" | ||
|
||
func TestGetenvDefault(t *testing.T) { | ||
if GetenvDefault("GO_DRY_BOGUS_ENVIRONMENT_VARIABLE", "default") != "default" { | ||
t.Fail() | ||
} | ||
} |