Merge pull request #2 from jimeh/add-readme

docs(readme): add README and update godoc comments/examples
This commit is contained in:
2021-01-17 20:49:24 +00:00
committed by GitHub
3 changed files with 80 additions and 2 deletions

66
README.md Normal file
View File

@@ -0,0 +1,66 @@
<h1 align="center">
envctl
</h1>
<p align="center">
<strong>
Go package providing test helper functions to temporarily change and restore
environment variables.
</strong>
</p>
<p align="center">
<a href="https://pkg.go.dev/github.com/jimeh/envctl">
<img src="https://img.shields.io/badge/%E2%80%8B-reference-387b97.svg?logo=go&logoColor=white"
alt="Go Reference">
</a>
<a href="https://github.com/jimeh/envctl/releases">
<img src="https://img.shields.io/github/v/tag/jimeh/envctl?label=release" alt="GitHub tag (latest SemVer)">
</a>
<a href="https://github.com/jimeh/envctl/actions">
<img src="https://img.shields.io/github/workflow/status/jimeh/envctl/CI.svg?logo=github" alt="Actions Status">
</a>
<a href="https://codeclimate.com/github/jimeh/envctl">
<img src="https://img.shields.io/codeclimate/coverage/jimeh/envctl.svg?logo=code%20climate" alt="Coverage">
</a>
<a href="https://github.com/jimeh/envctl/issues">
<img src="https://img.shields.io/github/issues-raw/jimeh/envctl.svg?style=flat&logo=github&logoColor=white"
alt="GitHub issues">
</a>
<a href="https://github.com/jimeh/envctl/pulls">
<img src="https://img.shields.io/github/issues-pr-raw/jimeh/envctl.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests">
</a>
<a href="https://github.com/jimeh/envctl/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/jimeh/envctl.svg?style=flat" alt="License Status">
</a>
</p>
```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)

View File

@@ -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 (

View File

@@ -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")