2 [![Build Status](https://travis-ci.org/gobuffalo/envy.svg?branch=master)](https://travis-ci.org/gobuffalo/envy)
4 Envy makes working with ENV variables in Go trivial.
6 * Get ENV variables with default values.
7 * Set ENV variables safely without affecting the underlying system.
8 * Temporarily change ENV vars; useful for testing.
9 * Map all of the key/values in the ENV.
10 * Loads .env files (by using [godotenv](https://github.com/joho/godotenv/))
16 $ go get -u github.com/gobuffalo/envy
22 func Test_Get(t *testing.T) {
24 r.NotZero(os.Getenv("GOPATH"))
25 r.Equal(os.Getenv("GOPATH"), envy.Get("GOPATH", "foo"))
26 r.Equal("bar", envy.Get("IDONTEXIST", "bar"))
29 func Test_MustGet(t *testing.T) {
31 r.NotZero(os.Getenv("GOPATH"))
32 v, err := envy.MustGet("GOPATH")
34 r.Equal(os.Getenv("GOPATH"), v)
36 _, err = envy.MustGet("IDONTEXIST")
40 func Test_Set(t *testing.T) {
42 _, err := envy.MustGet("FOO")
45 envy.Set("FOO", "foo")
46 r.Equal("foo", envy.Get("FOO", "bar"))
49 func Test_Temp(t *testing.T) {
52 _, err := envy.MustGet("BAR")
56 envy.Set("BAR", "foo")
57 r.Equal("foo", envy.Get("BAR", "bar"))
58 _, err = envy.MustGet("BAR")
62 _, err = envy.MustGet("BAR")
68 Envy now supports loading `.env` files by using the [godotenv library](https://github.com/joho/godotenv/).
69 That means one can use and define multiple `.env` files which will be loaded on-demand. By default, no env files will be loaded. To load one or more, you need to call the `envy.Load` function in one of the following ways:
74 envy.Load("MY_ENV_FILE") // 2
76 envy.Load(".env", ".env.prod") // 3
78 envy.Load(".env", "NON_EXISTING_FILE") // 4
82 envy.Load("NON_EXISTING_FILE")
85 envy.Load(".env", "NON_EXISTING_FILE", ".env.prod")
88 1. Will load the default `.env` file
89 2. Will load the file `MY_ENV_FILE`, **but not** `.env`
90 3. Will load the file `.env`, and after that will load the `.env.prod` file. If any variable is redefined in `. env.prod` it will be overwritten (will contain the `env.prod` value)
91 4. Will load the `.env` file and return an error as the second file does not exist. The values in `.env` will be loaded and available.
93 6. Will load the `.env` file and return an error as the second file does not exist. The values in `.env` will be loaded and available, **but the ones in** `.env.prod` **won't**.