Skip to content

Commit

Permalink
Add EnvironMap() that converts os.Environ() output to a map
Browse files Browse the repository at this point in the history
  • Loading branch information
lvillani committed Jun 9, 2015
1 parent 17ee752 commit 23979c3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 21 additions & 1 deletion os.go
@@ -1,6 +1,26 @@
package dry

import "os"
import (
"os"
"strings"
)

// EnvironMap returns the current environment variables as a map.
func EnvironMap() map[string]string {
return environToMap(os.Environ())
}

func environToMap(environ []string) map[string]string {
ret := make(map[string]string)

for _, v := range environ {
parts := strings.SplitN(v, "=", 2)

ret[parts[0]] = parts[1]
}

return ret
}

// GetenvDefault retrieves the value of the environment variable
// named by the key. It returns the given defaultValue if the
Expand Down
19 changes: 19 additions & 0 deletions os_test.go
Expand Up @@ -2,6 +2,25 @@ package dry

import "testing"

func TestEnvironMap(t *testing.T) {
ret := environToMap([]string{
"a=b",
"b=c=d",
})

if len(ret) != 2 {
t.Fail()
}

if ret["a"] != "b" {
t.Fail()
}

if ret["b"] != "c=d" {
t.Fail()
}
}

func TestGetenvDefault(t *testing.T) {
if GetenvDefault("GO_DRY_BOGUS_ENVIRONMENT_VARIABLE", "default") != "default" {
t.Fail()
Expand Down

0 comments on commit 23979c3

Please sign in to comment.