Skip to content

Noah-Huppert/goconf

Repository files navigation

Goconf Go Doc

Simple go configuration library.

Table Of Contents

Overview

Goconf is a simple, straightforward, go configuration library.

Configuration is defined via structs and tags. Values are loaded from files.

Supports TOML by default. Any file format can be used.

Usage

Example

The following example loads configuration files named foo.toml from the /etc/foo/ directory and any .toml files from the /etc/foo.d/ directory. The configuration files are required to have a foo key. The bar key is optional and will have a default value of bardefault if not provided.

import "github.com/Noah-Huppert/goconf"

// Create goconf instance
loader := goconf.NewLoader()

// Define locations to search for configuration files
// Can use shell globs
loader.AddConfigPath("/etc/foo/foo.*")
loader.AddConfigPath("/etc/foo.d/*")

// Load values
type YourConfigStruct struct {
    Foo string `mapstructure:"foo" validate:"required"`
    Bar string `mapstructure:"bar" default:"bardefault"`
}

config := YourConfigStruct{}
err := loader.Load(&config)
panic(err)

Define Configuration

Define configuration parameters in a struct.

Use mapstructure tags to specify the names of fields when being decoded.

Use validate tags to specify value requirements for fields.

Use default tags to specify default field values.

Custom File Formats

The MapDecoder interface allows Goconf to use any file format.

Goconf provides an implementation for TOML files in the github.com/Noah-Huppert/goconf/toml package.

To use any other file format simply implement a MapDecoder and register it with Goconf via the Loader.RegisterFormat() method.

Tests

Run tests:

make test
# Or
make