Skip to content

Commit

Permalink
fix: adds appropriate env-based switch for config file location
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpauwels committed Oct 5, 2023
1 parent 55b5422 commit 203ffd8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.1

require (
github.com/ajpauwels/pit-of-vipers v1.0.3
github.com/spf13/viper v1.10.1
go.uber.org/zap v1.26.0
)

Expand All @@ -17,7 +18,6 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.10.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
Expand Down
25 changes: 22 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"strings"

viperpit "github.com/ajpauwels/pit-of-vipers"
"github.com/spf13/viper"
"go.uber.org/zap"
"os"
)

type SlackConfig struct {
Expand Down Expand Up @@ -103,13 +105,30 @@ func main() {
}
defer logger.Sync()

// Load configuration
vpCh, errCh := viperpit.NewFromPathsAndName([]string{"./config"}, "base")
// Load env-specific configuration
env := os.Getenv("APPCFG_meta_env")
configPath := "./config"
if len(env) <= 0 {
env = "local"
}
if env != "local" {
configPath = "/etc/slack-bot/config"
}

// Create viper instances for base and env-specific config files
baseViper := viper.New()
baseViper.AddConfigPath(configPath)
baseViper.SetConfigName("base")
envViper := viper.New()
envViper.AddConfigPath(configPath)
envViper.SetConfigName(env)

vpCh, errCh := viperpit.New([]*viper.Viper{baseViper, envViper})
for {
select {
case vp := <-vpCh:
// Workaround to add ENV prefix and be able to unmarshal env-provided values
vp.SetEnvPrefix("SERVICE")
vp.SetEnvPrefix("APPCFG")
vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
for _, key := range vp.AllKeys() {
val := vp.Get(key)
Expand Down

0 comments on commit 203ffd8

Please sign in to comment.