envy

package module
v2.0.0-...-e8ad87e Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT, MIT Imports: 10 Imported by: 0

README

envy

Actions 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

NOTE: the behavior of .env support was changed in v2. Previously, envy.Load() overwrote all pre-existing environment variables with the values in the .env file but now pre-existing variables have higher priority and will remain as is. (It will help you to configure your runtime environment in the modern platforms including cloud computing environments)

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, Envy loads .env file in the working directory if the file exists. To load additional 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.prod", ".env") // 3

envy.Load(".env", "NON_EXISTING_FILE") // 4

envy.Load(".env.prod", "NON_EXISTING_FILE", ".env") // 5

// 6
envy.Load(".env.prod")
envy.Load("NON_EXISTING_FILE")
envy.Load(".env")

  1. Will load the default .env file from the current working directory.
  2. Will load the file MY_ENV_FILE, but not .env
  3. Will load the file .env.prod first, then will load the .env file. If any variable is redefined in .env, they will be ignored.
  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. Will load the .env.prod file and return an error as the second file does not exist. The values in .env.prod will be loaded and available, but the ones in .env won't.
  6. The result of this will be the same as 3 as you expected.

Documentation

Overview

package 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(https://github.com/joho/godotenv/)) * More!

Index

Constants

View Source
const Version = "v2.0.0"

Variables

This section is empty.

Functions

func CurrentModule

func CurrentModule() (string, error)

CurrentModule will attempt to return the module name from `go.mod`. GOPATH isn't supported, no fallback to `CurrentPackage()` anymore.

func Environ

func Environ() []string

Environ returns a copy of strings representing the environment, in the form "key=value".

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 GoBin

func GoBin() string

func GoPath

func GoPath() string

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.

WARNING: This function is NOT safe to use from a goroutine or from code which may access any Get or Set function from a goroutine

Types

This section is empty.

Jump to

Keyboard shortcuts

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