go-toml-config: github.com/stvp/go-toml-config Index | Files

package config

import "github.com/stvp/go-toml-config"

Package config implements simple TOML-based configuration variables, based on the flag package in the standard Go library (In fact, it's just a simple wrapper around flag.FlagSet). It is used in a similar manner, minus the usage strings and other command-line specific bits.

Usage:

Given the following TOML file:

country = "USA"

[atlanta]
enabled = true
population = 432427
temperature = 99.6

Define your config variables and give them defaults:

import "github.com/stvp/go-toml-config"

var (
	country            = config.String("country", "Unknown")
	atlantaEnabled     = config.Bool("atlanta.enabled", false)
	alantaPopulation   = config.Int("atlanta.population", 0)
	atlantaTemperature = config.Float("atlanta.temperature", 0)
)

After all the config variables are defined, load the config file to overwrite the default values with the user-supplied config settings:

if err := config.Parse("/path/to/myconfig.conf"); err != nil {
	panic(err)
}

You can also create separate ConfigSets for different config files:

networkConfig = config.NewConfigSet("network settings", config.ExitOnError)
networkConfig.String("host", "localhost")
networkConfig.Int("port", 8080)
networkConfig.Parse("/path/to/network.conf")

Index

Package Files

config.go

Constants

const (
    ContinueOnError flag.ErrorHandling = flag.ContinueOnError
    ExitOnError     flag.ErrorHandling = flag.ExitOnError
    PanicOnError    flag.ErrorHandling = flag.PanicOnError
)

func Bool Uses

func Bool(name string, value bool) *bool

Bool defines a bool config variable with a given name and default value.

func BoolVar Uses

func BoolVar(p *bool, name string, value bool)

BoolVar defines a bool config with a given name and default value. The argument p points to a bool variable in which to store the value of the config.

func Duration Uses

func Duration(name string, value time.Duration) *time.Duration

Duration defines a time.Duration config variable with a given name and default value.

func DurationVar Uses

func DurationVar(p *time.Duration, name string, value time.Duration)

DurationVar defines a time.Duration config with a given name and default value. The argument p points to a time.Duration variable in which to store the value of the config.

func Float64 Uses

func Float64(name string, value float64) *float64

Float64 defines a float64 config variable with a given name and default value.

func Float64Var Uses

func Float64Var(p *float64, name string, value float64)

Float64Var defines a float64 config with a given name and default value. The argument p points to a float64 variable in which to store the value of the config.

func Int Uses

func Int(name string, value int) *int

Int defines a int config variable with a given name and default value.

func Int64 Uses

func Int64(name string, value int64) *int64

Int64 defines a int64 config variable with a given name and default value.

func Int64Var Uses

func Int64Var(p *int64, name string, value int64)

Int64Var defines a int64 config with a given name and default value. The argument p points to a int64 variable in which to store the value of the config.

func IntVar Uses

func IntVar(p *int, name string, value int)

IntVar defines a int config with a given name and default value. The argument p points to a int variable in which to store the value of the config.

func Parse Uses

func Parse(path string) error

Parse takes a path to a TOML file and loads it into the global ConfigSet. This must be called after all config flags have been defined but before the flags are accessed by the program.

func String Uses

func String(name string, value string) *string

String defines a string config variable with a given name and default value.

func StringVar Uses

func StringVar(p *string, name string, value string)

StringVar defines a string config with a given name and default value. The argument p points to a string variable in which to store the value of the config.

func Uint Uses

func Uint(name string, value uint) *uint

Uint defines a uint config variable with a given name and default value.

func Uint64 Uses

func Uint64(name string, value uint64) *uint64

Uint64 defines a uint64 config variable with a given name and default value.

func Uint64Var Uses

func Uint64Var(p *uint64, name string, value uint64)

Uint64Var defines a uint64 config with a given name and default value. The argument p points to a uint64 variable in which to store the value of the config.

func UintVar Uses

func UintVar(p *uint, name string, value uint)

UintVar defines a uint config with a given name and default value. The argument p points to a uint variable in which to store the value of the config.

type ConfigSet Uses

type ConfigSet struct {
    *flag.FlagSet
}

func NewConfigSet Uses

func NewConfigSet(name string, errorHandling flag.ErrorHandling) *ConfigSet

NewConfigSet returns a new ConfigSet with the given name and error handling policy. The three valid error handling policies are: flag.ContinueOnError, flag.ExitOnError, and flag.PanicOnError.

func (*ConfigSet) Bool Uses

func (c *ConfigSet) Bool(name string, value bool) *bool

Bool defines a bool config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) BoolVar Uses

