configure: github.com/paked/configure Index | Files | Directories

package configure

import "github.com/paked/configure"

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

Package Files

configure.go doc.go environment.go flag.go hcl.go json.go

type Checker Uses

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 Uses

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 Uses

func New(stack ...Checker) *Configure

New returns a pointer to a new Configure instance with a stack provided through the variadic stack variable.

func (*Configure) Bool Uses

func (c *Configure) Bool(name string, def bool, description string) *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) BoolVar Uses

func (c *Configure) BoolVar(value *bool, name string, def bool, description string)

BoolVar binds a provided *bool to a flag value.

func (*Configure) Int Uses

func (c *Configure) Int(name string, def int, description string) *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) IntVar Uses

func (c *Configure) IntVar(value *int, name string, def int, description string)

IntVar binds a provided *int to a flag value.

func (*Configure) Parse Uses

func (c *Configure) Parse()

Parse populates all of the defined arguments with their values provided by the stacks Checkers.

func (*Configure) String Uses

func (c *Configure) String(name string, def string, description string) *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.

func (*Configure) StringVar Uses

func (c *Configure) StringVar(value *string, name string, def string, description string)

StringVar binds a provided *string to a flag value.

func (*Configure) Use Uses

func (c *Configure) Use(checkers ...Checker)

Use adds a variable amount of Checkers onto the stack.

type Environment Uses

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 Uses

func NewEnvironment() *Environment

NewEnvironment creates a new instance of the Environment Checker. Note: If you request a value "peanut-butter", the environment variable that will be checked is `PEANUT_BUTTER`.

func (*Environment) Bool Uses

func (e *Environment) Bool(name string) (bool, error)

Bool returns a bool if it exists in the set environment variables.

func (Environment) Int Uses

func (e Environment) Int(name string) (int, error)

Int returns an int if it exists in the set environment variables.

func (Environment) Setup Uses

func (e Environment) Setup() error

func (Environment) String Uses

func (e Environment) String(name string) (string, error)

String returns a string if it exists in the set environment variables.

type Flag Uses

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 Uses

func NewFlag() *Flag

NewFlag returns a new instance of the Flag Checker, using os.Args as its flag source.

func NewFlagWithUsage Uses

func NewFlagWithUsage(defaulter func() string) *Flag

NewFlagWithUsage returns a new instance of the Flag Checker with a custom usage printer. It uses os.Args as its flag source.

func (*Flag) Bool Uses

func (f *Flag) Bool(name string) (bool, error)

Bool returns a bool if it exists in the set flags.

func (Flag) Int Uses

func (f Flag) Int(name string) (int, error)

Int returns an int if it exists in the set flags.

func (Flag) Setup Uses

func (f Flag) Setup() error

func (Flag) String Uses

func (f Flag) String(name string) (string, error)

String returns a string if it exists in the set flags.

type HCL Uses

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 Uses

func NewHCL(gen func() (io.Reader, error)) *HCL

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 Uses

func NewHCLFromFile(path string) *HCL

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

func (*HCL) Bool Uses

func (h *HCL) Bool(name string) (bool, error)

Bool returns a bool if it exists within the HCL io.Reader.

func (*HCL) Int Uses

func (h *HCL) Int(name string) (int, error)

Int returns an int if it exists within the HCL io.Reader

func (*HCL) Setup Uses

func (h *HCL) Setup() error

Setup initializes the HCL Checker

func (*HCL) String Uses

func (h *HCL) String(name string) (string, error)

String returns a string if it exists within the HCL io.Reader.

type JSON Uses

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 Uses

func NewJSON(gen func() (io.Reader, error)) *JSON

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 Uses

func NewJSONFromFile(path string) *JSON

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

func (*JSON) Bool Uses

func (j *JSON) Bool(name string) (bool, error)

Bool returns a bool if it exists within the marshalled JSON io.Reader.

func (*JSON) Int Uses

func (j *JSON) Int(name string) (int, error)

Int returns an int if it exists within the marshalled JSON io.Reader.

func (*JSON) Setup Uses

func (j *JSON) Setup() error

Setup initializes the JSON Checker

func (*JSON) String Uses

func (j *JSON) String(name string) (string, error)

String returns a string if it exists within the marshalled JSON io.Reader.

Directories

PathSynopsis
example
example/hcl

Package configure imports 9 packages (graph) and is imported by 12 packages. Updated 2019-02-18. Refresh now. Tools for package owners.