ajpauwels / pit-of-vipers

Managing many viper instances
MIT License
10 stars 1 forks source link

A pit of vipers

Objective

Do you use golang? Do you use viper for configuration management within golang? Are you sick and tired of:

Look no more! Pit of Vipers ingests as many viper instances as you want and:

How to

TL;DR:

package main

import (
    "fmt"
    viperpit "github.com/ajpauwels/pit-of-vipers"
)

type Config struct {
    Host      string `mapstructure:"host"`
    Port      uint16 `mapstructure:"port"`
    SecretKey string `mapstructure:"secretkey"`
    NewKey    string `mapstructure:"newkey"`
}

func main() {
    vpCh, errCh := viperpit.NewFromPaths([]string{"./config", "./config/shared", "./config/app", "./config/preview/shared", "./config/preview/app"}) // (1)
    for { // (2)
        select {
        case vp := <-vpCh: // (3)
            var config Config
            vp.Unmarshal(&config)
            fmt.Printf("%+v\n", config)
        case err := <-errCh: // (4)
            fmt.Errorf("%s", err)
        }
    }
}

Explanation of highlighted portions below:

  1. Calling a New* function on viperpit returns two channels: the viper channel which receives a merged viper instance every time one of the sub-instances is updated, and an error channel which receives all errors which occurred in this process

  2. Main thread just loops infinitely on a channel select statement, waiting for config updates or errors in the config update process

  3. The <-vpCh case receives a fully merged viper instance every time one of the sub-instances is updated from the filesystem

  4. The <-errCh case receives any errors that may have occurred during the merging process