config

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 3 Imported by: 2

README

A pit of vipers

Objective

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

  • Only handling one config file at a time

  • Being thread-unsafe

  • Having to write dodgy synchronization code to merge multiple config files from multiple source deterministically

  • Never going through the effort of live-reloading config on filesystem updates

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

  • Deterministically merges them in the order provided

  • Updates the merged config every time a sub-instance receives an update from the filesystem

  • Forces use of live-reloaded config

  • Does all of this in a thread-safe manner

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(vipers []*viper.Viper) (viperChannel chan *viper.Viper, errChannel chan error)

/ NewFromPaths takes as input an array of vipers and creates a / ViperPit that monitors and merges and each one

func NewFromPaths

func NewFromPaths(paths []string) (viperChannel chan *viper.Viper, errChannel chan error)

/ NewFromPaths takes as input an array of paths and creates a / ViperPit that monitors each available file named config.* at those / paths.

func NewFromPathsAndName

func NewFromPathsAndName(paths []string, name string) (viperChannel chan *viper.Viper, errChannel chan error)

/ NewFromPathsAndName takes as input an array of paths and the name / of the config file (without extension) potentially stored at those / paths and creates a ViperPit that monitors each available config.

Types

type ViperPit

type ViperPit struct {
	// contains filtered or unexported fields
}

/ ViperPit stores many viper instances and merges them into one

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL