Documentation ¶
Overview ¶
Package cfg provides a simple mechanism for creating and loading configuration files.
The configuration files are saved using the TOML file format.
Modules need not worry about dealing with configuration files. They simply need to implement the Configurable interface.
Index ¶
Constants ¶
const ( // ConfZeroPID is the default PeerID set before any migration happens. // The real peerID will be generated to replace this one. ConfZeroPID = "QmRK6DGwgkzpHRG5rCgWUxTSKvQtmhZeMa5jdeHYTtxnNq" // ConfZeroPK is the default private key set before any migration happens. // The real private key will be generated to replace this one. ConfZeroPK = "" /* 1596-byte string literal not displayed */ )
Variables ¶
var ( // ErrUnexistingKey is the error returned when trying to get/set an unknown key. ErrUnexistingKey = errors.New("setting not found") // ErrEditGroupConfig is the error returned when trying to set // group of configuration attributes. ErrEditGroupConfig = errors.New("cannot edit a group of attribute") )
var ( // ErrInvalidVersion is returned when the config version is invalid. ErrInvalidVersion = errors.New("config version is invalid") // ErrInvalidSubtree is returned when a config subtree is invalid. ErrInvalidSubtree = errors.New("subtree is invalid") // ErrOutdatedExec is returned when the config version is more recent // than the executable. ErrOutdatedExec = errors.New("exec is out of date with config version") )
var ( // ErrInvalidGroup is returned when a configuration file contains // an invalid service group. ErrInvalidGroup = errors.New("the service group is invalid") )
Functions ¶
func Migrate ¶
func Migrate( set Set, filename string, versionKey string, migrations []MigrateHandler, perms os.FileMode, ) error
Migrate loads a TOML file and sets the configurations of a set of configurables, applying migrations if needed.
The version key should point to the tree path that contains the int config file version for global migrations.
The migrations should be a slice of MigrateHandler that upgrade the configuration from the version corresponding to its index in the slice to the next version.
Each configurable can have its own migrations if it implements the Migrator interface.
Types ¶
type ConfigSaveOpts ¶
ConfigSaveOpts are the options passed to ConfigSet.Save().
type ConfigSet ¶
type ConfigSet map[string]interface{}
ConfigSet represents a set of configurations.
type Configurable ¶
type Configurable interface { // ID returns the unique identifier of the configurable. ID() string // Config should return the current configuration or a default // configuration if it wasn't set. Config() interface{} // SetConfig should configure the configurable. SetConfig(interface{}) error }
Configurable represents something that can be configured.
type MigrateHandler ¶
MigrateHandler mutates a TOML tree to update it from one version to the next.
type Migrator ¶
type Migrator interface { // Version key returns the key containing the current int version relative // to the subtree of the configurable. VersionKey() string // Migrations returns all the migrations from first to last. Keys are // relative to the subtree of the configurable. Migrations() []MigrateHandler }
Migrator declares migrations specific to a configurable.
type Set ¶
type Set map[string]Configurable
Set represents a set of configurables.
func NewSet ¶
func NewSet(configurables []Configurable) Set
NewSet returns a new set of configurables.
func (Set) Get ¶
Get returns the value of the tree indexed by the provided key. The key is a dot-separated path (e.g. a.b.c) without single/double quoted strings.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree is used to modify the configuration tree.
func TreeFromMap ¶
TreeFromMap creates a tree from a map.
func (*Tree) AddServiceToGroup ¶
AddServiceToGroup adds a service to an existing group if it isn't already part of the group.
If the group is not found, it prints a warning and doesn't return an error. The user could have intentionally removed the group.
func (*Tree) Get ¶
Get returns the value of a key.
The key can be a path such as "core.boot_service".
func (*Tree) GetDefault ¶
GetDefault returns the value of a key or a default value.
The key can be a path such as "core.boot_service".