GithubHelp home page GithubHelp logo

mantyr / uniconfig Goto Github PK

View Code? Open in Web Editor NEW

This project forked from a-kr/uniconfig

0.0 2.0 0.0 14 KB

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

License: MIT License

Go 100.00%

uniconfig's Introduction

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

uniconfig's People

Contributors

a-kr avatar e-max avatar

Watchers

 avatar  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.