Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kkyr/fig

A minimalist Go configuration library
https://github.com/kkyr/fig

config-file configuration configuration-management environment environment-variables go golang json toml yaml

Last synced: 3 months ago
JSON representation

A minimalist Go configuration library

Awesome Lists containing this project

README

        


fig


godoc
semver tag
go report card
coverage status
license

# fig

fig is a tiny library for loading an application's configuration into a Go struct.

## Why fig?

- 🛠️ Define your **configuration**, **validations** and **defaults** all within a single struct.
- 🌍 Easily load your configuration from a **file**, the **environment**, or both.
- ⏰ Decode strings into `Time`, `Duration`, `Regexp`, or any custom type that satisfies the `StringUnmarshaler` interface.
- 🗂️ Compatible with `yaml`, `json`, and `toml` file formats.
- 🧩 Only three external dependencies.

## Getting Started

`$ go get -d github.com/kkyr/fig`

Define your config file:

```yaml
# config.yaml

build: "2020-01-09T12:30:00Z"

server:
ports:
- 8080
cleanup: 1h

logger:
level: "warn"
trace: true
```

Define your struct along with _validations_ or _defaults_:

```go
package main

import (
"fmt"

"github.com/kkyr/fig"
)

type Config struct {
Build time.Time `fig:"build" validate:"required"`
Server struct {
Host string `fig:"host" default:"127.0.0.1"`
Ports []int `fig:"ports" default:"[80,443]"`
Cleanup time.Duration `fig:"cleanup" default:"30m"`
}
Logger struct {
Level string `fig:"level" default:"info"`
Pattern *regexp.Regexp `fig:"pattern" default:".*"`
Trace bool `fig:"trace"`
}
}

func main() {
var cfg Config
err := fig.Load(&cfg)
// error handling omitted

fmt.Printf("%+v\n", cfg)
// {Build:2019-12-25T00:00:00Z Server:{Host:127.0.0.1 Ports:[8080] Cleanup:1h0m0s} Logger:{Level:warn Pattern:.* Trace:true}}
}
```

Fields marked as _required_ are checked to ensure they're not empty, and _default_ values are applied to fill in those that are empty.

## Environment

By default, fig will only look for values in a config file. To also include values from the environment, use the `UseEnv` option:

```go
fig.Load(&cfg, fig.UseEnv("APP_PREFIX"))
```

In case of conflicts, values from the environment take precedence.

## Usage

See usage [examples](/examples).

## Documentation

For detailed documentation, visit [go.dev](https://pkg.go.dev/github.com/kkyr/fig?tab=doc).

## Contributing

PRs are welcome! Please explain your motivation for the change in your PR and ensure your change is properly tested and documented.