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
5
votes
1 answer

Passing a string map as an environment variable in Go using Viper

For a project I'm working on I'm trying to pass a map of stings as an environment variable using Viper. I tried several approaches to achieve this but with no success. When I read the env variable from code it is empty. This is the code I'm…
Diego
  • 1,531
  • 1
  • 15
  • 27
4
votes
1 answer

How do I extract only flagsets that are being set explicitly in the cli?

I am new to cobra and viper. I wanted to know if there's a way to exclude flag values that aren't set by the user from cli. So my problem is, I have some optional flags in my cobra cmd that have default values. I wonder if there's a way to exclude…
dekauliya
  • 1,303
  • 2
  • 15
  • 26
4
votes
2 answers

Share properties from separate commands/process

Im providing command line tool with several command and sub commands, Im using cobra command line and I’ve two separate commands that first is prerequisite to other e.g. the first command is preferring the environment by creating temp folder and…
user6124024
4
votes
1 answer

How to load a list of maps with viper?

I have the following config I want to load with viper: artist: name: The Beatles albums: - name: The White Album year: 1968 - name: Abbey Road year: 1969 I can't work out how to load a list of maps. I guess I need to unmarshal just…
jbrown
  • 7,518
  • 16
  • 69
  • 117
4
votes
4 answers

Mapping Nested Config Yaml to struct

I'm new to go, and I'm using viper do load all my config what currently i have is the YAML look like below countryQueries: sg: - qtype: gmap qplacetype: postal_code - qtype: gmap qplacetype: address - qtype: geocode …
Fandy Dharmawan
  • 328
  • 4
  • 13
3
votes
1 answer

How to Unmarshall Viper config value to struct containing array of string properly?

I noticed that this is perhaps a bug when viper tries to unmarshall to struct. To explain it better, consider this: I have a cli command like below dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2 Here is my command line source…
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
3
votes
1 answer

How do I unmarshal environment variables to a slice using viper?

I have the following code: package main import ( "log" "github.com/spf13/viper" ) func main() { viper.SetEnvPrefix("myprefix") viper.SetDefault("languages", []string{"french", "spanish"}) viper.BindEnv("name") …
user776942
2
votes
1 answer

Golang: Load config from file & environment variables

I'm using viper package to load config from file. My config file look like this: /// config.yml server: &server name: "Test Server" host: localhost port: 8084 database: drivers: mysql: &mysql driver: mysql host:…
Dean
  • 415
  • 1
  • 5
  • 15
2
votes
0 answers

Dynamically registering command in Cobra

is there a way of dynamically registering cobra commands based on a viper configuration file? Basically I want to only register commands that are available within my config file as "modules". My root.go's init function looks like this func init() { …
2
votes
2 answers

Golang: AWS Lambda: Fails finding spf13/viper config file: Config File "" Not Found in "[/var/task /var/config]"

Golang vesion: 1.18.5 Configuration lib: "github.com/spf13/viper" github repo I'm writing a AWS Lambda using Go. It sits behind an AWS APIGateway as a REST API. It sends out POST requests to an external API. I want to config that external API's URL…
samme4life
  • 1,143
  • 2
  • 14
  • 20
2
votes
1 answer

Can't get nested key from env to override yaml config file using viper

This is my simplified config: stripe: secret_key: sk_fromconfig Why viper don't take value from env? % echo $STRIPE_SECRET_KEY sk_fromenv % go run main.go sk_fromconfig I expect it takes value from env because I have one like this: % echo…
yusufmalikul
  • 508
  • 1
  • 9
  • 26
2
votes
1 answer

Determine if flags were actually passed in (sub)command invocation in golang's cobra/viper

I have a cobra command var mycommandCmd = &cobra.Command{ Use: "mycommand", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { viper.BindPFlags(cmd.Flags()) and a subcommand var mysubcommandCmd = &cobra.Command{ …
pkaramol
  • 16,451
  • 43
  • 149
  • 324
2
votes
1 answer

Configure a default directory path in Go with Cobra and Viper

I want the user of my CLI app to enter a specific path where they want to save files. I tried to use Cobra's --config flag but I don't know how to. Example: app --config path "~/Documents/" How could I do this?
2
votes
1 answer

Viper Automatic Environment Does Not Read from Environment

I'm trying to complete a very simple example wherein I use viper to load configuration from environment variables. I have installed it in my project via go get github.com/spf13/viper, and the dependency is in my go.mod: module…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
2
votes
1 answer

Viper mapping environment variable to struct member

Here's my config.yaml file server: port: 5000 here's my logic to unmarshal using viper type Configurations struct { Server ServerConfig } type ServerConfig struct { Port int } // LoadConfig reads configuration from file or environment…
mahendra
  • 367
  • 5
  • 18