ezconfig

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2021 License: BSD-3-Clause Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-easy-config

A strongly opinionated configurator.

Currently wraps around the awesome viper and pflag libraries, and uses gookit/event to notify config clients.

Documentation

Overview

ezconfig is an opinionated wrapper around Viper and pflag. It allows the autoregistration of config files, flags and environement variables simultaneously. It supports registering a struct so that its values can also be set via flags or env values.

Example
// Tell it where to look
SetFile("config", "./testdata")
SetEnvPrefix("TESTER")

// mock environment variables
os.Setenv("TESTER_SUPER_DEEP_FOO", "bar")

// register with the system
var super testdata.FullExample
Bind("super", &super)

// once everything set, just run:
Initialize()

fmt.Println(super.Simple)
fmt.Println(viper.GetString("super.deep.foo"))
fmt.Println(viper.GetString("shallow.foo"))
Output:
untouched
bar
just barely here

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind added in v0.2.0

func Bind(key string, obj interface{})

Bind parses a struct definition and autobinds pflags based on the struct 'json' tag. An optional prefix is added to all found names in order to support nesting structs. It also supports an optional 'usage' tag.

Example
viper.Reset()

Bind("simple", testdata.SmallExample{})

fmt.Println(pflag.Lookup("simple-none").Usage)
fmt.Println(viper.GetBool("simple.none"))
Output:
A boolean flag
false

func Initialize added in v0.2.0

func Initialize() error

func OnUpdate added in v0.3.0

func OnUpdate(callback Callback)

func SetEnvPrefix

func SetEnvPrefix(prefix string)

SetEnvPrefix tells Viper to check environment variables. It automatically adds the prefix and sets '_' as the seperator.

func SetFile

func SetFile(name string, paths ...string) error

SetFile tells Viper the file name and paths to search, and autoloads the config. Returns an error if no config file is found.

Example
viper.Reset()

SetFile("config", "./", "$HOME/.myproj", "/etc/myproj", "./testdata")

file := viper.ConfigFileUsed()

here, _ := filepath.Abs("./")
found, _ := filepath.Rel(here, file)
fmt.Println(found)
Output:
testdata/config.yaml

func ShowConfig added in v0.4.0

func ShowConfig() bool

ShowConfig will output all config values as yaml if the --print-config flag is set returns the value of the print-config flag

func ShowHelp added in v0.4.0

func ShowHelp() bool

ShowHelp will output all config options as flags if the --help/-h flag is passed returns the value of the help flag

Types

type Callback added in v0.3.0

type Callback func() error

Jump to

Keyboard shortcuts

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