Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config viper bug fix #17

Merged
merged 8 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ endpoints:
oidc_audience_url:
oidc_client_id:
oidc_issuer_url:
oidc_scopes:
oidc_client_scopes:
url: http://fleetdb:8000
nats:
app_name: flipflop
connection_timeout: 60s
consumer:
name: flipflop
ack_wait: 5m
max_ack_pending: 10
pull: true
queue_group: flipflop
creds:
file: /etc/nats/nats.creds
creds_file: /etc/nats/nats.creds
kv_replication: 1
stream:
user: flipflop
acknowledgements: true
duplicate_window: 5m
name: controllers
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ require (
github.com/equinix-labs/otel-init-go v0.0.9
github.com/google/uuid v1.6.0
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/metal-toolbox/ctrl v0.2.2
github.com/jeremywohl/flatten v1.0.1
github.com/metal-toolbox/ctrl v0.2.1
github.com/metal-toolbox/fleetdb v1.19.3
github.com/metal-toolbox/rivets v1.3.2
github.com/mitchellh/copystructure v1.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.1
github.com/sirupsen/logrus v1.9.3
Expand Down Expand Up @@ -91,7 +93,6 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/metal-toolbox/conditionorc v1.1.1-0.20240805163108-b1c018c91b87 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef h1:G4k02HGmBUf
github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef/go.mod h1:FgmiLTU6cJewV4Xgrq6m5o8CUlTQOJtqzaFLGA0mG+E=
github.com/jacobweinstock/registrar v0.4.7 h1:s4dOExccgD+Pc7rJC+f3Mc3D+NXHcXUaOibtcEsPxOc=
github.com/jacobweinstock/registrar v0.4.7/go.mod h1:PWmkdGFG5/ZdCqgMo7pvB3pXABOLHc5l8oQ0sgmBNDU=
github.com/jeremywohl/flatten v1.0.1 h1:LrsxmB3hfwJuE+ptGOijix1PIfOoKLJ3Uee/mzbgtrs=
github.com/jeremywohl/flatten v1.0.1/go.mod h1:4AmD/VxjWcI5SRB0n6szE2A6s2fsNHDLO0nAlMHgfLQ=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down Expand Up @@ -549,8 +551,8 @@ github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxU
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/metal-toolbox/conditionorc v1.1.1-0.20240805163108-b1c018c91b87 h1:C3Gkj7+bV18HgXJuzeJ6RAttzqsO6sDBF7I20x5JvWM=
github.com/metal-toolbox/conditionorc v1.1.1-0.20240805163108-b1c018c91b87/go.mod h1:0Rh7BPun3VcJzxsBfbZ94lxxxSUNLtugAeZPRWPcD3c=
github.com/metal-toolbox/ctrl v0.2.2 h1:8OFId6uxyVpwyS+Asmehs9PLwZrnBRWOc6viMVd6/vc=
github.com/metal-toolbox/ctrl v0.2.2/go.mod h1:pN64bE8tqvG2x1YpS62lfwEPOzMh98lXwyF11t8VLYw=
github.com/metal-toolbox/ctrl v0.2.1 h1:CYOOsmX7SNBFJjxDIWWW76imGXlIfxO8KEKlUffXAak=
github.com/metal-toolbox/ctrl v0.2.1/go.mod h1:VfYYvY7SAvHjN+PTPsaahaazM7gKWq5s37aS3kX4KKo=
github.com/metal-toolbox/fleetdb v1.19.3 h1:Dk7MVi5rS42a4OI46Ye/etg8R5hm1iajaI4U0k1GSec=
github.com/metal-toolbox/fleetdb v1.19.3/go.mod h1:v9agAzF7BhBBjA4rY+H2eZzQaBTEXO4b/Qikn4jmA7A=
github.com/metal-toolbox/rivets v1.3.2 h1:wazODpX94E2IbjuqEAmARl1r9TZE0NS1vBiGSpBW/tE=
Expand Down
36 changes: 35 additions & 1 deletion internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"os"
"strings"

"github.com/jeremywohl/flatten"
"github.com/metal-toolbox/flipflop/internal/model"
"github.com/metal-toolbox/flipflop/internal/store/fleetdb"
"github.com/metal-toolbox/rivets/events"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -54,12 +56,17 @@ func (a *App) LoadConfiguration(cfgFilePath, loglevel string) error {
cfg := &Configuration{}
a.Config = cfg

err := a.envBindVars()
if err != nil {
return err
}

a.v.SetConfigType("yaml")
a.v.SetEnvPrefix(model.AppName)
a.v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
a.v.AutomaticEnv()

err := a.ReadInFile(cfg, cfgFilePath)
err = a.ReadInFile(cfg, cfgFilePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -122,3 +129,30 @@ func (cfg *Configuration) validate() error {

return nil
}

// envBindVars binds environment variables to the struct
// without a configuration file being unmarshalled,
// this is a workaround for a viper bug,
//
// This can be replaced by the solution in https://github.com/spf13/viper/pull/1429
// once that PR is merged.
func (a *App) envBindVars() error {
envKeysMap := map[string]interface{}{}
if err := mapstructure.Decode(a.Config, &envKeysMap); err != nil {
return err
}

// Flatten nested conf map
flat, err := flatten.Flatten(envKeysMap, "", flatten.DotStyle)
if err != nil {
return errors.Wrap(err, "Unable to flatten config")
}

for k := range flat {
if err := a.v.BindEnv(k); err != nil {
return errors.Wrap(ErrConfig, "env var bind error: "+err.Error())
}
}

return nil
}
Loading