yamlconf

package
v0.0.0-...-8b1022e Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2016 License: Apache-2.0, Apache-2.0 Imports: 11 Imported by: 0

README

yamlconf Travis CI Status Coverage Status GoDoc

Provides mechanism for managing yaml-based configuration.

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	GetVersion() int

	SetVersion(version int)

	ApplyDefaults()
}

Config is the interface for configuration objects that provide the in-memory representation of yaml configuration managed by yamlconf.

type Manager

type Manager struct {
	// FilePath: required, path to the config file on disk
	FilePath string

	// ValidateConfig: optional. If specified, the config loaded from disk is
	// validated with this function. If validation fails, the config is replaced
	// with the result of DefaultConfig.
	ValidateConfig func(cfg Config) error

	// DefaultConfig: optional. If specified, and there's a problem reading the
	// config from disk or that , this is used.
	DefaultConfig func() (Config, error)

	// EmptyConfig: required, factory for new empty Configs
	EmptyConfig func() Config

	// PerSessionSetup runs at the beginning of each session (for example applying
	// command-line flags)
	PerSessionSetup func(currentCfg Config) error

	// CustomPoll: optionally, specify a custom polling function that returns
	// a mutator for applying the result of polling, the time to wait till the
	// next poll, and an error (if polling itself failed). This is useful for
	// example for fetching config updates from a remote server.
	CustomPoll func(currentCfg Config) (mutate func(cfg Config) error, waitTime time.Duration, err error)

	// Obfuscate: if true, the on-disk version of the config will be obfuscated by
	// "encrypting" it with ROT13.
	Obfuscate bool
	// contains filtered or unexported fields
}

Manager exposes a facility for managing configuration a YAML configuration file. After creating a Manager, one must call the Init() method to start the necessary background processing. If you set a CustomPoll function, you need to call StartPolling() also.

As the configuration is updated, the updated version of the config is made available via the Next() method. Configs are always copied, never updated in place.

The config can be updated in several ways:

1. Programmatically - Clients can call the Manager's Update() method. 2. Updating the file on disk directly 3. Using the optional HTTP config server 4. Optionally specifying a custom polling mechanism (e.g. for fetching updates) from a server.

When the file on disk is updated, Manager uses optimistic locking to make sure that manual updates to the file don't overwrite intervening programmatic updates. Specifically, the Config includes a Version field. Every time that a programmatic update is made, the Version field is incremented. If someone edits the file and then saves it, but there was an intervening programmatic update, the Version in the file will not match the Version in memory, and the file will be rejected and overwritten with the latest Version from memory.

Programmatic updates (including ones via the HTTP config server and custom polling) are processed serialy. Since these operations are all defined as mutators that receive the current version of the config, the order of processing doesn't really matter.

The optional HTTP config server provides an HTTP REST endpoint that allows making updates to portions of the config. The portion of the config is identified by the path. The allowed operations are POST (insert/update) and DELETE (delete), both of which expect a YAML fragment in their body.

For example, given a config like this:

items:
  a:
    description: Item A
    price: 55
  b:
    description: Item B
    price: 23

If applying the following sequence of calls:

POST /items/a "price: 56"
DELETE /items/b
POST /items/c "description: Item C\nprice:19"

We would end up with the following configuration:

items:
  a:
    description: Item A
    price: 56
  c:
    description: Item C
    price: 19

func (*Manager) Init

func (m *Manager) Init() (Config, error)

Init starts the Manager, returning the initial Config (i.e. what was on disk). If no config exists on disk, an empty config with ApplyDefaults() will be created and saved.

func (*Manager) Next

func (m *Manager) Next() Config

Next gets the next version of the Config, blocking until the config is updated.

func (*Manager) StartPolling

func (m *Manager) StartPolling()

StartPolling starts polling if there is a custom polling function defined.

func (*Manager) Update

func (m *Manager) Update(mutate func(cfg Config) error) error

Update updates the config by using the given mutator function.

Jump to

Keyboard shortcuts

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