func (c *ConfigSet) BoolVar(p *bool, name string, value bool)

BoolVar defines a bool config with a given name and default value for a ConfigSet. The argument p points to a bool variable in which to store the value of the config.

func (*ConfigSet) Duration Uses

func (c *ConfigSet) Duration(name string, value time.Duration) *time.Duration

Duration defines a time.Duration config variable with a given name and default value.

func (*ConfigSet) DurationVar Uses

func (c *ConfigSet) DurationVar(p *time.Duration, name string, value time.Duration)

DurationVar defines a time.Duration config with a given name and default value for a ConfigSet. The argument p points to a time.Duration variable in which to store the value of the config.

func (*ConfigSet) Float64 Uses

func (c *ConfigSet) Float64(name string, value float64) *float64

Float64 defines a float64 config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) Float64Var Uses

func (c *ConfigSet) Float64Var(p *float64, name string, value float64)

Float64Var defines a float64 config with a given name and default value for a ConfigSet. The argument p points to a float64 variable in which to store the value of the config.

func (*ConfigSet) Int Uses

func (c *ConfigSet) Int(name string, value int) *int

Int defines a int config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) Int64 Uses

func (c *ConfigSet) Int64(name string, value int64) *int64

Int64 defines a int64 config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) Int64Var Uses

func (c *ConfigSet) Int64Var(p *int64, name string, value int64)

Int64Var defines a int64 config with a given name and default value for a ConfigSet. The argument p points to a int64 variable in which to store the value of the config.

func (*ConfigSet) IntVar Uses

func (c *ConfigSet) IntVar(p *int, name string, value int)

IntVar defines a int config with a given name and default value for a ConfigSet. The argument p points to a int variable in which to store the value of the config.

func (*ConfigSet) Parse Uses

func (c *ConfigSet) Parse(path string) error

Parse takes a path to a TOML file and loads it. This must be called after all the config flags in the ConfigSet have been defined but before the flags are accessed by the program.

func (*ConfigSet) String Uses

func (c *ConfigSet) String(name string, value string) *string

String defines a string config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) StringVar Uses

func (c *ConfigSet) StringVar(p *string, name string, value string)

StringVar defines a string config with a given name and default value for a ConfigSet. The argument p points to a string variable in which to store the value of the config.

func (*ConfigSet) Uint Uses

func (c *ConfigSet) Uint(name string, value uint) *uint

Uint defines a uint config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) Uint64 Uses

func (c *ConfigSet) Uint64(name string, value uint64) *uint64

Uint64 defines a uint64 config variable with a given name and default value for a ConfigSet.

func (*ConfigSet) Uint64Var Uses

func (c *ConfigSet) Uint64Var(p *uint64, name string, value uint64)

Uint64Var defines a uint64 config with a given name and default value for a ConfigSet. The argument p points to a uint64 variable in which to store the value of the config.

func (*ConfigSet) UintVar Uses

func (c *ConfigSet) UintVar(p *uint, name string, value uint)

UintVar defines a uint config with a given name and default value for a ConfigSet. The argument p points to a uint variable in which to store the value of the config.

Package config imports 9 packages (graph) and is imported by 32 packages. Updated 2017-05-25. Refresh now. Tools for package owners.