goconf

package module
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 8 Imported by: 2

README

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

Documentation

Overview

Package goconf is a simple, straightforward go configuration library.

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

Any file format can be used.

Example (Toml)

Simple configuration setup using Toml files

package main

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

func main() {
	// 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"`
	}

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader loads configuration

func NewDefaultLoader added in v0.1.1

func NewDefaultLoader() *Loader

NewDefaultLoader creates a Loader with all MapDecoders implemented by goconf.

func NewLoader

func NewLoader() *Loader

NewLoader creates a Loader with all MapDecoders implemented by goconf.

func (*Loader) AddConfigPath

func (l *Loader) AddConfigPath(p string)

AddConfigPath adds a potential path from which configuration files will be loaded. Must point to file(s) not of directories. The p argument can contain shell globs.

func (Loader) GetValidate added in v0.2.1

func (l Loader) GetValidate() *validator.Validate

GetValidate returns the validator instance used to validate configuration

func (Loader) Load

func (l Loader) Load(c interface{}) error

Load configuration files into a struct

func (*Loader) RegisterFormat

func (l *Loader) RegisterFormat(ext string, decoder MapDecoder)

RegisterFormat registers a MapDecoder to be used for a file extension. The ext argument should include the final dot and then the extension name. An empty string can be passed to target files without an extension.

type MapDecoder

type MapDecoder interface {
	// Decode a reader into a map
	Decode(r io.Reader, m *map[string]interface{}) error
}

MapDecoder decodes a Reader into a map

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL