envctl

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 3 Imported by: 0

README

envctl

Go package providing test helper functions to temporarily change and restore environment variables.

Go Reference GitHub tag (latest SemVer) Actions Status Coverage GitHub issues GitHub pull requests License Status

os.Setenv("PORT", "8080")

envctl.With(map[string]string{"BIND": "0.0.0.0", "PORT": "3000"}, func() {
	fmt.Println(os.Getenv("BIND") + ":" + os.Getenv("PORT"))
})

fmt.Println(os.Getenv("BIND") + ":" + os.Getenv("PORT"))
0.0.0.0:3000
:8080

Documentation

Please see the Go Reference for documentation and examples.

Benchmarks

Benchmark reports and graphs are available here: https://jimeh.me/envctl/dev/bench/

License

MIT

Documentation

Overview

Package envctl provides test helper functions that temporarily changes environment variables accessible via os.Getenv() and os.LookupEnv(). After they are done, all changes to environment variables are reset, including manual changes with os.Setenv() within the helper functions.

Example (Basic)
package main

import (
	"fmt"
	"os"

	"github.com/jimeh/envctl"
)

func main() {
	os.Setenv("PORT", "8080")

	envctl.With(map[string]string{"BIND": "0.0.0.0", "PORT": "3000"}, func() {
		fmt.Println(os.Getenv("BIND") + ":" + os.Getenv("PORT"))
	})

	fmt.Println(os.Getenv("BIND") + ":" + os.Getenv("PORT"))
}
Output:

0.0.0.0:3000
:8080

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func With

func With(env map[string]string, f func())

With temporarily sets all given vars as environment variables during the execution of f function. Existing environment variables are also available within f. Any overridden environment variables will contain the overridden value..

After f execution completes all changes to environment variables are reset, including manual changes within the f function.

Example
package main

import (
	"fmt"
	"os"

	"github.com/jimeh/envctl"
)

func main() {
	// existing environment variables
	os.Setenv("MYAPP_HOSTNAME", "myapp.com")
	os.Setenv("MYAPP_PORT", "80")

	fmt.Println("Before:")
	fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
	fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
	fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
	fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))

	// temporary environment variables
	env := map[string]string{
		"MYAPP_HOSTNAME": "testing-myapp.test",
		"MYAPP_TESTING":  "unit",
	}
	envctl.With(env, func() {
		os.Setenv("MYAPP_THEME", "dark")

		fmt.Println("Inside func:")
		fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
		fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
		fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
		fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))
	})

	// original environment variables restored
	fmt.Println("After:")
	fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
	fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
	fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
	fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))
}
Output:

Before:
 - MYAPP_HOSTNAME=myapp.com
 - MYAPP_PORT=80
 - MYAPP_THEME=
 - MYAPP_TESTING=
Inside func:
 - MYAPP_HOSTNAME=testing-myapp.test
 - MYAPP_PORT=80
 - MYAPP_THEME=dark
 - MYAPP_TESTING=unit
After:
 - MYAPP_HOSTNAME=myapp.com
 - MYAPP_PORT=80
 - MYAPP_THEME=
 - MYAPP_TESTING=

func WithClean

func WithClean(env map[string]string, f func())

WithClean temporarily changes all environment variables available within f function to only be those provided. Existing environment variables are not available within f.

After f execution completes all changes to environment variables are reset, including manual changes within the f function.

Example
package main

import (
	"fmt"
	"os"

	"github.com/jimeh/envctl"
)

func main() {
	// existing environment variables
	os.Setenv("MYAPP_HOSTNAME", "myapp.com")
	os.Setenv("MYAPP_PORT", "80")

	fmt.Println("Before:")
	fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
	fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
	fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
	fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))

	// temporary environment variables
	env := map[string]string{
		"MYAPP_HOSTNAME": "testing-myapp.test",
		"MYAPP_TESTING":  "unit",
	}
	envctl.WithClean(env, func() {
		os.Setenv("MYAPP_THEME", "dark")

		fmt.Println("Inside func:")
		fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
		fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
		fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
		fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))
	})

	// original environme
	fmt.Println("After:")
	fmt.Printf(" - MYAPP_HOSTNAME=%s\n", os.Getenv("MYAPP_HOSTNAME"))
	fmt.Printf(" - MYAPP_PORT=%s\n", os.Getenv("MYAPP_PORT"))
	fmt.Printf(" - MYAPP_THEME=%s\n", os.Getenv("MYAPP_THEME"))
	fmt.Printf(" - MYAPP_TESTING=%s\n", os.Getenv("MYAPP_TESTING"))
}
Output:

Before:
 - MYAPP_HOSTNAME=myapp.com
 - MYAPP_PORT=80
 - MYAPP_THEME=
 - MYAPP_TESTING=
Inside func:
 - MYAPP_HOSTNAME=testing-myapp.test
 - MYAPP_PORT=
 - MYAPP_THEME=dark
 - MYAPP_TESTING=unit
After:
 - MYAPP_HOSTNAME=myapp.com
 - MYAPP_PORT=80
 - MYAPP_THEME=
 - MYAPP_TESTING=

Types

This section is empty.

Jump to

Keyboard shortcuts

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