aconfig

package module
v0.10.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 10 Imported by: 73

README

aconfig

build-img pkg-img reportcard-img coverage-img

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.
  • Clean and tested code.
  • Automatic naming.
  • Supports different sources:
    • defaults in the code
    • files (JSON, YAML, TOML)
    • environment variables
    • command-line flags
  • Dependency-free (even file-parsers are optional).
  • Walk over configuration fields (automate what you want)

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"`
}

var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
	// feel free to skip some steps :)
	// SkipDefaults:    true,
	// SkipFiles:       true,
	// SkipEnvironment: true,
	// SkipFlags:       true,
	Files:           []string{"/var/opt/myapp/config.json", "ouch.yaml"},
	EnvPrefix:       "APP",
	FlagPrefix:      "app",
	FileDecoders: map[string]aconfig.FileDecoder{
		// from `aconfigyaml` submodule
		// see submodules in repo for more formats
		".yaml": aconfigyaml.New(),
	},
})

// IMPORTANT: define your own flags with `flagSet`
flagSet := loader.Flags()

if err := loader.Load(); 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 the prefix `APP_`
// 4. command-line flags with the prefix `app.` if they are 

Also see examples: examples_test.go.

Integration with spf13/cobra playground.

Documentation

See these docs.

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.9.0

type Config struct {
	SkipDefaults    bool
	SkipFiles       bool
	SkipEnvironment bool
	SkipFlags       bool

	EnvPrefix  string
	FlagPrefix string

	StopOnFileError bool
	Files           []string
	FileDecoders    map[string]FileDecoder
}

Config to configure configuration loader.

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 FileDecoder added in v0.8.0

type FileDecoder interface {
	DecodeFile(filename string) (map[string]interface{}, error)
}

FileDecoder is used to read config from files. See aconfig submodules.

type Loader

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

Loader of user configuration.

func LoaderFor added in v0.2.1

func LoaderFor(dst interface{}, cfg Config) *Loader

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

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() error

Load configuration into a given param.

func (*Loader) LoadWithFile added in v0.5.0

func (l *Loader) LoadWithFile(file string) error

LoadWithFile configuration into a given param.

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.

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 🇻🇳