diff --git a/README.md b/README.md new file mode 100644 index 0000000..a3013d0 --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +

+ envctl +

+ +

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

+ +

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

+ +```go +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](https://pkg.go.dev/github.com/jimeh/envctl#section-documentation) +for documentation and examples. + +## Benchmarks + +Benchmark reports and graphs are available here: +https://jimeh.me/envctl/dev/bench/ + +## License + +[MIT](https://github.com/jimeh/envctl/blob/master/LICENSE) diff --git a/envctl.go b/envctl.go index 49a2424..114d6f2 100644 --- a/envctl.go +++ b/envctl.go @@ -1,7 +1,7 @@ // 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(). +// manual changes with os.Setenv() within the helper functions. package envctl import ( diff --git a/envctl_example_test.go b/envctl_example_test.go index 72c886b..6546503 100644 --- a/envctl_example_test.go +++ b/envctl_example_test.go @@ -7,6 +7,19 @@ import ( "github.com/jimeh/envctl" ) +func Example_basic() { + 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 +} + func ExampleWith() { // existing environment variables os.Setenv("MYAPP_HOSTNAME", "myapp.com") @@ -23,7 +36,6 @@ func ExampleWith() { "MYAPP_HOSTNAME": "testing-myapp.test", "MYAPP_TESTING": "unit", } - envctl.With(env, func() { os.Setenv("MYAPP_THEME", "dark")