config

package module
v0.0.0-...-1347a3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 9 Imported by: 16

README

go-toml-config

Build Status

go-toml-config is a simple TOML-based configuration package for Golang apps that allows you to easily load configuration files and set defaults. It's a simple wrapper around flag.FlagSet, so you can use it in pretty much the same exact way.

API documentation

Example

With my_app.conf:

country = "USA"

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

Use:

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.Float64("atlanta.temperature", 0)
)

func main() {
  config.Parse("/path/to/my_app.conf")
}

You can also create different ConfigSets to manage different logical groupings of config variables:

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

Contributors

Thanks all!

  • @tysonmote
  • @matrixik
  • @fwang2002
  • @shanks
  • @xboston
  • @tgulacsi

Documentation

Overview

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

Constants

Variables

This section is empty.

Functions

func Bool

func Bool(name string, value bool) *bool

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

func BoolVar

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

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

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

func Float64(name string, value float64) *float64

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

func Float64Var

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

func Int(name string, value int) *int

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

func Int64

func Int64(name string, value int64) *int64

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

func Int64Var

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

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

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

func String(name string, value string) *string

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

func StringVar

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

func Uint(name string, value uint) *uint

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

func Uint64

func Uint64(name string, value uint64) *uint64

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

func Uint64Var

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

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.

Types

type ConfigSet

type ConfigSet struct {
	*flag.FlagSet
}

func NewConfigSet

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL