.travis.yml | ||
bool_test.go | ||
config.go | ||
errors_test.go | ||
example_test.go | ||
float_test.go | ||
general_test.go | ||
int_test.go | ||
LICENSE | ||
os-args_test.go | ||
precedence_test.go | ||
rconfig_suite_test.go | ||
README.md | ||
slice_test.go | ||
sub-struct_test.go | ||
uint_test.go | ||
vardefault_providers.go | ||
vardefault_test.go |
Description
Package rconfig implements a CLI configuration reader with struct-embedded defaults, environment variables and posix compatible flag parsing using the pflag library.
Installation
Install by running:
go get -u github.com/Luzifer/rconfig
OR fetch a specific version:
go get -u gopkg.in/luzifer/rconfig.v1
Run tests by running:
go test -v -race -cover github.com/Luzifer/rconfig
Usage
As a first step define a struct holding your configuration:
type config struct {
Username string `default:"unknown" flag:"user" description:"Your name"`
Details struct {
Age int `default:"25" flag:"age" env:"age" description:"Your age"`
}
}
Next create an instance of that struct and let rconfig
fill that config:
var cfg config
func init() {
cfg = config{}
rconfig.Parse(&cfg)
}
You're ready to access your configuration:
func main() {
fmt.Printf("Hello %s, happy birthday for your %dth birthday.",
cfg.Username,
cfg.Details.Age)
}
Provide variable defaults by using a file
Given you have a file ~/.myapp.yml
containing some secrets or usernames (for the example below username is assumed to be "luzifer") as a default configuration for your application you can use this source code to load the defaults from that file using the vardefault
tag in your configuration struct.
The order of the directives (lower number = higher precedence):
- Flags provided in command line
- Environment variables
- Variable defaults (
vardefault
tag in the struct) default
tag in the struct
type config struct {
Username string `vardefault:"username" flag:"username" description:"Your username"`
}
var cfg = config{}
func init() {
rconfig.SetVariableDefaults(VarDefaultsFromYAMLFile("~/.myapp.yml"))
rconfig.Parse(&cfg)
}
func main() {
fmt.Printf("Username = %s", cfg.Username)
// Output: Username = luzifer
}
More info
You can see the full reference documentation of the rconfig package at godoc.org, or through go's standard documentation system by running godoc -http=:6060
and browsing to http://localhost:6060/pkg/github.com/Luzifer/rconfig after installation.