Back to godoc.org
github.com/crgimenes/goconfig

package goconfig

v1.2.1
Latest Go to latest
Published: Dec 7, 2018 | License: MIT | Module: github.com/crgimenes/goconfig

Overview

Package goconfig uses a struct as input and populates the fields of this struct with parameters fom command line, environment variables and configuration file.

Index

Examples

Variables

var ErrFileFormatNotDefined = errors.New("file format not defined")

ErrFileFormatNotDefined Is the error that is returned when there is no defined configuration file format.

var File string

File name of default config file

var FileRequired bool

FileRequired config file required

var Formats []Fileformat

Formats is the list of registered formats.

var HelpString string

HelpString temporarily saves help

var Path string

Path sets default config path

var PrefixEnv string

PrefixEnv is a string that would be placed at the beginning of the generated Event tags.

var PrefixFlag string

PrefixFlag is a string that would be placed at the beginning of the generated Flag tags.

var Tag = "cfg"

Tag to set main name of field

var TagDefault = "cfgDefault"

TagDefault to set default value

var TagHelper = "cfgHelper"

TagHelper to set usage help line

var Usage func()

Usage is a function to show the help, can be replaced by your own version.

func DefaultUsage

func DefaultUsage()

DefaultUsage is assigned for Usage function by default

func Parse

func Parse(config interface{}) (err error)

Parse configuration

Example

Code:

package goconfig

import (
	"errors"
	"os"
	"testing"

	"github.com/crgimenes/goconfig/goflags"
	"github.com/crgimenes/goconfig/structtag"
)

type testStruct struct {
	A int    `cfg:"A" cfgDefault:"100"`
	B string `cfg:"B" cfgDefault:"200"`
	C string
	N string `cfg:"-"`
	p string
	S testSub `cfg:"S"`
}

type testSub struct {
	A int        `cfg:"A" cfgDefault:"300"`
	B string     `cfg:"C" cfgDefault:"400"`
	S testSubSub `cfg:"S"`
}
type testSubSub struct {
	A int    `cfg:"A" cfgDefault:"500"`
	B string `cfg:"S" cfgDefault:"" cfgRequired:"true"`
}

func TestFindFileFormat(t *testing.T) {
	_, err := findFileFormat(".json")
	if err != ErrFileFormatNotDefined {
		t.Fatal(err)
	}
	Formats = []Fileformat{{Extension: ".json"}}
	_, err = findFileFormat(".json")
	if err != nil {
		t.Fatal(err)
	}
}

// -=-=-=-=-=-=-=-=-=

func mLoad(config interface{}) (err error) {
	return
}

func mPrepareHelp(config interface{}) (help string, err error) {
	return
}

// -=-=-=-=-=-=-=-=-
func eLoad(config interface{}) (err error) {
	err = errors.New("test")
	return
}

func ePrepareHelp(config interface{}) (help string, err error) {
	err = errors.New("test")
	return
}

// -=-=-=-=-=-=-=-=-

func TestParse(t *testing.T) {

	s := &testStruct{A: 1, S: testSub{A: 1, B: "2"}}
	File = "config.txt"

	Formats = []Fileformat{{Extension: ".json", Load: mLoad, PrepareHelp: mPrepareHelp}}

	err := Parse(s)
	if err != ErrFileFormatNotDefined {
		t.Fatal("Error ErrFileFormatNotDefined expected")
	}

	File = "config.json"

	Formats = []Fileformat{{Extension: ".json", Load: eLoad, PrepareHelp: mPrepareHelp}}

	err = Parse(s)
	if err == nil {
		t.Fatal("Error expected")
	}

	Formats = []Fileformat{{Extension: ".json", Load: mLoad, PrepareHelp: ePrepareHelp}}

	err = Parse(s)
	if err == nil {
		t.Fatal("Error expected")
	}

	Formats = []Fileformat{{Extension: ".json", Load: mLoad, PrepareHelp: mPrepareHelp}}

	err = os.Setenv("A", "900")
	if err != nil {
		t.Fatal(err)
	}
	err = os.Setenv("B", "TEST")
	if err != nil {
		t.Fatal(err)
	}

	Tag = ""
	err = Parse(s)
	if err != structtag.ErrUndefinedTag {
		t.Fatal("Error structtag.ErrUndefinedTag expected")
	}

	err = os.Setenv("S_S_S", "TEST")
	if err != nil {
		t.Fatal(err)
	}

	Tag = "cfg"
	err = Parse(s)
	if err != nil {
		t.Fatal(err)
	}

	os.Setenv("A", "900ERROR")

	goflags.Reset()
	err = Parse(s)
	if err == nil {
		t.Fatal("Error expected")
	}

	err = os.Setenv("A", "")
	if err != nil {
		t.Fatal(err)
	}

	goflags.Reset()
	err = Parse(s)
	if err != nil {
		t.Fatal(err)
	}

	s1 := "test"
	goflags.Reset()
	err = Parse(s1)
	if err == nil {
		t.Fatal("Error expected")
	}

	goflags.Reset()
	err = Parse(&s1)
	if err == nil {
		t.Fatal("Error expected")
	}
}

func ExampleParse() {

	type config struct {
		Name  string `cfg:"Name" cfgDefault:"root"`
		Value int    `cfg:"Value" cfgDefault:"123"`
	}

	cfg := config{}

	err := Parse(&cfg)
	if err != nil {
		println(err)
	}

	println("Name:", cfg.Name, "Value:", cfg.Value)

}

func PrintDefaults

func PrintDefaults()

PrintDefaults print the default help

type Fileformat

type Fileformat struct {
	Extension   string
	Load        func(config interface{}) (err error)
	PrepareHelp func(config interface{}) (help string, err error)
}

Fileformat struct holds the functions to Load the file containing the settings

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier