Questions tagged [viper-go]

This tag is for questions related to the Go Viper package for reading configuration data from files and other sources.

Viper is a configuration package for Go. It supports reading configuration data from many formats (JSON, TOML, YAML, HCL, or Java properties) and other sources, like environment variables and command line flags, setting defaults and overriding them. Optionally, configuration files can be watched and reloaded whenever they change.

96 questions
2
votes
1 answer

Unable to unmarshal using viper

I've been trying to extract some JSON by unmarshalling my json file but, I have no idea why it is not happening. I am able to fetch the data using viper.AllSettings() but not by unmarshal. I think i am making a silly mistake, please share your…
Parth Wadhwa
  • 577
  • 1
  • 7
  • 11
2
votes
1 answer

golang viper & hcl config file issues

I've written a simple go program using viper to read in a config file. package main import ( "fmt" "log" "github.com/spf13/viper" ) func main() { viper.SetConfigType("hcl") // # first used file …
thetango
  • 41
  • 3
2
votes
1 answer

Issues with overriding config using ENV variables in Viper

We are using Viper to read and parse our config file and all of that works without any issues. However we are not able to override some of our config values using env variables. These are specific use cases where the config is bound to a struct or…
user10267174
  • 51
  • 2
  • 6
2
votes
3 answers

Removal of key-value pair from viper config file

Is there a way to remove a key/value pair from the loaded config file? viper.Set("key", nil) does not work
RoKK
  • 617
  • 8
  • 17
2
votes
2 answers

Unmarshal hcl to struct using viper

Trying to Unmarshal a hcl config file to a struct, using viper, this error is returned: 1 error(s) decoding:\n\n* 'NATS' expected a map, got 'slice'. What is missing? The code: func lab() { var c conf // config file …
Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139
2
votes
1 answer

How to bind config items in an array to environment variables

Here below is my configuration file in toml format. [[hosts]] name = "host1" username = "user1" password = "password1" [[hosts]] name = "host2" username = "user2" password = "password2" ... and here is my code to load it: import ( "fmt" …
j3d
  • 9,492
  • 22
  • 88
  • 172
2
votes
1 answer

Replace viper map key without replacing entire map

I am using viper for my config. How do I replace a key without replacing the entire map? package main import ( "log" "github.com/spf13/viper" ) type person struct { First string Last string } func main() { v := viper.New() …
user776942
2
votes
0 answers

Config file masks

We deploy services as docker containers using Marathon. The containers include a base config file, Marathon pulls an environment config file (which has a subset of the base keys) at deployment time so when the app starts it…
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
2
votes
3 answers

golang: How can I use pflag with other packages that use flag?

How does one use pflag while also using other packages that use flag? Some of these packages define flags for the flag package (e.g. in their init functions) - and require flag.Parse() to be called. Defining flags using the pflag package, require…
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
1
vote
0 answers

Do I have to initialize viper in every package?

I am using viper for reading config files in my go module. Tree structure; . ├── README.md ├── package1 │   └── package1.go ├── config.json ├── helper │   └── helper.go └── main.go I initialized Viper in my main.go …
Nagri
  • 3,008
  • 5
  • 34
  • 63
1
vote
1 answer

How to write testable commands for app using cobra and viper

Summary:- I have a cobra-cli based golang app which uses viper for config management. I want to make my commands testable. For this I want to inject dependencies to my commands when they are being added to the root command (which happens in the…
abnvanand
  • 203
  • 2
  • 6
1
vote
1 answer

How to mention time duration in days so that golang viper config can be loaded without errors?

I have two configurations in the .env file as following MAX_TOKEN_EXPIRY_DAYS=30d ACCESS_TOKEN_DURATION=30m The struct in golang to load the config looks like following type AppConfig struct { MaxTokenExpiry time.Duration…
Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
1
vote
1 answer

Golang viper pass flags from config

I have written a cli tool for use within my org, to make connecting to certain DB's easier. Not sure how to word this question, so an example will hopefully explain. I would like to use a config like so: databases: db1: db1.endpoint.company.org …
fraserc182
  • 237
  • 1
  • 4
  • 13
1
vote
1 answer

How would I Read from multiple config files and environment variables in golang viper?

I'm using https://github.com/spf13/viper for configuration manager. I've come across a use case where I've to use multiple config files .json, .env and environment variables in such a way. First all config from .json is loaded All non-empty…
Sujit Baniya
  • 895
  • 9
  • 27
1
vote
1 answer

mapstructure tags not used by Viper when writing to YAML

I have structs defined as follows type config struct { Contexts map[string]Context `mapstructure:"contexts"` CurrentContext string `mapstructure:"current-context"` Tokens []Token …