Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meowgorithm/babyenv
Go environment var parsing, for babies
https://github.com/meowgorithm/babyenv
environment-variables go
Last synced: about 2 months ago
JSON representation
Go environment var parsing, for babies
- Host: GitHub
- URL: https://github.com/meowgorithm/babyenv
- Owner: meowgorithm
- License: mit
- Created: 2019-02-04T04:32:43.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T15:41:01.000Z (over 3 years ago)
- Last Synced: 2024-10-16T19:19:28.837Z (2 months ago)
- Topics: environment-variables, go
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 33
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Babyenv
=======[![GoDoc Badge](https://godoc.org/github.com/meowgorithm/babylogger?status.svg)](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 mainimport (
"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`/`[]uint8`
* `*string`
* `*bool`
* `*int`
* `*int64`
* `*[]byte`/`*[]uint8`Pull requests are welcome, especially for new types.
## Credit
This is entirely based on [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