/go-config

An extensible config auto-loading library. Walk that path!

Primary LanguageGo

Config Manager

This package provides a simple way to manage configuration for a Go application. It allows you to read configuration from multiple sources and merge them into a single struct.

Installation

go get github.com/mateothegreat/go-config

Example

type CustomConfig struct {
  Base struct {
    A int    `yaml:"a" json:"a" env:"A" required:"true"`
    B string `yaml:"b" json:"b" env:"B" required:"true"`
    C string `yaml:"c" json:"c" env:"C" required:"false"`
 } `yaml:"base" json:"base" env-prefix:"BASE_" required:"true"`
  Foo struct {
    Bar string `yaml:"bar" json:"bar" env:"BAR" required:"true"`
  } `yaml:"foo" json:"foo" required:"true"`
}

config, err := GetConfig[ExtraConfig](GetConfigArgs{
  Paths: []string{
    ".env.json",
    ".env.base.yaml",
    ".env.local.yaml",
  },
  WalkDepth: 7,
})

println(config.Foo.Bar)

Usage

Method Description
GetConfig Get the configuration from the given paths and merge them into a single struct.
SaveConfig Save the configuration to the given path.

GetConfig

GetConfig[T any](args GetConfigArgs) (T, error)

Returns a new instance of the configuration struct with the configuration merged from the given paths.

Argument Type Description
T interface{} The type of the configuration to get. Must be a struct.
args GetConfigArgs An arguments struct to get the configuration with.

SaveConfig

SaveConfig(config interface{}, path string, walkDepth int) error

Saves the configuration to the given path.

Argument Type Description
config interface{} The configuration to save.
path string The path to save the configuration to.
walkDepth int The depth to walk the path to find the configuration file.