Envy
Envy makes working with ENV variables in Go trivial.
Install / Use
/learn @gobuffalo/EnvyREADME
envy
Envy makes working with ENV variables in Go trivial.
- Get ENV variables with default values.
- Set ENV variables safely without affecting the underlying system.
- Temporarily change ENV vars; useful for testing.
- Map all of the key/values in the ENV.
- Loads .env files (by using godotenv)
- More!
Installation
$ go get -u github.com/gobuffalo/envy
Usage
func Test_Get(t *testing.T) {
r := require.New(t)
r.NotZero(os.Getenv("GOPATH"))
r.Equal(os.Getenv("GOPATH"), envy.Get("GOPATH", "foo"))
r.Equal("bar", envy.Get("IDONTEXIST", "bar"))
}
func Test_MustGet(t *testing.T) {
r := require.New(t)
r.NotZero(os.Getenv("GOPATH"))
v, err := envy.MustGet("GOPATH")
r.NoError(err)
r.Equal(os.Getenv("GOPATH"), v)
_, err = envy.MustGet("IDONTEXIST")
r.Error(err)
}
func Test_Set(t *testing.T) {
r := require.New(t)
_, err := envy.MustGet("FOO")
r.Error(err)
envy.Set("FOO", "foo")
r.Equal("foo", envy.Get("FOO", "bar"))
}
func Test_Temp(t *testing.T) {
r := require.New(t)
_, err := envy.MustGet("BAR")
r.Error(err)
envy.Temp(func() {
envy.Set("BAR", "foo")
r.Equal("foo", envy.Get("BAR", "bar"))
_, err = envy.MustGet("BAR")
r.NoError(err)
})
_, err = envy.MustGet("BAR")
r.Error(err)
}
.env files support
Envy now supports loading .env files by using the godotenv library.
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:
envy.Load() // 1
envy.Load("MY_ENV_FILE") // 2
envy.Load(".env", ".env.prod") // 3
envy.Load(".env", "NON_EXISTING_FILE") // 4
// 5
envy.Load(".env")
envy.Load("NON_EXISTING_FILE")
// 6
envy.Load(".env", "NON_EXISTING_FILE", ".env.prod")
- Will load the default
.envfile - Will load the file
MY_ENV_FILE, but not.env - Will load the file
.env, and after that will load the.env.prodfile. If any variable is redefined in. env.prodit will be overwritten (will contain theenv.prodvalue) - Will load the
.envfile and return an error as the second file does not exist. The values in.envwill be loaded and available. - Same as 4
- Will load the
.envfile and return an error as the second file does not exist. The values in.envwill be loaded and available, but the ones in.env.prodwon't.
Related Skills
node-connect
340.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
340.5kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
84.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
340.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
