cfg := &ServerConfig{
RedisURL: viper.GetString("redis_port_6379_tcp") | viper.GetString("database_url"),
}
Hi! I've noticed when loading an HCL file that it is read with slices under each key and not as a hash. The file is read correctly when using hcl.Decode
, so it seems to be something with viper.
I seem to hit the same thing whether using viper.ReadInConfig
or viper.ReadConfig
and wondering something had come across this or if I'm doing something wrong. (not sure best way to share example code and output)
db_host: localhost
db_user: appuser
test:
db_host: test-host
Hello Devs, I use Viper to handle the configuration for a REST service. DELETE/POST/PUT requests have the potential to modify the config after which I use viper.WriteConfig() to persist the changes.
My question is whether other or not there's a built-in or easy way to do a rolling history of the last X number of configs? I've looked at the docs, and I don't believe so, but wanted to reach out to double check.
MY_VARIABLE
, does Viper do any kind of default rewriting (aside from lowercase) to access that value? So Get("my_variable")
...? If I want it to be camelCase I'll need to SetEnvKeyReplacer
, right? (Looking at viper_test.go
it seems this is how it works).
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???