Code Monkey home page Code Monkey logo

aconfig's Introduction

aconfig

Build Status GoDoc Go Report Card Coverage

Simple, useful and opinionated config loader.

Rationale

There are more than 2000 repositories on Github regarding configuration in Go. I was looking for a simple configuration loader that will automate a lot of things for me. Idea was to load config from 4 common places: defaults (in the code), config files, environment variables, command-line flags. This library works with all of them.

Features

  • Simple API.
  • Automates a lot of things.
  • Opinionated.
  • Supports different sources:
    • defaults in code
    • files (JSON, YAML, TOML)
    • environment variables
    • command-line flags
  • Dependency-free (except file parsers).
  • Walk over configuration fields.

Install

Go version 1.14+

go get github.com/cristalhq/aconfig

Example

type MyConfig struct {
	Port int `default:"1111" usage:"just give a number"`
	Auth struct {
		User string `default:"def-user"`
		Pass string `default:"def-pass"`
	}
	Pass string `default:"" env:"SECRET" flag:"sec_ret"`
}

loader := aconfig.LoaderFor(&MyConfig{}).
	// feel free to skip some steps :)
	// SkipDefaults().
	// SkipFiles().
	// SkipEnvironment().
	// SkipFlags().
	WithFiles([]string{"file.json", "ouch.yaml"}).
	WithEnvPrefix("APP").
	WithFlagPrefix("app").
	Build()

flagSet := loader.Flags() // now use exactly this flags to define your own

var cfg MyConfig
if err := loader.Load(&cfg); err != nil {
	panic(err)
}

// configuration fields will be loaded from (in order):
//
// 1. defaults set in structure tags (see structure defenition)
// 2. loaded from files `file.json` if not `ouch.yaml` will be used
// 3. from corresponding environment variables with prefix `APP`
// 4. and command-line flags if they are

Also see examples: examples_test.go or integration with spf13/Cobra using AddGoFlagSet playground

Documentation

See here: pkg.go.dev.

License

MIT License.

aconfig's People

Contributors

cristaloleg avatar fzambia avatar jakubdyszkiewicz 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.