goconf

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2019 License: MIT Imports: 7 Imported by: 6

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.

Any file format can be used.

Usage

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.

Example

See the Go Doc Toml example.

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.NewDefaultLoader()

	// 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)
}

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 registered with the appropriate file extensions.

func NewLoader

func NewLoader() *Loader

NewLoader creates a Loader

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) 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