Hey guys. I am not sure this is the correct place for this but....
I am using Viper to manage environment variables for a web app. It works perfectly if I run in normally....
So the web app reads a couple of values from the environment using AutomaticEnv() and a couple of calls to GetString().
If I package this up into a docker container viper will not be able to grab the env values it would if running outside the docker container. I know the environment variables are getting set. I can exec into the container see it.
I tried switching to os.Getenv(). That call is able to pull in the values.
Any ideas what's broken?
hello i have a problem when i use viper.GetString("") outside main package return nil somebody help
`package main
import (
"project/Model"
"github.com/spf13/viper"
...
)
func main() {
//Config handling
viper.SetConfigName("main")
viper.AddConfigPath("/config/")
err = viper.ReadInConfig()
...
}`
`package Model
import ("github.com/spf13/viper"
...
)
var sqlhost = viper.GetString("db.host")
func foo() {
log.Println(sqlhost)
} `
I just want to write a config with mapstructure annotations on it...
Struct:
type Person struct {
Age int `mapstructure:"a"`
Gender string `mapstructure:"g"`
Family string `mapstructure:"f"`
Name string `mapstructure:"n"`
}
The write method to write a config file:
import (
"bytes"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
func WriteConfig(configInstance interface{}, filePath string,) error {
v := viper.New()
b, err := yaml.Marshal(&configInstance)
if err != nil {
return err
}
defaultConfig := bytes.NewReader(b)
v.SetConfigType("yaml")
if err := v.MergeConfig(defaultConfig); err != nil {
return err
}
return v.WriteConfigAs(filePath)
}
The result for yaml or json is always:
{
"age": 48,
"family": "Family-Name",
"gender": "m",
"name": "Name"
}
yaml
age: 48
family: Family-Name
gender: m
name: Name
The mapstructure annotation are not respected..
Do I make something wrong here???
flags.String(longOpt, defaultValue, description)
config.ViperBindPFlag(key, flags.Lookup(longOpt))
viper.SetDefault(key, defaultValue)
WithEnvPrefix("MYPREFIX")
and WithAutomaticEnv(true)
when loading the config. I absolutely need the env vars to have the priority over by config, but i don't know how my env var should be named, as my config is not flag, but contain array and nested key/vals.
host
, my env var should be named MYPREFIX_HOST
MYPREFIX_LEVEL1_LEVEL2
RootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
envKey := strings.ToUpper(replacer.Replace(f.Name))
f.Usage = fmt.Sprintf("%s [env: %s]", f.Usage, envKey)
})
Which viper.Get method should I use to retrieve JSON from a YAML config?
did you tried with viper.GetString("path.to.your.json") ?
then parse with viper again
Hey guys, really small question here but in the README of viper, I don't see any documentation as to how to write to the the config file. I understand that there is the viper.WriteConfig()
but what I am wishing to do is something like:
viper.WriteConfig("key.key", "val")
is there a semantic approach to staging changes to the config file before viper.WriteConfig()
?
Hello friends! I'm working with environment variables to set configurations in place and it seems that viper.IsSet()
is not picking them up, my config is:
accounts:
someaccount: "one"
When I set this structure from and environment variable like so: ACCOUNTS_SOMEACCOUNT=one
(without reconfiguring this from the config.yaml file), while viper.GetString(accounts.someaccount)
works as expected, viper.IsSet('accounts')
does not. When setting the structure through yaml as the example above everything works as expected.
Am I missing something? Is this a bug or intentional behavior? Any tips on how to overcome this?