Documentation ¶
Overview ¶
Package yacfg is yet another config package. Its meant to be modular and supplemental to commandline parsing which is already part of the standard library. The focus of yacfg is to populate configration data structures from environment variables or configuration files.
The examples package contains executables that show how to use it.
Index ¶
- Variables
- func DefaultEnv(cfg any) error
- func Env(cfg any, m EnvMapper) error
- func FileFormat(ext string, f FileReadFunc)
- func Files(cfg any, log func(string), names ...string) (files []string, err error)
- func FlagFiles(args []string, flag, dir string) (files []string, err error)
- func FlagFilesDoc(name string) string
- func GlobFlagDoc() string
- func MustFlagFiles(args []string, flag, dir string) []string
- type EnvMap
- type EnvMapField
- type EnvMapList
- type EnvMapTag
- type EnvMapUpTo
- type EnvMapUpper
- type EnvMapper
- type FileReadFunc
- type FromEnvThenFiles
- type ValueSetter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultEnvMapper = EnvMapList{ EnvMapTag(true), EnvMapUpper{EnvMapField{PathSep: "_"}}, }
DefaultEnvMapper first tries to determine the name of an environment variable from the field tag with the key 'env'—stopping when the value is "-". Next it takes the field's path and name joined with "_" as separator and converts the result to upper case.
Functions ¶
func DefaultEnv ¶
DefaultEnv calls Env with DefaultEnvMapper
Example ¶
type testCfgBase struct { Foo string `env:"HOME"` Bar int } type testCfg struct { testCfgBase Baz bool Quux testCfgBase Home string Bar int `env:"-"` } os.Setenv("HOME", "/usr/johndoe") os.Setenv("BAR", "4") os.Setenv("QUUX_BAR", "4711") os.Setenv("BAZ", "true") var cfg testCfg fmt.Println(DefaultEnv(&cfg)) fmt.Println(cfg)
Output: <nil> {{/usr/johndoe 4} true {/usr/johndoe 4711} /usr/johndoe 0}
func Env ¶
Env visits all fields of the cfg struct and uses the EvnMapper e to find an environment variable that provides a value for the field. If a value can be obtained from the environment is is set to the field in cfg.
Example ¶
type testCfgBase struct { Foo string `env:"HOME"` Bar int } type testCfg struct { testCfgBase Baz bool Quux testCfgBase Home string Bar int `env:"-"` } os.Setenv("HOME", "/usr/johndoe") os.Setenv("YACFG_HOME", "/usr/johndoe") os.Setenv("YACFG_BAR", "4") os.Setenv("QUUX_BAR", "4711") os.Setenv("FLAG", "true") var cfg testCfg fmt.Println(Env(&cfg, EnvMapList{ EnvMap{"Baz": "FLAG", "Quux/Bar": "QUUX_BAR"}, EnvMapTag(true), EnvMapUpper{EnvMapField{Prefix: "YACFG_"}}, })) fmt.Println(cfg)
Output: <nil> {{/usr/johndoe 4} true {/usr/johndoe 4711} /usr/johndoe 0}
func FileFormat ¶
func FileFormat(ext string, f FileReadFunc)
FileFormat registers a FileReadFunc for the file extension ext. The Go JSON reader from "encoding/json" is preconfigured for the ".json" extension.
func Files ¶
Files loads all config files in names into the configuration cfg and returns the names of all read files. Configration values are subsequently merged into cfg using daq Merge.
func FlagFiles ¶ added in v0.4.2
FlagFiles looks up all config files from flags named flag in args.
Example ¶
package main import ( "fmt" "path/filepath" ) const testFilesDir = "testdata" func main() { const lsSep = string(filepath.ListSeparator) c, err := FlagFiles([]string{"-cfg"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"--cfg"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"--c"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"--c"}, "c", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"-c"}, "c", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"-cfg", "cfg.json"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"-cfg=cfg.json"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"-cfg=no.json" + lsSep}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{"-cfg=no.json" + lsSep + "cfg.json"}, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) c, err = FlagFiles([]string{ "-cfg=no.json" + lsSep, // Finds nothing; no error because trailing ListSeparator "-cfg=no.json" + lsSep + "cfg.json" + lsSep + "cfg2.json", // Stop when finding cfg.json "-cfg=cfg2.json", // Adds cfg2.json }, "cfg", testFilesDir) fmt.Printf("'%s' (%v)\n", c, err) }
Output: '[]' (missing value for config flag 'cfg') '[]' (missing value for config flag 'cfg') '[]' (<nil>) '[]' (missing value for config flag 'c') '[]' (missing value for config flag 'c') '[testdata/cfg.json]' (<nil>) '[testdata/cfg.json]' (<nil>) '[]' (<nil>) '[testdata/cfg.json]' (<nil>) '[testdata/cfg.json testdata/cfg2.json]' (<nil>)
func FlagFilesDoc ¶ added in v0.4.2
func GlobFlagDoc ¶ added in v0.2.0
func GlobFlagDoc() string
func MustFlagFiles ¶ added in v0.4.2
Types ¶
type EnvMap ¶
EnvMap explicitly maps the complete path of a config field to the name of an environment variable. The complete path is the joined path with the field's name computed by path.Join().
type EnvMapField ¶
EnvMapField computes the name of the environment variable from the config field. The name will start with Prefix and ends with the field name.
If PathSep is not empty the elements of the field's path with appended separator will be put between the prefix and the field name.
func (EnvMapField) VarName ¶
func (em EnvMapField) VarName(f *reflect.StructField, path []string) (string, bool)
VarName implements EnvMapper
type EnvMapList ¶
type EnvMapList []EnvMapper
EnvMapList successively tries the listed EnvMappers until it finds a non-empty name for an environment variable. If one EnvMapper returns stop==true EnvMapList immediatley returns the result from that mapper.
func (EnvMapList) VarName ¶
func (m EnvMapList) VarName(f *reflect.StructField, path []string) (string, bool)
VarName implements EnvMapper
type EnvMapTag ¶
type EnvMapTag bool
EnvMapTag inspects the field's tag with the key 'env' for the name of the environment variable. EnvMapTag(true) return stop==true for the tag `env:"-"`. I.e. one can specify a struct field to be not intended for Env configuration. With EnvMapTag(false) that intend can be ignored.
type EnvMapUpTo ¶
func (EnvMapUpTo) VarName ¶
func (em EnvMapUpTo) VarName(f *reflect.StructField, path []string) (string, bool)
VarName implements EnvMapper
type EnvMapUpper ¶
type EnvMapUpper struct {
Map EnvMapper
}
EnvMapUpper wraps another EnvMapper and converts its result to upper case.
func (EnvMapUpper) VarName ¶
func (em EnvMapUpper) VarName(f *reflect.StructField, path []string) (string, bool)
VarName implements EnvMapper
type EnvMapper ¶
type EnvMapper interface { // VarName computes the name of the environment variable from the // current field of the config structure and the path, i.e. the // names of all parent structures, of the filed. If no environment // variable can be determined name is the empty string. // // When an EnvMapper is used in an EnvMapList the returned stop // value determines if further mappers are queried if name is // empty. VarName(f *reflect.StructField, path []string) (name string, stop bool) }
EnvMapper is an interface that defines how the name of the environment variable is determined depending on the configuration field that is to be set.
See also: Env
type FileReadFunc ¶
FileReadFunc reads config data from files of a specific format, e.g. JSON.
type FromEnvThenFiles ¶ added in v0.7.0
type FromEnvThenFiles struct { EnvPrefix string FilesFlagName string Flags *flag.FlagSet Log func(string) }
func (FromEnvThenFiles) Configure ¶ added in v0.7.0
func (cfgr FromEnvThenFiles) Configure(cfg any) (files []string, err error)
func (FromEnvThenFiles) ListFlagDoc ¶ added in v0.7.0
func (cfgr FromEnvThenFiles) ListFlagDoc() string
func (FromEnvThenFiles) MustConfigure ¶ added in v0.7.0
func (cfgr FromEnvThenFiles) MustConfigure(cfg any) (files []string)
type ValueSetter ¶
ValueSetter is an interface that allow to define how a value is parsed from string.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
yasec
Command yasec manages secrets for your config files.
|
Command yasec manages secrets for your config files. |
examples
|
|
Package yachg provides observable config values and tools to watch config files for changes while a program is running.
|
Package yachg provides observable config values and tools to watch config files for changes while a program is running. |
Package yaml registers a YAML config file reader for the extensions ".yaml" and ".yml".
|
Package yaml registers a YAML config file reader for the extensions ".yaml" and ".yml". |
Package yasec helps to keep secrets in config files as secret as possible.
|
Package yasec helps to keep secrets in config files as secret as possible. |