GithubHelp home page GithubHelp logo

go-ucfg's Introduction

Build Status Go Report Card codecov

ucfg - Universal Configuration

ucfg is a Golang library to handle hjson, json, and yaml 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+

go-ucfg's People

Contributors

blakerouse avatar ph avatar kvch avatar ruflin avatar andrewkroh avatar cyrilleverrier avatar alrs avatar axw avatar jsoriano avatar jalvz avatar mtojek avatar miku avatar rbaradari avatar sherifabdlnaby avatar spacewander avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.