aconfig

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 11 Imported by: 73

README

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: pkgo.dev.

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field added in v0.4.0

type Field interface {
	// Name of the field.
	Name() string

	// DefaultValue of the field.
	DefaultValue() string

	// Usage of the field (set in `usage` tag).
	Usage() string

	// Tag returns a given tag for a field.
	Tag(tag string) string

	// Parent of the current node.
	Parent() (Field, bool)
}

Field of the user configuration structure. Done as an interface to export less things in lib.

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader of user configuration.

func LoaderFor added in v0.2.1

func LoaderFor(src interface{}) *Loader

LoaderFor creates a new Loader based on a given configuration structure.

func (*Loader) Build added in v0.2.1

func (l *Loader) Build() *Loader

Build to initialize flags for a given configuration.

func (*Loader) Flags added in v0.1.2

func (l *Loader) Flags() *flag.FlagSet

Flags returngs flag.FlagSet to create your own flags.

func (*Loader) Load

func (l *Loader) Load(into interface{}) error

Load configuration into a given param.

func (*Loader) LoadWithFile added in v0.5.0

func (l *Loader) LoadWithFile(into interface{}, file string) error

LoadWithFile configuration into a given param.

func (*Loader) SkipDefaults added in v0.2.1

func (l *Loader) SkipDefaults() *Loader

SkipDefaults if you don't want to use them.

func (*Loader) SkipEnvironment added in v0.2.1

func (l *Loader) SkipEnvironment() *Loader

SkipEnvironment if you don't want to use it.

func (*Loader) SkipFiles added in v0.2.1

func (l *Loader) SkipFiles() *Loader

SkipFiles if you don't want to use them.

func (*Loader) SkipFlags added in v0.2.1

func (l *Loader) SkipFlags() *Loader

SkipFlags if you don't want to use them.

func (*Loader) StopOnFileError added in v0.5.0

func (l *Loader) StopOnFileError() *Loader

StopOnFileError to stop configuration loading on file error.

func (*Loader) WalkFields added in v0.4.0

func (l *Loader) WalkFields(fn func(f Field) bool)

WalkFields iterates over configuration fields. Easy way to create documentation or other stuff.

func (*Loader) WithEnvPrefix added in v0.2.1

func (l *Loader) WithEnvPrefix(prefix string) *Loader

WithEnvPrefix to specify environment prefix.

func (*Loader) WithFiles added in v0.2.1

func (l *Loader) WithFiles(files []string) *Loader

WithFiles for a configuration.

func (*Loader) WithFlagPrefix added in v0.2.1

func (l *Loader) WithFlagPrefix(prefix string) *Loader

WithFlagPrefix to specify command-line flags prefix.

Directories

Path Synopsis
aconfighcl module
aconfigtoml module
aconfigyaml module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