go-toml-config.v0: gopkg.in/go-kornel/go-toml-config.v0 Index | Files

package config

import "gopkg.in/go-kornel/go-toml-config.v0"

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 "gopkg.in/go-kornel/go-toml-config.v0"

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

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 config.Sets for different config files:

networkConfig = config.New("network settings", config.ExitOnError)
networkConfig.String("host", "localhost", "your computer name")
networkConfig.Int("port", 8080, "port to serve on")
networkConfig.Parse("/path/to/network.conf")

Index

Package Files

config.go set.go

Constants

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

flag.ErrorHandling defines how to handle flag parsing errors.

Variables

var CommandLine = New(os.Args[0], flag.ExitOnError)

CommandLine is the package's global default config.Set. It is an analogue of flag.CommandLine. You can set

config.CommandLine = &congig.Set{flag.CommandLine}

if you wish.

func Bool Uses

func Bool(name string, value bool, usage string) *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, usage string)

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, usage string) *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, usage string)

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, usage string) *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, usage string)

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, usage string) *int

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

func Int64 Uses

func Int64(name string, value int64, usage string) *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, usage string)

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, usage string)

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 config.Set. This must be called after all config flags have been defined but before the flags are accessed by the program.

func ParseArgs Uses

func ParseArgs() error

ParseArgs parses the command-line flags from os.Args[1:]. Must be called after all config flags are defined and before flags are accessed by the program.

func ParseString Uses

func ParseString(str string) error

ParseString takes a string representing a TOML file and loads it into the global config.Set. This must be called after all config flags have been defined but before the flags are accessed by the program.

func PrintCurrentValues Uses

func PrintCurrentValues()

PrintCurrentValues prints lines in format

flagName=flagCurrentValue

to the os.Stderr. Useful for showing current configuration to the user. The output format is subject to change.

func String Uses

func String(name string, value string, usage 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, usage 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, usage string) *uint

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

func Uint64 Uses

func Uint64(name string, value uint64, usage string) *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, usage string)

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, usage string)

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 Set Uses

type Set struct {
    *flag.FlagSet
}

A Set represents a set of defined configure flags. The zero value of a Set has no name and has ContinueOnError error handling.

func New Uses

func New(name string, errorHandling flag.ErrorHandling) *Set

New returns a new config.Set with the given name and error handling policy. The three valid error handling policies are: ContinueOnError, ExitOnError, and PanicOnError.

func (*Set) Parse Uses

func (c *Set) 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 config.Set have been defined but before the flags are accessed by the program.

func (*Set) ParseArguments Uses

func (c *Set) ParseArguments(arguments []string) error

ParseArguments parses flag definitions from the argument list, which should not include the command name. Must be called after all the config flags in the config.Set have been defined but before the flags are accessed by the program. The return value will be flag.ErrHelp if -help or -h were set but not defined.

func (*Set) ParseString Uses

func (c *Set) ParseString(str string) error

ParseString takes a string representation of a TOML file and loads it. This must be called after all the config flags in the config.Set have been defined but before the flags are accessed by the program.

func (*Set) PrintCurrentValues Uses

func (c *Set) PrintCurrentValues()

PrintCurrentValues prints lines in format

flagName=flagCurrentValue

to the os.Stderr. Useful for showing current configuration to the user. The output format is subject to change.

Package config imports 9 packages (graph) and is imported by 1 packages. Updated 2016-07-18. Refresh now. Tools for package owners.