autoconfig

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2019 License: Apache-2.0 Imports: 10 Imported by: 5

README

Documentation Build Status

go-autoconfig

JSON & YAML based configuration library with automatic reload.

Example Usage

import "github.com/dparrish/go-autoconfig"

func main() {
	// Load the initial configuration.
	config := autoconfig.New("config.yaml")

	// Ensure that hash.value.here is always set in configuration, even after it changes.
	config.Required("hash.value.here")

	// Ensure that hash.value.cannot.change doesn't change on configuration reload.
	config.Immutable("hash.value.cannot.change")

	// Validate values whenever configuration is loaded.
	config.AddValidator(func(old, new *autoconfig.Config) error {
		if new.GetInt("intvalue") > 1000 {
			return errors.New("intvalue cannot be above 100")
		}
		return nil
	})

	if err := config.Load(); err != nil {
		panic("Configuration is invalid")
	}

	// Start a background goroutine that will reload configuration whenever it changes.
	if err := config.Watch(context.Background()); err != nil {
		panic(err)
	}

	...

	// Read config values in your code. These will always return the latest loaded value.
	log.Printf("String value: %s", config.Get("hash.stringvalue"))
	log.Printf("Int value: %d", config.GetInt("hash.intvalue"))
	log.Printf("List value: %v", config.GetAll("hash.listvalue"))
}

Documentation

Overview

Package autoconfig wraps a JSON or YAML configuration stored on disk that is queryable using the Get* functions.

The configuration file will be watched for changes after the initial load. Whenever the file has changed, each validation function will be called in the order they were added.

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type Config

type Config struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Config wraps a JSON/YAML configuration stored on disk and provides functions to query it.

func New

func New(filename string) *Config

New creates a new empty configuration.

func (*Config) AddValidator

func (c *Config) AddValidator(f func(old, new *Config) error)

AddValidator adds a function that will be called whenever the config file changes. The function will be passed both the old and new configurations. If the function returns an error, the new configuration will not be applied. The validation function *may* modify the new config but *must not* modify the old config.

func (*Config) Default added in v1.2.0

func (c *Config) Default(key string, value interface{})

Default sets the default value of an entry.

func (*Config) Get

func (c *Config) Get(path string) string

Get looks up a configuration item in dotted path notation and returns the first (or only) value in string form. Example: c.Get("spanner.database.path")

func (*Config) GetAll

func (c *Config) GetAll(path string) []string

Get looks up a configuration item in dotted path notation and returns a list of values.

func (*Config) GetFloat

func (c *Config) GetFloat(path string) float64

GetFloat looks up a configuration item in dotted path notation and returns the first (or only) value in float64 form.

func (*Config) GetInt

func (c *Config) GetInt(path string) int

GetFloat looks up a configuration item in dotted path notation and returns the first (or only) value in int form.

func (*Config) GetMapList added in v1.2.1

func (c *Config) GetMapList(path string) []map[string]interface{}

GetMapList looks up a configuration item and returns a list of maps for each.

func (*Config) GetRaw

func (c *Config) GetRaw(path string) interface{}

GetRaw looks up the raw configuration item and does not do any conversion to a particular type. This is generally only used by the other Get* functions but is exposed for convenience.

func (*Config) Immutable

func (c *Config) Immutable(key string)

Immutable marks a configuration entry as immutable. If the value changes when the configuration is updated, the new configuration will be rejected.

func (*Config) Load

func (c *Config) Load() error

Load loads a configuration file from disk.

func (*Config) Required

func (c *Config) Required(key string)

Required marks a configuration entry as required. If the value is missing when the configuration changes, the new configuration will be rejected.

func (*Config) Watch

func (c *Config) Watch(ctx context.Context) error

Watch starts a background goroutine to watch for changes in the configuration. When changes are detected, the validator functions are called with the new configuration.

Jump to

Keyboard shortcuts

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