AutoEnv
Dynamically interpolate placeholder variables in any config file. Made especially to be a plugin for Viper.
Features
- Very easy to use
- Deep interpolation/substitution of variables
- Works with Viper
- Elegant
- Ability to change the left/right delimiters for convenience How?
Installation
go get z3ntl3.com/autoenv
After that
You can just import it in your project and use it.
package main
import "z3ntl3.com/autoenv"
Example
Let's consider the following as a .env
file:
NAME="z3ntl3"
DOMAIN="com"
WEBSITE="{NAME}.{DOMAIN}"
And this code:
package main
import (
"fmt"
"log"
"github.com/spf13/viper"
"z3ntl3.com/autoenv/pkg/autoenv"
)
func main(){
viper.AddConfigPath("../tests")
viper.SetConfigName("test")
viper.SetConfigType("env")
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
autoenv.New().Execute()
fmt.Println(viper.GetString("WEBSITE"))
}
Output
WEBSITE="{NAME}.{DOMAIN}"
turns into z3ntl3.com
.
How to change delimiters
Have this example ENV
NAME="z3ntl3"
DOMAIN="com"
WEBSITE="<NAME>.<DOMAIN>"
And this code:
package main
import (
"fmt"
"log"
"github.com/spf13/viper"
"z3ntl3.com/autoenv/pkg/autoenv"
)
func main() {
viper.AddConfigPath("../../tests")
viper.SetConfigName("test2")
viper.SetConfigType("env")
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
autoenv.New().SetDelim("<", ">").Execute()
fmt.Println(viper.GetString("WEBSITE"))
}
Output
WEBSITE="<NAME>.<DOMAIN>"
turns into z3ntl3.com
.
License
GNU GPLv3