uniconfig

package module
v0.0.0-...-b52ce83 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2016 License: MIT Imports: 11 Imported by: 4

README

uniconfig

Build Status

Simple golang configuration library which works with INI files, cmdline flags and environment variables.

This package is influenced by https://github.com/rakyll/globalconf, but is different: instead of defining flags for every option, you create a struct with all required fields, and uniconfig creates all flags for you.

Installation

go get github.com/a-kr/uniconfig

Usage

First, import the package:

import "github.com/a-kr/uniconfig"

Define the structure which will hold your configuration. uniconfig supports two-level structure nesting.

type MyConfig struct {
	Debug   bool
	Count   int `help:"number of items"`
	Nested1 struct {
		A       string
		B       string
		ignored string
	}
	Nested2 struct {
		Zzz bool
	}
}
...
config := MyConfig{}
// set some defaults here...
config.Count = 42

Pass the config structure to the library:

uniconfig.Load(&config)

This line will perform the following actions:

  1. Check the command line for --config option and fill the config structure from INI file, if specified. The INI file can look like this:

     debug = true
     count = 65535
     ; this is a comment
     # also a comment
    
     [Nested1]
     A  = sometag
     b = something else
     irrelevant = ignored option
     number = 14
    
  2. Overwrite config from environment variables. Environment variables are named in uppercase, like this: COUNT=65535 NESTED1_A=sometag.

  3. Generate command line flags for each of config's public fields, parse the command line and overwrite config with parsed values. Command line options are named in lowercase, like this: --count 65535 --nested1-a sometag. The --help option will be handled here, printing something like this:

     -config="": path to configuration file
     -count=42: number of items
     -debug=false:
     -nested1-a="":
     -nested1-b="":
     -nested2-zzz=false:
    

    Note that the help text comes from the structure field tags (e.g. Count int `help:"number of items"` ).

You can dump the final config in the form of INI file:

fmt.Println(uniconfig.ConfigAsIniFile(config))

Example code

Clone this repository and try running example application in various ways:

git clone https://github.com/a-kr/uniconfig
cd uniconfig

NESTED1_A=bee go run example_app/example.go --count=45 --config=example_app/testconfig.ini
NESTED1_A=bee go run example_app/example.go --count=45
go run example_app/example.go --count=45

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnvPrefix = ""

Functions

func ConfigAsIniFile

func ConfigAsIniFile(config interface{}) string

func GetConfigPathFromCmd

func GetConfigPathFromCmd(args []string) string

func InitFlags

func InitFlags(configItems []*ConfigItem)

func ItemsAsIniFile

func ItemsAsIniFile(configItems []*ConfigItem) string

func Load

func Load(config interface{})

func LoadFromConfigFile

func LoadFromConfigFile(configItems []*ConfigItem)

func LoadFromEnv

func LoadFromEnv(configItems []*ConfigItem)

func LoadFromFlags

func LoadFromFlags(configItems []*ConfigItem)

func NewFloatSlice

func NewFloatSlice(data *[]float64) *floatslice

func NewIntSlice

func NewIntSlice(data *[]int) *intslice

func NewStrSlice

func NewStrSlice(data *[]string) *strslice

func ParseIniFile

func ParseIniFile(inifile io.Reader) map[string]string

func SetFromParsedIniFile

func SetFromParsedIniFile(configItems []*ConfigItem, ini map[string]string)

Types

type ConfigItem

type ConfigItem struct {
	Section string
	Name    string
	Value   reflect.Value
	Help    string
}

func ScanConfig

func ScanConfig(config interface{}) []*ConfigItem

func (*ConfigItem) CmdFlagName

func (i *ConfigItem) CmdFlagName() string

func (*ConfigItem) EnvVarName

func (i *ConfigItem) EnvVarName() string

func (*ConfigItem) InitFlag

func (i *ConfigItem) InitFlag()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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