1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
Babyenv
=======
[](http://godoc.org/github.com/meowgorithm/babyenv)
Package babyenv collects environment variables and places them in corresponding
struct fields. It aims to reduce the boilerplate in reading data from the
environment.
The struct should contain `env` tags indicating the names of corresponding
environment variables. The values of those environment variables will be then
collected and placed into the struct. If nothing is found, struct fields will
be given their default values (for example, `bool`s will be `false`).
```go
type config struct {
Name string `env:"NAME"`
}
```
Default values can also be provided in the `default` tag.
```go
type config struct {
Name string `env:"NAME" default:"Jane"`
}
```
A 'required' flag can also be set in the following format:
```go
type config struct {
Name string `env:"NAME,required"`
}
```
If a required flag is set the 'default' tag will be ignored.
## Example
```go
package main
import (
"fmt"
"os"
"github.com/meowgorithm/babyenv"
)
type config struct {
Debug bool `env:"DEBUG"`
Port string `env:"PORT" default:"8000"`
Workers int `env:"WORKERS" default:"16"`
Name string `env:"NAME,required"`
}
func main() {
os.Setenv("DEBUG", "true")
os.Setenv("WORKERS", "4")
os.Setenv("NAME", "Jane")
var cfg config
if err := babyenv.Parse(&cfg); err != nil {
log.Fatalf("could not get environment vars: %v", err)
}
fmt.Printf("%b\n%s\n%d\n%s", cfg.Debug, cfg.Port, cfg.Workers, cfg.Name)
// Output:
// true
// 8000
// 4
// Jane
}
```
## Supported Types
Currently, only the following types are supported:
* `string`
* `bool`
* `int`
* `int64`
* `[]byte`
* `*string`
* `*bool`
* `*int`
* `*[]byte`
Pull requests are welcome, especially for new types.
## Credit
This is entirely based on the [caarlos0][carlos]’s [env][carlosenv] package.
This one simply has a slightly different interface, and less functionality.
[carlos]: https://github.com/caarlos0
[carlosenv]: https://github.com/caarlos0/env
## LICENSE
MIT
|