https://github.com/bartventer/viperenv
A simple way to bind environment variables using Viper.
https://github.com/bartventer/viperenv
configuration environment-variables go viper
Last synced: about 1 year ago
JSON representation
A simple way to bind environment variables using Viper.
- Host: GitHub
- URL: https://github.com/bartventer/viperenv
- Owner: bartventer
- License: mit
- Created: 2023-10-13T11:21:58.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T08:50:38.000Z (about 2 years ago)
- Last Synced: 2024-12-24T10:38:06.020Z (over 1 year ago)
- Topics: configuration, environment-variables, go, viper
- Language: Go
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# viperenv
[](https://pkg.go.dev/github.com/bartventer/viperenv/v2)
[](https://goreportcard.com/report/github.com/bartventer/viperenv)
[](https://coveralls.io/github/bartventer/viperenv?branch=master)
[](https://github.com/bartventer/viperenv/actions/workflows/go.yml)
[](LICENSE)
`viperenv` is a Go package that provides a simple way to bind environment variables to a struct using [Viper](https://github.com/spf13/viper). It allows you to define a struct with tags that specify the environment variable names. You can then use the `viperenv.Bind()` function to bind the environment variables to the struct. The upside of this is that you don't have to manually set each environment variable with `viper.BindEnv()`, you can just bind them all at once.
## Installation
To install `viperenv`, use `go get`:
```sh
go get github.com/bartventer/viperenv/v2
```
## Usage
```go
package main
import (
"fmt"
"log"
"github.com/spf13/viper"
"github.com/bartventer/viperenv/v2"
)
type Config struct {
Host string `env:"HOST,default=localhost" mapstructure:"host"`
Port int `env:"PORT" mapstructure:"port"`
}
func main() {
// Set environment variables
os.Setenv("PORT", "8080")
// Set up viper
v := viper.New()
var config Config
# ... set up viper, and read config file, etc.
err := viperenv.Bind(&config, v, viperenv.BindOptions{
AutoEnv: true,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(config.Host) // "localhost"
fmt.Println(config.Port) // "8080"
}
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.