https://github.com/morkid/goenvi
Simple golang environment initializer
https://github.com/morkid/goenvi
config configuration dotenv environment
Last synced: about 1 year ago
JSON representation
Simple golang environment initializer
- Host: GitHub
- URL: https://github.com/morkid/goenvi
- Owner: morkid
- License: mit
- Created: 2021-08-02T06:38:15.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-02T07:38:12.000Z (almost 5 years ago)
- Last Synced: 2025-02-02T18:45:03.528Z (over 1 year ago)
- Topics: config, configuration, dotenv, environment
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goenvi - Simple golang environment initializer
[](https://pkg.go.dev/github.com/morkid/goenvi)
[](https://github.com/morkid/goenvi/actions)
[](https://goreportcard.com/report/github.com/morkid/goenvi)
[](https://github.com/morkid/goenvi/releases)
Initialize your environment variables in one shot. goenvi is built on top of [Viper](https://github.com/spf13/viper).
Install dependency:
```bash
go get github.com/morkid/goenvi
```
Supported config file:
- .env
- json
- yaml / yml
- toml
- java properties
- see more about [supported config](https://github.com/spf13/viper#what-is-viper)
## How to use goenvi
`cat .env`
```bash
MESSAGE="hello world"
```
`cat main.go`
```go
package main
import (
"os"
"fmt",
"github.com/morkid/goenvi"
"github.com/spf13/viper"
)
func main() {
goenvi.Initialize()
fmt.Println(os.Getenv("MESSAGE"))
fmt.Println(viper.GetString("MESSAGE"))
}
```
`go run main.go`
```bash
hello world
hello world
```
By default goenvi autoload `.env` file in current working directory.
## Load custom config file
`cat config.json`
```json
{
"version": {
"number": 1,
"name": "v1.0.0"
}
}
```
`cat main.go`
```go
package main
import (
"os"
"fmt"
"github.com/morkid/goenvi"
"github.com/spf13/viper"
)
func main() {
goenvi.Add("json", "config.json")
goenvi.Initialize()
fmt.Println(os.Getenv("VERSION_NUMBER"))
fmt.Println(os.Getenv("VERSION_NAME"))
fmt.Println(viper.GetInt("version.number"))
fmt.Println(viper.GetInt("version.name"))
}
```
## Multiple variations in one shot
`cat main.go`
```go
package main
import (
"os"
"fmt"
"github.com/morkid/goenvi"
...
)
func main() {
goenvi.Add("properties", "config.properties")
goenvi.Add("json", "config.json") // override properties if variable or file does not exists
goenvi.Add("toml", "config.toml") // override json if variable or file does not exists
goenvi.Add("yaml", "config.yaml") // override toml if variable or file does not exists
goenvi.Add("dotenv", ".env") // override yaml if variable or file does not exists
goenvi.Initialize()
fmt.Println(os.Getenv("VERSION_NUMBER"))
fmt.Println(os.Getenv("VERSION_NAME"))
fmt.Println(viper.GetInt("version.number"))
fmt.Println(viper.GetInt("version.name"))
}
```
## Register custom viper instance
```go
func main() {
myEnv := viper.New()
goenvi.Register(myEnv, true) // false if you want to load after .env loaded
goenvi.Initialize()
}
```
## Register command-line parameters as environment
by implementing `goenvi.FlagSetProvider` interface, you can register command-line parameters as environment variables.
```go
import (
"github.com/morkid/goenvi"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type myFlagSet struct {}
func (myFlagSet) VisitAll(fn func(*pflag.FlagSet)) {
defaultValue := viper.GetString("message")
pflag.String("message", defaultValue, "message to show")
pflag.Parse()
fn(pflag.CommandLine)
}
func main() {
goenvi.AddFlagSetProvider(myFlagSet{})
goenvi.Initialize()
}
```
> Note:
> `pflag` will override some environment variables if command-line parameters specified
## License
Published under the [MIT License](https://github.com/morkid/goenvi/blob/master/LICENSE).