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

Multiple config files with go-viper

Is it possible to load/merge multiple config files with Viper? Say I have a general config file containing configuration for my program, and client specific config files with configuration for each client, where one of them would be loaded,…
madshov
  • 653
  • 3
  • 9
  • 15
18
votes
1 answer

Reading in environmental Variable Using Viper Go

I am trying to make Viper read my environment variables, but its not working. Here is my configuration: # app.yaml dsn: RESTFUL_APP_DSN jwt_verification_key: RESTFUL_APP_JWT_VERIFICATION_KEY jwt_signing_key:…
Tonespy
  • 3,257
  • 7
  • 26
  • 52
12
votes
3 answers

How do I use Viper to get a value from a nested YAML structure?

How do I write the code below to get a string from my nested yaml struct? Here is my yaml: element: - one: url: http://test nested: 123 - two: url: http://test nested: 123 weather: - test: zipcode: 12345 - ca: …
Defenestrator6
  • 405
  • 1
  • 6
  • 10
12
votes
2 answers

Cobra + Viper Golang How to test subcommands?

I am developing an web app with Go. So far so good, but now I am integrating Wercker as a CI tool and started caring about testing. But my app relies heavily on Cobra/Viper configuration/flags/environment_variables scheme, and I do not know how to…
CESCO
  • 7,305
  • 8
  • 51
  • 83
11
votes
1 answer

How to make configuration fields required in Viper?

I use Viper https://github.com/spf13/viper for managing project configurations in my GO app, and also for Unmarshaling configuration values to a struct. var config c.Configuration // Configuration is my configuration struct err :=…
Tigran Babajanyan
  • 1,967
  • 1
  • 22
  • 41
9
votes
1 answer

Config file with cobra and viper

Basic information: I have created a go application and used Cobra for it. Cobra uses Viper for command line parameters and flags. I have a command listen with a flag bind and I want to configure it in a yaml file. Code: The init function of the…
Sascha
  • 10,231
  • 4
  • 41
  • 65
8
votes
1 answer

How to unmarshall viper config to struct with dash character

I have this following config file defined as toml file: [staging] project-id = "projectId" cluster-name = "cluster" zone = "asia-southeast1-a" Then, I have this struct type ConfigureOpts struct { GCPProjectID string `json:"project-id"` …
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
7
votes
1 answer

What is the difference between StringVar vs StringVarP or String vs StringP when using Go (golang), cobra and viper?

I'm maintaining some code written using Go (golang), Viper and Cobra. On one line, it has: rootCmd.PersistentFlags().String("cfg", "", "A description") And then on the following line it has rootCmd.PersistentFlags().StringP("output", "o", ".",…
M_M
  • 1,955
  • 2
  • 17
  • 23
7
votes
6 answers

Go viper .yaml values environment variables override

I'm trying to have application.yaml file in go application which contains ${RMQ_HOST} values which I want to override with environment variables. In application.yaml I've got: rmq: test: host: ${RMQ_HOST} port: ${RMQ_PORT} And in my…
Ajdin Halac
  • 153
  • 1
  • 9
7
votes
4 answers

Why is Cobra not reading my configuration file?

The documentation in Cobra and Viper are confusing me. I did cobra init fooproject and then inside the project dir I did cobra add bar. I have a PersistentFlag that is named foo and here is the init function from the root command. func Execute() { …
Joff
  • 11,247
  • 16
  • 60
  • 103
7
votes
1 answer

Working with interfaces using Viper language

I am building a little app using Viper and Cobra. At the moment, I have a yaml file like this: hosts: - name: host1 port: 90 key: my_key - name: host2 port: 90 key: prompt And I've read in the config file using Viper. When I run…
jaxxstorm
  • 12,422
  • 5
  • 57
  • 67
6
votes
3 answers

Why am I getting a nil pointer error depending on where I call BindPFlag?

I've just recently started working with Go, and I've run into some behavior working with Cobra and Viper that I'm not sure I understand. This is a slightly modified version of the sample code you get by running cobra init. In main.go I have: package…
larsks
  • 277,717
  • 41
  • 399
  • 399
6
votes
3 answers

Handling Viper Config File Path During Go Tests

So I have a pretty basic configuration with Viper reading a .env file from my base directory. I fatal kill the process if there's no .env file. All goes well when running my app normally. When I run my tests with go test -v ./.., the test framework…
Galen Howlett
  • 530
  • 4
  • 12
5
votes
1 answer

Cobra: Providing context to subcommands without using package globals?

I've written a simple CLI tool using cobra and viper. I've recently been refactoring it to avoid package global variables, largely because it turned out to be difficult to test using the layout suggested by e.g. cobra init. Instead of... var rootCmd…
larsks
  • 277,717
  • 41
  • 399
  • 399
5
votes
1 answer

Using viper to read config from envfile

I don't really understand how viper works. This is my code: configuration.go var Config *Configuration type ServerConfiguration struct { Port string } type Configuration struct { Server ServerConfiguration } func Init() { var…
xN031x
  • 53
  • 1
  • 3
1
2 3 4 5 6 7