goconfig: github.com/crgimenes/goconfig Index | Examples | Files | Directories

package goconfig

import "github.com/crgimenes/goconfig"

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

Package Files

config.go doc.go

Variables

var (

    // Tag to set main name of field
    Tag = "cfg"

    // TagDefault to set default value
    TagDefault = "cfgDefault"

    // TagHelper to set usage help line
    TagHelper = "cfgHelper"

    // Path sets default config path
    Path string

    // File name of default config file
    File string

    // FileRequired config file required
    FileRequired bool

    // HelpString temporarily saves help
    HelpString string

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

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

    // ErrFileFormatNotDefined Is the error that is returned when there is no defined configuration file format.
    ErrFileFormatNotDefined = errors.New("file format not defined")

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

    // Formats is the list of registered formats.
    Formats []Fileformat

    // FileEnv is the enviroment variable that define the config file
    FileEnv string

    // PathEnv is the enviroment variable that define the config file path
    PathEnv string

    // WatchConfigFile is the flag to update the config when the config file changes
    WatchConfigFile bool

    // DisableFlags on the command line
    DisableFlags bool
)

func DefaultUsage Uses

func DefaultUsage()

DefaultUsage is assigned for Usage function by default

func Parse Uses

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

Parse configuration

Code:

package main

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

    value := "test_file.json"
    err = os.Setenv(FileEnv, value)
    if err != nil {
        t.Fatal(err)
    }

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

    if value != File {
        t.Fatal("File name could not be loaded")
    }

    value = "/var"
    err = os.Setenv(PathEnv, value)
    if err != nil {
        t.Fatal(err)
    }

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

    if value != Path {
        t.Fatal("File path could not be loaded")
    }
}

func main() {

    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 ParseAndWatch Uses

func ParseAndWatch(config interface{}) (chChanges chan int64, chErr chan error, err error)

ParseAndWatch configuration returns a channel for errors while watching files and anorther when each update has been detected

func PrintDefaults Uses

func PrintDefaults()

PrintDefaults print the default help

type Fileformat Uses

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

Directories

PathSynopsis
goenv
goflags
hcl
helper
ini
json
structtag
toml
validate
yaml

Package goconfig imports 11 packages (graph) and is imported by 15 packages. Updated 2020-03-16. Refresh now. Tools for package owners.