• Stars
    star
    539
  • Rank 82,376 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple, useful and opinionated config loader.

aconfig

build-img pkg-img version-img

Simple, useful and opinionated config loader.

Rationale

There are many solutions regarding configuration loading in Go. I was looking for a simple loader that is as easy to use and understand as possible. The goal was to load config from 4 places: defaults (in the code), files, environment variables, command-line flags. This library works with all of these sources.

Features

  • Simple API.
  • Clean and tested code.
  • Automatic fields mapping.
  • Supports different sources:
    • defaults in the code
    • files (JSON, YAML, TOML, DotENV, HCL)
    • environment variables
    • command-line flags
  • Dependency-free (file parsers are optional).
  • Ability to 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 `required:"true"`
		Pass string `required:"true"`
	}
	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,
	// SkipEnv:      true,
	// SkipFlags:    true,
	EnvPrefix:       "APP",
	FlagPrefix:      "app",
	Files:           []string{"/var/opt/myapp/config.json", "ouch.yaml"},
	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 MyConfig 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.

More Repositories

1

jwt

Safe, simple and fast JSON Web Tokens for Go
Go
664
star
2

base64

Faster base64 encoding for Go
Go
180
star
3

hedgedhttp

Hedged HTTP client which helps to reduce tail latency at scale.
Go
119
star
4

acmd

Simple, useful and opinionated CLI package in Go.
Go
115
star
5

builq

Easily build SQL queries in Go.
Go
89
star
6

atomix

Simple and easy wrappers for Go sync/atomic package.
Go
31
star
7

sqlmetrics

Prometheus metrics for Go database/sql via VictoriaMetrics/metrics
Go
24
star
8

synx

Better sync package for Go.
Go
22
star
9

oauth2

OAuth2 client in Go
Go
22
star
10

otp

One time password for Go.
Go
20
star
11

.github

Common stuff across cristalhq organization
Go
18
star
12

redis

WIP. Redis client for Go
Go
16
star
13

sse

Server-Sent Events (SSE) library for Go
Go
11
star
14

pgstats

Postgres statistics for Go
Go
11
star
15

bencode

Bencode encoding and decoding in Go.
Go
10
star
16

pgxutil

Go jackc/pgx helper to write proper transactions
Go
7
star
17

qrcode

QR code for Go.
Go
6
star
18

ipfilterware

Go HTTP middleware to filter clients by IP address
Go
6
star
19

dbump

Go database schema migrator library (See `cristalhq/dbumper` tool)
Go
6
star
20

hedgedgrpc

Hedged Go GRPC client which helps to reduce tail latency at scale.
Go
6
star
21

fastid

Fast ID generator based on timestamp, sequence number and worker id.
Go
5
star
22

httpx

Better net/http for Go
Go
4
star
23

glob

Glob pattern matching in Go.
Go
4
star
24

dbg

Debug helpers for Go
Go
4
star
25

dsvreader

Fast reader for delimiter-separated data
Go
4
star
26

jsn

Go package to easily construct JSON without defined types.
Go
4
star
27

cron

A cron library for Go.
Go
3
star
28

obfid

Obfuscating ID based on modular multiplicative inverse.
Go
3
star
29

dotenv

WIP
Go
3
star
30

errorx

Better `errors` package for Go
Go
3
star
31

benchmarks

Benchmarks for cristalhq and other solutions
Go
3
star
32

ternary

Ternary logic for Go.
Go
3
star
33

fresbi

WIP: Fast Reliable ElasticSearch Bulk Indexer
Makefile
3
star
34

netx

Better net package for Go
Go
3
star
35

bson

BSON for Go
Go
3
star
36

envx

Go environment utils
Go
2
star
37

dbumper

Database schema migrator.
Go
2
star
38

natsort

Natural sorting in Go, see Wikipedia.
Go
2
star
39

timex

Better `time` package for Go
Go
1
star
40

fasthedged

1
star
41

testt

Go
1
star
42

mathx

Go
1
star
43

appx

Go library for building applications. Dramatically simple.
Go
1
star
44

dynconst

Dynamic constants for Go application
Go
1
star
45

paseto

PASETO in Go
Go
1
star
46

lvlbp

Leveled byte pool
Go
1
star
47

feeds

Library for Atom, JSON and RSS feeds creation and parsing.
Go
1
star
48

typex

Type helpers for Go
Go
1
star
49

magneturi

A Go package for magnet links
Go
1
star
50

flagx

Go flag utils
Go
1
star