config

package module
v0.0.0-...-02f0489 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 7 Imported by: 0

README

GoDoc Codeship Codecov Go Report Card

config

Quick and easy way to load config files based on a simple set of rules.

Project inspired by https://github.com/lorenwest/node-config

Important stuff

Supported files

Before you can load any file you must register parsers using Loader.RegisterParser.

Each parser has a list of supported extensions that will be used to find files to load.

Config folder

By default the load will try to find the files based on the environment variable name given to it (defaults to CONFIG_DIR). If the variable name is empty or the variable value is empty, it will look for files in ./config.

File load order
default.{ext}
{deployment}.{ext}
{hostname}.{ext}
{hostname}-{deployment}.{ext}
local.{ext}
local-{deployment}.{ext}

Where

  • {ext} is one of the registered extensions.
  • {deployment} is the deployment name, from the $ENV environment variable. (No default value, ignored if empty)
  • {hostname} is the value returned from os.Hostname() with no changes. (No default value, ignored if empty)

Installation

go get -u github.com/txgruppi/config

Example

package main

import (
	"fmt"
	"log"

	"github.com/txgruppi/config"
	"github.com/txgruppi/config/parsers/json"
)

type Config struct {
	Server struct {
		Bind string `json:"bind"`
		Port int    `json:"port"`
	} `json:"server"`
}

func main() {
	loader := NewLoader()
	if err := loader.RegisterParser(json.NewParser()); err != nil {
		log.Fatal(err)
	}
	var config Config
	info, err := loader.Load(&config)
	if err != nil {
		log.Fatal(err)
	}
  fmt.Printf("Looked for files in: %s\n", info.ConfigFolder)
	fmt.Printf("Loaded files: %v\n", info.LoadedFiles)
	fmt.Printf("Loaded config: %v\n", config)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrFailedToLoad

type ErrFailedToLoad struct {
	Reason error
}

ErrFailedToLoad is returned at any point that load process fails.

func (*ErrFailedToLoad) Error

func (t *ErrFailedToLoad) Error() string

type ErrMissingExtDot

type ErrMissingExtDot struct {
	Ext string
}

ErrMissingExtDot is returned when the extension is missing the dot prefix.

func (*ErrMissingExtDot) Error

func (t *ErrMissingExtDot) Error() string

type ErrNilParser

type ErrNilParser struct {
}

ErrNilParser is returned when a register is called with parsers.Parser(nil).

func (*ErrNilParser) Error

func (*ErrNilParser) Error() string

type ErrParserConflict

type ErrParserConflict struct {
	Ext string
}

ErrParserConflict is returned when a parser is trying to register an extension that is already registered.

func (*ErrParserConflict) Error

func (t *ErrParserConflict) Error() string

type Info

type Info struct {
	ConfigFolder string
	LoadedFiles  []string
}

Info contains some basic information about what was loaded

type Loader

type Loader interface {
	// RegisterParser add a new parser to N extensions.
	// It will fail if: 1. the parser is `nil`; 2. any of its supported
	// extensions is already registered.
	RegisterParser(parsers.Parser) error
	// SupportedExtensions returns a list of registered extensions.
	SupportedExtensions() []string
	// Load find and loads files into the given data type.
	Load(v interface{}) (*Info, error)
	// SetEnvironmentVariableName changes the default `CONFIG_DIR` variable name.
	// Passing an empty name will disable it
	SetEnvironmentVariableName(name string)
}

Loader registers parsers and loads files into a given data type.

func NewLoader

func NewLoader() Loader

NewLoader returns a new Loader instance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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