Config entry override from env
aaydinyan opened this issue · comments
Describe the bug
Config values in a ConfigEntry file cannot be overridden from env vars
To Reproduce
- Create config entry in boot.yaml:
- name: db-config
locale: "*::*::*::*"
path: config/db.yaml
- Add name value pair in db.yaml config:
host: localhost
- Override from env
os.Setenv("host=anotherhost")
Expected behavior
rkentry.GlobalAppCtx.GetConfigEntry("db-config").GetViper().GetString("host")
should be equal "anotherhost"
@aaydinyan Hi, I reproduced the bug and planned for bug fixing.
Root cause
ConfigEntry init viper instance without enabling viper.AutomaticEnv(). I've tried bellow example with fixes.
- boot.yaml
---
config:
- name: db-config
locale: "*::*::*::*"
path: config/db.yaml
gin:
- name: greeter
port: 8080
enabled: true
- config/db.yaml
host: localhost
- main.go
func main() {
// As viper docs described, the env key should be upper case in order to override
// https://github.com/spf13/viper
// AutomaticEnv is a powerful helper especially when combined with SetEnvPrefix. When called, Viper will check for an
// environment variable any time a viper.Get request is made. It will apply the following rules. It will check for an
// environment variable with a name matching the key uppercased and prefixed with the EnvPrefix if set.
os.Setenv("HOST", "anotherhost")
// Create a new boot instance.
boot := rkboot.NewBoot()
fmt.Println(rkentry.GlobalAppCtx.GetConfigEntry("db-config").GetViper().GetString("host"))
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
- Output
anotherhost
Fixes
// enable automatic env
// issue: https://github.com/rookie-ninja/rk-boot/issues/55
entry.vp.AutomaticEnv()
entry.vp.SetEnvPrefix(entry.EnvPrefix)
-
Enable AutomaticEnv() in viper instance
-
Add envPrefix option in ConfigEntry, see example bellow:
-
boot.yaml
---
config:
- name: db-config
locale: "*::*::*::*"
path: config/db.yaml
envPrefix: rk # default is empty string
gin:
- name: greeter
port: 8080
enabled: true
- main.go
func main() {
os.Setenv("RK_HOST", "anotherhost")
// Create a new boot instance.
boot := rkboot.NewBoot()
fmt.Println(rkentry.GlobalAppCtx.GetConfigEntry("db-config").GetViper().GetString("host"))
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
- Output
anotherhost
Release procedure
1: bump up rk-entry to v1.0.5
2: bump up rk-boot to v1.4.2 with rk-entry@v1.0.5
Estimated release time
By the end of today ( 2021-01.17 )
@aaydinyan
Hi, bug was fixed with new release. Please [go get] bellow newest version of your dependencies based on web framework you use.
If non of web framework were used, please bump up rk-boot version to v1.4.2.
Frameworks | Status | Tag | Installation | Dependency |
---|---|---|---|---|
gin-gonic/gin | Stable | v1.2.15 | go get github.com/rookie-ninja/rk-boot/gin | rk-gin |
gRPC | Stable | v1.2.19 | go get github.com/rookie-ninja/rk-boot/grpc | rk-grpc |
labstack/echo | Stable | v0.0.9 | go get github.com/rookie-ninja/rk-boot/echo | rk-echo |
gogf/gf | Stable | v0.0.8 | go get github.com/rookie-ninja/rk-boot/gf | rk-gf |
gofiber/fiber | Testing | v0.0.5 | go get github.com/rookie-ninja/rk-boot/fiber | rk-fiber |
zeromicro/go-zero | Testing | v0.0.3 | go get github.com/rookie-ninja/rk-boot/zero | rk-zero |
gorilla/mux | Testing | v0.0.3 | go get github.com/rookie-ninja/rk-boot/mux | rk-mux |
The bug was fixed, closing issue.