Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / gobuffalo / envy / README.md
1 # envy
2 [![Build Status](https://travis-ci.org/gobuffalo/envy.svg?branch=master)](https://travis-ci.org/gobuffalo/envy)
3
4 Envy makes working with ENV variables in Go trivial.
5
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/))
11 * More!
12
13 ## Installation
14
15 ```text
16 $ go get -u github.com/gobuffalo/envy
17 ```
18
19 ## Usage
20
21 ```go
22 func Test_Get(t *testing.T) {
23         r := require.New(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"))
27 }
28
29 func Test_MustGet(t *testing.T) {
30         r := require.New(t)
31         r.NotZero(os.Getenv("GOPATH"))
32         v, err := envy.MustGet("GOPATH")
33         r.NoError(err)
34         r.Equal(os.Getenv("GOPATH"), v)
35
36         _, err = envy.MustGet("IDONTEXIST")
37         r.Error(err)
38 }
39
40 func Test_Set(t *testing.T) {
41         r := require.New(t)
42         _, err := envy.MustGet("FOO")
43         r.Error(err)
44
45         envy.Set("FOO", "foo")
46         r.Equal("foo", envy.Get("FOO", "bar"))
47 }
48
49 func Test_Temp(t *testing.T) {
50         r := require.New(t)
51
52         _, err := envy.MustGet("BAR")
53         r.Error(err)
54
55         envy.Temp(func() {
56                 envy.Set("BAR", "foo")
57                 r.Equal("foo", envy.Get("BAR", "bar"))
58                 _, err = envy.MustGet("BAR")
59                 r.NoError(err)
60         })
61
62         _, err = envy.MustGet("BAR")
63         r.Error(err)
64 }
65 ```
66 ## .env files support
67
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:
70
71 ```go
72 envy.Load() // 1
73
74 envy.Load("MY_ENV_FILE") // 2
75
76 envy.Load(".env", ".env.prod") // 3
77
78 envy.Load(".env", "NON_EXISTING_FILE") // 4
79
80 // 5
81 envy.Load(".env")
82 envy.Load("NON_EXISTING_FILE")
83
84 // 6
85 envy.Load(".env", "NON_EXISTING_FILE", ".env.prod")
86 ```
87
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.
92 5. Same as 4
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**.