Skip to content

Commit

Permalink
Getenv wrapper to return a default value if an environment variable i…
Browse files Browse the repository at this point in the history
…sn't there
  • Loading branch information
lvillani committed Jun 9, 2015
1 parent 77c0b5f commit 17ee752
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions os.go
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
}
9 changes: 9 additions & 0 deletions os_test.go
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()
}
}

0 comments on commit 17ee752

Please sign in to comment.