Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make configuration fields required in Viper?

Tags:

yaml

go

viper-go

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 := viper.Unmarshal(&config)

When I miss some configs in .yml config file it doesn't throw any error (as I had guessed) during Unmarshaling.

So how can I force to have all configs implemented? I want to see errors if any field from struct doesn't have value in yaml.

like image 830
Tigran Babajanyan Avatar asked Sep 02 '20 16:09

Tigran Babajanyan


People also ask

What is Viper configuration?

Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports: setting defaults. reading from JSON, TOML, YAML, HCL, envfile and Java properties config files.

Why use Viper golang?

Viper is a very popular Golang package for this purpose. It can find, load, and unmarshal values from a config file. It supports many types of files, such as JSON, TOML, YAML, ENV, or INI. It can also read values from environment variables or command-line flags.

How do you use Viper Go?

Installing Viper is similar to installing any package in Go. The first step is to initialize the Go mod file. The best way to do this is to initialize the folder with git init . Next, set up the git origin using git remote add origin ORIGIN_URL , then initialize the project with go mod init .

How do I set up a config file in Viper?

First, we call viper.AddConfigPath () to tell Viper the location of the config file. In this case, the location is given by the input path argument. Next, we call viper.SetConfigName () to tell Viper to look for a config file with a specific name. Our config file is app.env, so its name is app.

Why use Viper for config variables?

There are many cases when we need to set up defaults for the application or load config variables from different file types, and this is where using Viper can be extremely useful. It can even live-read config variables, work with flags, and enable you to marshal and unmarshal.

How to read yml configuration files with a Viper?

There are many libraries used to manage configuration in go, this time in this story we will discuss a simple way to read yml configuration files with a viper. 2. The configuration file looks like this 3. Then install library 4. This is full code of how it’s look 5. Then run project This example only reads from the config file anyway …

Why can't I use the Viper config file with ENV Vars?

Due to the way Viper works, this cannot load from *only* env vars, so the config file is still needed and the env vars can override it. See spf13/viper#584 To help protect users from this issue, this change also adds verification that essential config settings have been configured.


1 Answers

You can integrate validator package along with viper, so that you can check for any missing configuration. Attaching code snippet and config screenshot of my working code.

package config

import (
    "github.com/go-playground/validator/v10"
    "github.com/spf13/viper"
    "log"
)

type Configuration struct {
    Server struct {
        Application string `yaml:"application" validate:"required"`
    } `yaml:"server"`
}

var config Configuration

func GetConfig() *Configuration {
    return &config
}

func init() {

    vp := viper.New()
    vp.SetConfigName("config") // name of config file (without extension)
    vp.SetConfigType("yaml")   // REQUIRED if the config file does not have the extension in the name
    vp.AddConfigPath(".")
    if err := vp.ReadInConfig(); err!=nil {
        log.Fatalf("Read error %v", err)
    }
    if err := vp.Unmarshal(&config); err!=nil {
        log.Fatalf("unable to unmarshall the config %v", err)
    }
    validate := validator.New()
    if err := validate.Struct(&config); err!=nil{
        log.Fatalf("Missing required attributes %v\n", err)
    }
}

My property screenshot property configuration

Missing property error Missing property error

like image 182
Abinash Avatar answered Nov 12 '22 06:11

Abinash