Documentation
¶
Overview ¶
Package configure is an easy to use multi-layer configuration system.
Examples can be found in the example folder (http://github.com/paked/configure/blob/master/examples/) as well as a getting started guide in the main README file (http://github.com/paked/configure).
configure makes use of Checkers, which are used to retrieve values from their respective data sources. There are three built in Checkers, Environment, Flag and JSON. Environment retrieves environment variables. Flag retrieves variables within the flags of a command. JSON retrieves values from a JSON file/blob. Checkers can be essentially thought of as "middlewear for configuration", in fact parts of the package API was inspired by negroni (https://github.com/codegangsta/negroni, the awesome net/http middlewear manager) and the standard library's flag package.
It is very easy to create your own Checkers, all they have to do is satisfy the Checker interface.
type Checker interface { Int(name string) (int, error) Bool(name string) (int, error) String(name string) (string, error) Setup() error }
That is an, Int method, String method and a Bool method. These functions are used to retrieve their respective data types. A setup method is also required, where the Checker should initialize itself and throw any errors.
If you do create your own Checkers I would be more than happy to add a link to the README in the github repository.
Index ¶
- type Checker
- type Configure
- func (c *Configure) Bool(name string, def bool, description string) *bool
- func (c *Configure) BoolVar(value *bool, name string, def bool, description string)
- func (c *Configure) Int(name string, def int, description string) *int
- func (c *Configure) IntVar(value *int, name string, def int, description string)
- func (c *Configure) Parse()
- func (c *Configure) String(name string, def string, description string) *string
- func (c *Configure) StringVar(value *string, name string, def string, description string)
- func (c *Configure) Use(checkers ...Checker)
- type Environment
- type Flag
- type HCL
- type JSON
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Checker ¶
type Checker interface { // Setup initializes the Checker Setup() error // Int attempts to get an int value from the data source. Int(name string) (int, error) // String attempts to get a string value from the data source. String(name string) (string, error) // Bool attempts to get a bool value from the data source. Bool(name string) (bool, error) }
Checker is the interface which external checkers must comply to. In the configure implementation, if one Checker fails the next one in the stack should be called.
type Configure ¶
type Configure struct {
// contains filtered or unexported fields
}
Configure is a stack of Checkers which are used to retrieve configuration values. It has a similar API as the flag package in the standard library, and is also partially inspired by negroni. Checkers are evaluated in the same order they are added.
func New ¶
New returns a pointer to a new Configure instance with a stack provided through the variadic stack variable.
func (*Configure) Bool ¶
Bool defines a bool flag with a name, default and description. The return value is a pointer which will be populated with the value of the flag.
func (*Configure) Int ¶
Int defines an int flag with a name, default and description. The return value is a pointer which will be populated with the value of the flag.
func (*Configure) Parse ¶
func (c *Configure) Parse()
Parse populates all of the defined arguments with their values provided by the stacks Checkers.
func (*Configure) String ¶
String defines a string flag with a name, default and description. The return value is a pointer which will be populated with the value of the flag.
type Environment ¶
type Environment struct { }
Environment is a Checker. It retrieves values from the host OS's environment variables. In this process, it will take a flag-like name, and convert it to a environmnet-like name. The process for this is to change all characters to upper case, and then replace hyphons with underscores.
func NewEnvironment ¶
func NewEnvironment() *Environment
NewEnvironment creates a new instance of the Environment Checker.
func (*Environment) Bool ¶
func (e *Environment) Bool(name string) (bool, error)
Bool returns a bool if it exists in the set environment variables.
func (Environment) Int ¶
func (e Environment) Int(name string) (int, error)
Int returns an int if it exists in the set environment variables.
func (Environment) Setup ¶
func (e Environment) Setup() error
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag is an ULTRA simple flag parser for strings, ints and bools. It expects flags in the format --x=2 (where x is the var name and 2 is the value). It can be provided with a "usage printer", which by default will be printed when the "-h" flag is used.
func NewFlag ¶
func NewFlag() *Flag
NewFlag returns a new instance of the Flag Checker, using os.Args as its flag source.
func NewFlagWithUsage ¶
NewFlagWithUsage returns a new instance of the Flag Checker with a custom usage printer. It uses os.Args as its flag source.
type HCL ¶
type HCL struct {
// contains filtered or unexported fields
}
HCL represents the HCL Checker. It reads an io.Reader and then pulls a value out of a map[string]interface{}.
func NewHCL ¶
NewHCL returns an instance of the HCL checker. It takes a function which returns an io.Reader which will be called when the first value is recalled. The contents of the io.Reader MUST be decodable into HCL or JSON.
func NewHCLFromFile ¶
NewHCLFromFile returns an instance of the HCL checker. It reads its data from a file (file content can be HCL or JSON) which its location has been specified through the path parameter
type JSON ¶
type JSON struct {
// contains filtered or unexported fields
}
JSON represents the JSON Checker. It reads an io.Reader and then pulls a value out of a map[string]interface{}.
func NewJSON ¶
NewJSON returns an instance of the JSON checker. It takes a function which returns an io.Reader which will be called when the first value is recalled. The contents of the io.Reader MUST be decodable into JSON.
func NewJSONFromFile ¶
NewJSONFromFile returns an instance of the JSON checker. It reads its data from a file which its location has been specified through the path parameter