MCPcopy Index your code
hub / github.com/cristalhq/aconfig

github.com/cristalhq/aconfig @v0.19.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.19.0 ↗ · + Follow
232 symbols 736 edges 16 files 41 documented · 18%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 89
Method 71
Struct 68
Interface 2
TypeAlias 2

Languages

Go100%

Modules by API surface

aconfig_test.go81 symbols
aconfig.go27 symbols
reflection.go22 symbols
utils.go16 symbols
parser.go14 symbols
aconfigyaml/yaml_test.go14 symbols
aconfigtoml/toml_test.go10 symbols
aconfighcl/hcl_test.go10 symbols
aconfigdotenv/dotenv_test.go9 symbols
example_test.go7 symbols
aconfigyaml/yaml.go5 symbols
aconfigtoml/toml.go5 symbols

For agents

$ claude mcp add aconfig \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page