Skip to content

Commit

Permalink
viper reading env/file config fixes
Browse files Browse the repository at this point in the history
pog7x committed Dec 19, 2022
1 parent 22cbb65 commit 1414961
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -9,4 +9,4 @@ ENV DISPLAY:=99

EXPOSE 8099

CMD ["go", "run", "main.go", "serve"]
CMD ["go", "run", "main.go", "serve", "-c=./configs/.screenpng-config.dev.yml"]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -8,5 +8,4 @@
docker build -t screen_png .

docker run -v $(pwd):/screenpng -p 8099:8099 -it screen_png

```
39 changes: 24 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"fmt"
"os"

"github.com/mitchellh/mapstructure"
"github.com/pog7x/screenpng/configs"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -24,28 +24,37 @@ func Execute() {
}

func init() {
rootCmd.AddCommand(serveCmd)
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(
&cfgFile,
"config",
"./configs/.screenpng-config.dev.yml",
"path to config file",
)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "path to config file")

initConfig()
rootCmd.AddCommand(serveCmd)
}

func initConfig() {
viper.SetConfigFile(cfgFile)
v := viper.New()
if cfgFile != "" {
v.SetConfigFile(cfgFile)

viper.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("reading config %s error: %w", v.ConfigFileUsed(), err))
}
}

v.AutomaticEnv()

envKeysMap := map[string]interface{}{}
if err := mapstructure.Decode(*configs.Configuration, &envKeysMap); err != nil {
panic(fmt.Errorf("decoding config to mapstructure error: %w", err))
}

if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("reading config %s error: %w", viper.ConfigFileUsed(), err))
for k := range envKeysMap {
if bindErr := v.BindEnv(k); bindErr != nil {
panic(fmt.Errorf("binding viper env variable '%s' error: %w", k, bindErr))
}
}

if err := viper.Unmarshal(configs.Configuration); err != nil {
panic(fmt.Errorf("decoding config %s error: %w", viper.ConfigFileUsed(), err))
if err := v.Unmarshal(configs.Configuration); err != nil {
panic(fmt.Errorf("decoding env configuration error: %w", err))
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ module github.com/pog7x/screenpng
go 1.19

require (
github.com/mitchellh/mapstructure v1.5.0
github.com/pog7x/ssfactory v0.0.0-20221116183740-42dbe8bcde9e
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
@@ -15,7 +16,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.9.2 // indirect

0 comments on commit 1414961

Please sign in to comment.