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: 8 months ago 
        JSON representation
    
A minimalist Go configuration library
- Host: GitHub
 - URL: https://github.com/kkyr/fig
 - Owner: kkyr
 - License: apache-2.0
 - Created: 2020-01-16T18:43:19.000Z (almost 6 years ago)
 - Default Branch: master
 - Last Pushed: 2024-03-04T23:00:30.000Z (over 1 year ago)
 - Last Synced: 2024-07-31T20:44:17.065Z (over 1 year ago)
 - Topics: config-file, configuration, configuration-management, environment, environment-variables, go, golang, json, toml, yaml
 - Language: Go
 - Homepage:
 - Size: 246 KB
 - Stars: 367
 - Watchers: 4
 - Forks: 31
 - Open Issues: 3
 - 
            Metadata Files:
            
- Readme: README.md
 - License: LICENSE
 
 
Awesome Lists containing this project
- awesome-go - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - fucking-awesome-go - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - awesome-go - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - go-awesome - fig
 - awesome-go - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - awesome-go-extra - fig - 01-16T18:43:19Z|2022-01-03T22:02:55Z| (Configuration / Advanced Console UIs)
 - awesome-go-with-stars - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - awesome-go-cn - fig
 - awesome-go-plus - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults).  (Configuration / Standard CLI)
 - awesome-go-plus - fig - Tiny library for reading configuration from a file and from environment variables (with validation & defaults). (Configuration / Standard CLI)
 - awesome-go - kkyr/fig
 - awesome-go - kkyr/fig
 
README
          
    
# 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.