Documentation
¶
Overview ¶
GoFigure is a small utility library for reading configuration files. It's usefuly especially if you want to load many files recursively (think /etc/apache2/mods-enabled/*.conf).
It can support multiple formats, as long as you take a file and unmarshal it into a struct containing your configurations. Right now the only implemented formats are YAML and JSON files, but feel free to add more :)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLoader = NewLoader(yaml.Decoder{}, true)
DefaultDecoder is a yaml based decoder that can be used for convenience
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder interface {
// Decode reads data from the io stream, and unmarshals it to the config struct.
Decode(r io.Reader, config interface{}) error
// CanDecode should return true if a file is decode-able by the decoder,
// based on extension or similar mechanisms
CanDecode(path string) bool
}
Decoder is the interface for config decoders (right now we've just implemented a YAML one)
type Loader ¶
type Loader struct {
// StrictMode determines whether the loader will completely fail on any IO or decoding error,
// or whether it will continue traversing all files even if one of them is invalid.
StrictMode bool
// contains filtered or unexported fields
}
Loader traverses directories recursively and lets the decoder decode relevant files.
It can also explicitly decode single files
Example ¶
// create our configuration container
var conf = &struct {
Redis struct {
Server string
Monitor int
Timeout int
}
}{}
//if we set some default, the loader will override it
conf.Redis.Server = "localhost:6377"
// init a loader with a YAML decoder in strict mode
loader := NewLoader(yaml.Decoder{}, true)
// run recursively on the testdata directory
err := loader.LoadRecursive(conf, "./testdata")
if err != nil {
panic(err)
}
fmt.Println(conf.Redis.Server)
Output: localhost:6379
func NewLoader ¶
NewLoader creates and returns a new Loader wrapping a decoder, using strict mode if specified
func (Loader) LoadFile ¶
LoadFile takes a pointer to a struct containing configurations, and a path to a file, and uses the decoder to read the file's contents into the struct. It returns an error if the file could not be opened or properly decoded
func (Loader) LoadRecursive ¶
LoadRecursive takes a pointer to a struct containing configurations, and a series of paths. It then traverses the paths recursively in their respective order, and lets the decoder decode every relevant file.
type ReloadFunc ¶
type ReloadFunc func()
ReloadFunc can be used to make a simple func conform to the Reloader interface
func (ReloadFunc) Reload ¶
func (f ReloadFunc) Reload()
type ReloadMonitor ¶
type ReloadMonitor interface {
Watch(Reloader)
Stop()
}
ReloadMonitor is an interface for waiting for external notifications that we need to reload our configs. The only implementation right now is a SIGHUP listener
type Reloader ¶
type Reloader interface {
Reload()
}
Reloader is an interface to be implemented by the calling program, that gets called when we need to reload our configs
type SignalMonitor ¶
type SignalMonitor struct {
// contains filtered or unexported fields
}
SignalMonitor is a monitor that waits for SIGHUP and calls its Reloader
func NewSignalMonitor ¶
func NewSignalMonitor() *SignalMonitor
NewSignalMonitor creates a new signal monitor
func (*SignalMonitor) Monitor ¶
func (m *SignalMonitor) Monitor(r Reloader)
Monitor waits for SIGHUP and calls the reloader's Reload method