Skip to content
Golang universal configuration
Branch: master
Clone or download
sherifabdlnaby and urso Make README.md example compile (#130)
Make README.md example compile
Latest commit c5859d3 Feb 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cfgutil Add license headers to go files Jun 29, 2018
diff Add license headers to go files Jun 29, 2018
flag Minor cleanup (#125) Jan 23, 2019
internal/parse Add license headers to go files Jun 29, 2018
json Minor cleanup (#125) Jan 23, 2019
testdata/error/message Add (*Config).Remove method (#126) Jan 28, 2019
vendor Remove UUID lib && use pseudo-random IDs instead (#118) Oct 4, 2018
yaml Minor cleanup (#125) Jan 23, 2019
.editorconfig Add development files (#83) Jan 3, 2017
.gitignore Add development files (#83) Jan 3, 2017
.travis.yml Update supported go versions (#128) Jan 23, 2019
CHANGELOG.md Prepare 0.7.0 (#129) Jan 28, 2019
LICENSE Initial commit Jan 11, 2016
README.md Make README.md example compile (#130) Feb 27, 2019
common_test.go Add license headers to go files Jun 29, 2018
doc.go Add license headers to go files Jun 29, 2018
error.go Add (*Config).Remove method (#126) Jan 28, 2019
error_test.go Add (*Config).Remove method (#126) Jan 28, 2019
errpred.go Add license headers to go files Jun 29, 2018
errpred_test.go Add license headers to go files Jun 29, 2018
fieldset.go Minor cleanup (#125) Jan 23, 2019
fieldset_test.go Minor cleanup (#125) Jan 23, 2019
getset.go Add license headers to go files Jun 29, 2018
getset_test.go Add license headers to go files Jun 29, 2018
merge.go Remove UUID lib && use pseudo-random IDs instead (#118) Oct 4, 2018
merge_test.go Fix escape character not removed from escaped string (#115) Jul 19, 2018
opts.go Add (*Config).Remove method (#126) Jan 28, 2019
path.go Add (*Config).Remove method (#126) Jan 28, 2019
reify.go Minor cleanup (#125) Jan 23, 2019
reify_test.go Array merging (#107) Jul 4, 2018
structs_test.go Add license headers to go files Jun 29, 2018
types.go
ucfg.go Add (*Config).Remove method (#126) Jan 28, 2019
ucfg_test.go Add (*Config).Remove method (#126) Jan 28, 2019
unpack.go Minor cleanup (#125) Jan 23, 2019
unpack_test.go Add license headers to go files Jun 29, 2018
util.go Array merging (#107) Jul 4, 2018
validator.go Add license headers to go files Jun 29, 2018
validator_test.go Add license headers to go files Jun 29, 2018
variables.go Do not treat $ as escape char in plain strings/regexes (#120) Oct 5, 2018
variables_test.go Do not treat $ as escape char in plain strings/regexes (#120) Oct 5, 2018

README.md

Build Status Go Report Card

ucfg - Universal Configuration

ucfg is a Golang library to handle yaml and json configuration files in your Golang project. It was developed for the libbeat framework and used by all beats.

API Documentation

The full API Documentation can be found here.

Examples

A few examples on how ucfg can be used. All examples below assume, that the following packages are imported:

import (
	"github.com/elastic/go-ucfg"
	"github.com/elastic/go-ucfg/yaml"
)

Dot notations

ufcg allows you to load yaml configuration files using dots instead of indentation. For example instead of having:

config:
  user: name

with ucfg you can write:

config.user: name

This makes configurations easier and simpler.

To load such a config file in Golang, use the following command:

config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))

Validation and Defaults

ucfg allows to automatically validate fields and set defaults for fields in case they are not defined.

// Defines struct to read config from
type ExampleConfig struct {
    Counter  int 	`config:"counter" validate:"min=0, max=9"`
}

// Defines default config option
var (
    defaultConfig = ExampleConfig{
		    Counter: 4,
    }
)

func main() {
    appConfig := defaultConfig // copy default config so it's not overwritten
    config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    err = config.Unpack(&appConfig)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

The above uses Counter as the config variable. ucfg assures that the value is between 0 and 9 and will return an error if this is not the case. In addition, if the value is not set, it will default to 4.

Requirements

ucfg has the following requirements:

  • Golang 1.10+
You can’t perform that action at this time.