confusion

package module
v0.0.0-...-6c4f110 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: MIT Imports: 6 Imported by: 0

README

go-confusion

godoc

Simple configuration management for Go projects. Inspired by cristalhq/aconfig.

Install

$ go get github.com/njkleiner/go-confusion

Usage

package example

import (
	"fmt"

	"github.com/njkleiner/go-confusion"
	"github.com/njkleiner/go-confusion/toml"
)

type ExampleConfig struct {
	Foo, Bar string
}

func Example() {
	opts := confusion.Options{
		Prefix: "example",
		Paths: []string{
			"/etc",
			"$XDG_CONFIG_HOME",
			"$HOME/.config",
		},
		Loaders: map[string]confusion.Loader{
			".toml": toml.Loader,
		},
	}

	config := ExampleConfig{}

	// Loads the config file located at "$HOME/.config/example/config.toml"
	path, err := confusion.LoadConfig("config.toml", opts, &config)

	if err != nil {
		panic(err)
	}

	fmt.Printf("loaded from %s: %#v", path, config)
}

Contributing

You can contribute to this project by sending patches to noah@njkleiner.com. Pull Requests are also welcome.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Documentation

Overview

Package confusion provides a simple mechanism for loading configuration files of arbitrary formats from a number of alternate locations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConfigNotFound is returned when LoadConfig is unable to locate
	// a suitable configuration file in any of the provided locations.
	ErrConfigNotFound = errors.New("config not found")
	// ErrLoaderNotFound is returned by LoadConfig if a configuration
	// file is found but it cannot be loaded because no Loader matching
	// its file extension is available.
	ErrLoaderNotFound = errors.New("loader not found")
)

Functions

func LoadConfig

func LoadConfig(name string, opts Options, config interface{}) (string, error)

LoadConfig attempts to load a configuration file with a certain name according to the given Options and store the result in config.

Types

type Loader

type Loader interface {
	// Load attemtps to load a configuration file from r and store the
	// result in v.
	Load(r io.Reader, v interface{}) error
}

A Loader is responsible for loading configuration files of a specific format.

type Options

type Options struct {
	Prefix  string
	Paths   []string
	Loaders map[string]Loader
	// contains filtered or unexported fields
}

Options determines where LoadConfig will look for configuration files and what formats can be loaded.

Loaders maps file extensions (e.g., ".json") to a Loader that is capable of loading configuration files with this extension.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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