envy

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2018 License: MIT Imports: 8 Imported by: 0

README

envy

Build Status

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")
  1. Will load the default .env file
  2. Will load the file MY_ENV_FILE, but not .env
  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)
  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.
  5. Same as 4
  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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentPackage

func CurrentPackage() string

func Environ

func Environ() []string

func Get

func Get(key string, value string) string

Get a value from the ENV. If it doesn't exist the default value will be returned.

func GoPath

func GoPath() string

func GoPaths

func GoPaths() []string

GoPaths returns all possible GOPATHS that are set.

func Load

func Load(files ...string) error

Load .env files. Files will be loaded in the same order that are received. Redefined vars will override previously existing values. IE: envy.Load(".env", "test_env/.env") will result in DIR=test_env If no arg passed, it will try to load a .env file.

func Map

func Map() map[string]string

Map all of the keys/values set in envy.

func MustGet

func MustGet(key string) (string, error)

Get a value from the ENV. If it doesn't exist an error will be returned

func MustSet

func MustSet(key string, value string) error

MustSet the value into the underlying ENV, as well as envy. This may return an error if there is a problem setting the underlying ENV value.

func Reload

func Reload()

Reload the ENV variables. Useful if an external ENV manager has been used

func Set

func Set(key string, value string)

Set a value into the ENV. This is NOT permanent. It will only affect values accessed through envy.

func Temp

func Temp(f func())

Temp makes a copy of the values and allows operation on those values temporarily during the run of the function. At the end of the function run the copy is discarded and the original values are replaced. This is useful for testing.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL