Categorygithub.com/kkyr/fig
modulepackage
0.4.0
Repository: https://github.com/kkyr/fig.git
Documentation: pkg.go.dev

# 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:

# 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:

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:

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

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

Usage

See usage examples.

Documentation

For detailed documentation, visit go.dev.

Contributing

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

# Packages

No description provided by the author

# Functions

Dirs returns an option that configures the directories that fig searches to find the configuration file.
File returns an option that configures the filename that fig looks for to provide the config values.
IgnoreFile returns an option which disables any file lookup.
Load reads a configuration file and loads it into the given struct.
Tag returns an option that configures the tag key that fig uses when for the alt name struct tag key in fields.
TimeLayout returns an option that conmfigures the time layout that fig uses when parsing a time in a config file or in the default tag for time.Time fields.
UseEnv returns an option that configures fig to additionally load values from the environment.
UseStrict returns an option that configures fig to return an error if there exists additional fields in the config file that are not defined in the config struct.

# Constants

DefaultDir is the default directory that fig searches in for the config file.
DefaultFilename is the default filename of the config file that fig looks for.
DefaultTag is the default struct tag key that fig uses to find the field's alt name.
DefaultTimeLayout is the default time layout that fig uses to parse times.

# Variables

ErrFileNotFound is returned as a wrapped error by `Load` when the config file is not found in the given search dirs.

# Interfaces

StringUnmarshaler is an interface designed for custom string unmarshaling.

# Type aliases

Option configures how fig loads the configuration.