loadcfg

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 9 Imported by: 0

README

loadcfg

This package is used to load config files (optionally) and environment variables into structs. See godoc for more details.

Documentation

Overview

Package loadcfg loads a configuration using an optional file and basic serialization with the standard struct tags, but then also loads values from the environment.

Environment variables are defined with some prefix given to the function such (eg. PREFIX) and then they can be used to set almost any type of field. Below is an example of a complicated set of structs that show how you could set the fields for each type.

type A struct {
    // PREFIX_INT=5
    Int     int       `toml:"int"`
    // PREFIX_INTPTR=5
    IntPtr  *int      `toml:"intptr"`
    // PREFIX_STRINGS="one,two,three"
    Strings []string  `toml:"strings"`
    // PREFIX_TIME=RFC3339TimeString
    Time    time.Time `toml:"time"`

    // PREFIX_MAP_KEYNAME_FLOAT=4.5
    Map        map[string]B    `toml:"map"`
    // PREFIX_MAP_KEYNAME_FLOAT=4.5
    MapPtr     map[string]*B   `toml:"mapptr"`
    // PREFIX_MAPPRIM_KEYNAME=1
    MapPrim    map[string]int  `toml:"mapprim"`
    // PREFIX_MAPPRIMPTR_KEYNAME=2
    MapPrimPtr map[string]*int `toml:"mapprimptr"`

    // PREFIX_SLICE_0_FLOAT=4.5
    // PREFIX_SLICE_1_FLOAT=4.5
    Slice    []B  `toml:"slice"`
    // PREFIX_SLICEPTR_0_FLOAT=4.5
    SlicePtr []*B `toml:"sliceptr"`

    // PREFIX_STRUCT_FLOAT=4.5
    Struct    B  `toml:"struct"`
    // PREFIX_STRUCTPTR_FLOAT=4.5
    StructPtr *B `toml:"structptr"`
    // Ignored, cannot be set by env
    Ignored   *B `toml:"-"`
}

type B struct {
    Float float64 `toml:"float"`
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Env

func Env(envPrefix, structTag string, obj interface{}) error

Env deserializes environment variables into a struct. The envPrefix is not optional. The structTag is configurable.

func TOML

func TOML(envPrefix, filename string, obj interface{}) (m toml.MetaData, err error)

TOML loads filename using toml and deserializes it into obj, then the environment overrides are applied. There is no error if a config file is not found so you must check explicitly for this.

Types

This section is empty.

Jump to

Keyboard shortcuts

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