konfig

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: ISC Imports: 16 Imported by: 0

Documentation

Overview

Package konfig is a minimal and unopinionated library for reading configuration values in Go applications based on The 12-Factor App (https://12factor.net/config).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pick

func Pick(config interface{}, opts ...Option) error

Pick reads values for exported fields of a struct from either command-line flags, environment variables, or configuration files. Default values can also be specified. You should pass the pointer to a struct for config; otherwise you will get an error.

Example
package main

import (
	"fmt"
	"net/url"
	"time"

	"github.com/moorara/acai/konfig"
)

func main() {
	// First, you need to define a struct.
	// Each field of the struct represents a configuration value.
	// Create an object from the struct with default values for its fields.
	var config = struct {
		LogLevel    string
		Environment string
		Region      string
		Timeout     time.Duration
		Replicas    []url.URL
	}{
		LogLevel:    "info",           // default
		Environment: "dev",            // default
		Region:      "local",          // default
		Timeout:     10 * time.Second, // default
	}

	// Second, pass the pointer to the struct object to the Pick method.
	// For each field, a value will be read either from flags, environment variables, or files.
	_ = konfig.Pick(&config)

	// Now, you can access the configuration values on the struct object.
	fmt.Printf("%+v\n", config)
}
Output:

func Watch

func Watch(config sync.Locker, subscribers []chan Update, opts ...Option) (func(), error)

Watch first reads values for exported fields of a struct from either command-line flags, environment variables, or configuration files. It then watches any change to those fields that their values are read from configuration files and notifies subscribers on a channel.

Example
package main

import (
	"sync"

	"github.com/moorara/acai/konfig"
)

func main() {
	// When using the Watch method, your struct needs to implement the sync.Locker interface.
	// You can simply achieve that by embedding the sync.Mutex type in your struct.
	var config = struct {
		sync.Mutex
		LogLevel string
	}{
		LogLevel: "info", // default
	}

	// For using the Watch method, you need to define a channel for receiving updates.
	// If a configuration value gets a new value (through files), you will get notified on this channel.
	ch := make(chan konfig.Update, 1)

	// In a separate goroutine, you can receive the new configuration values and re-configure your application accordingly.
	go func() {
		for update := range ch {
			if update.Name == "LogLevel" {
				config.Lock()
				// logger.SetLevel(config.LogLevel)
				config.Unlock()
			}
		}
	}()

	// You can now watch for configuration values.
	close, _ := konfig.Watch(&config, []chan konfig.Update{ch})
	defer close()
}
Output:

Types

type Option

type Option func(*reader)

Option sets optional parameters for reader.

func Debug

func Debug(verbosity uint) Option

Debug is the option for enabling logs for debugging purposes. verbosity is the verbosity level of logs. You can also enable this option by setting KONFIG_DEBUG environment variable to a verbosity level. You should not use this option in production.

func ListSep

func ListSep(sep string) Option

ListSep is the option for specifying list separator for all fields with slice type. You can specify a list separator for each field using `sep` struct tag. Using `tag` struct tag for a field will override this option for that field.

func PrefixEnv

func PrefixEnv(prefix string) Option

PrefixEnv is the option for prefixing all environment variable names with a given string. You can specify a custom name for environment variable for each field using `env` struct tag. Using `env` struct tag for a field will override this option for that field.

func PrefixFileEnv

func PrefixFileEnv(prefix string) Option

PrefixFileEnv is the option for prefixing all file environment variable names with a given string. You can specify a custom name for file environment variable for each field using `fileenv` struct tag. Using `fileenv` struct tag for a field will override this option for that field.

func PrefixFlag

func PrefixFlag(prefix string) Option

PrefixFlag is the option for prefixing all flag names with a given string. You can specify a custom name for command-line flag for each field using `flag` struct tag. Using `flag` struct tag for a field will override this option for that field.

func SkipEnv

func SkipEnv() Option

SkipEnv is the option for skipping environment variables as a source for all fields. You can skip environment variables as a source for each field by setting `env` struct tag to `-`.

func SkipFileEnv

func SkipFileEnv() Option

SkipFileEnv is the option for skipping file environment variables as a source for all fields. You can skip file environment variable as a source for each field by setting `fileenv` struct tag to `-`.

func SkipFlag

func SkipFlag() Option

SkipFlag is the option for skipping command-line flags as a source for all fields. You can skip command-line flag as a source for each field by setting `flag` struct tag to `-`.

func Telepresence

func Telepresence() Option

Telepresence is the option for reading files when running in a Telepresence shell. If the TELEPRESENCE_ROOT environment variable exist, files will be read from mounted volume. See https://telepresence.io/howto/volumes.html for details.

type Update

type Update struct {
	Name  string
	Value interface{}
}

Update represents a configuration field that received a new value.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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