diff --git a/README.md b/README.md index 2c7af18..4e6d5fc 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,13 @@ loglevel = verbose outdir = "test" ``` +## Environment Variables + +Kleingebaeck can also be configured using environment variables. Just prefix the config variables with `KLEINGEBAECK_` and put them to upper case. Eg: +```shell +% KLEINGEBAECK_OUTDIR=/backup kleingebaeck -v +``` + ## Usage To setup the tool, you need to lookup your userid on diff --git a/config.go b/config.go index 81d3980..bdb0392 100644 --- a/config.go +++ b/config.go @@ -23,9 +23,11 @@ import ( "os" "path/filepath" "runtime" + "strings" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/providers/confmap" + "github.com/knadh/koanf/providers/env" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/providers/posflag" "github.com/knadh/koanf/v2" @@ -162,7 +164,15 @@ func InitConfig(w io.Writer) (*Config, error) { // else: we ignore the file if it doesn't exists } - // command line overrides config file + // env overrides config file + if err := k.Load(env.Provider("KLEINGEBAECK_", ".", func(s string) string { + return strings.Replace(strings.ToLower( + strings.TrimPrefix(s, "KLEINGEBAECK_")), "_", ".", -1) + }), nil); err != nil { + return nil, errors.New("error loading environment: " + err.Error()) + } + + // command line overrides env if err := k.Load(posflag.Provider(f, ".", k), nil); err != nil { return nil, errors.New("error loading flags: " + err.Error()) } diff --git a/go.mod b/go.mod index 9560640..efe6d19 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/andybalholm/cascadia v1.0.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect + github.com/knadh/koanf/providers/env v0.1.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect diff --git a/go.sum b/go.sum index 5d13c4b..c92ca20 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6OD github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18= github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPghxRVp7JbIvdvqzU= github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU= +github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg= +github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ= github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c= github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA= github.com/knadh/koanf/providers/posflag v0.1.0 h1:mKJlLrKPcAP7Ootf4pBZWJ6J+4wHYujwipe7Ie3qW6U= diff --git a/kleingebaeck.1 b/kleingebaeck.1 index c763139..2709a6b 100644 --- a/kleingebaeck.1 +++ b/kleingebaeck.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "KLEINGEBAECK 1" -.TH KLEINGEBAECK 1 "2024-01-16" "1" "User Commands" +.TH KLEINGEBAECK 1 "2024-01-17" "1" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -219,6 +219,22 @@ directory. Then just execute \f(CW\*(C`kleingebaeck\*(C'\fR. .PP You can use the \fB\-v\fR option to get verbose output or \fB\-d\fR to enable debugging. +.SH "ENVIRONMENT VARIABLES" +.IX Header "ENVIRONMENT VARIABLES" +The following environment variables are considered: +.PP +.Vb 7 +\& KLEINGEBAECK_USER +\& KLEINGEBAECK_DEBUG +\& KLEINGEBAECK_VERBOSE +\& KLEINGEBAECK_OUTDIR +\& KLEINGEBAECK_LIMIT +\& KLEINGEBAECK_CONFIG +\& KLEINGEBAECK_IGNOREERRORS +.Ve +.PP +Please note, that they take precedence over config file, but +commandline flags take precedence over env! .SH "BUGS" .IX Header "BUGS" In order to report a bug, unexpected behavior, feature requests diff --git a/kleingebaeck.go b/kleingebaeck.go index 0fc8b54..7488b98 100644 --- a/kleingebaeck.go +++ b/kleingebaeck.go @@ -74,6 +74,20 @@ SETUP You can use the -v option to get verbose output or -d to enable debugging. +ENVIRONMENT VARIABLES + The following environment variables are considered: + + KLEINGEBAECK_USER + KLEINGEBAECK_DEBUG + KLEINGEBAECK_VERBOSE + KLEINGEBAECK_OUTDIR + KLEINGEBAECK_LIMIT + KLEINGEBAECK_CONFIG + KLEINGEBAECK_IGNOREERRORS + + Please note, that they take precedence over config file, but commandline + flags take precedence over env! + BUGS In order to report a bug, unexpected behavior, feature requests or to submit a patch, please open an issue on github: diff --git a/kleingebaeck.pod b/kleingebaeck.pod index 2d964ae..cad4423 100644 --- a/kleingebaeck.pod +++ b/kleingebaeck.pod @@ -77,6 +77,23 @@ directory. Then just execute C. You can use the B<-v> option to get verbose output or B<-d> to enable debugging. +=head1 ENVIRONMENT VARIABLES + +The following environment variables are considered: + + KLEINGEBAECK_USER + KLEINGEBAECK_DEBUG + KLEINGEBAECK_VERBOSE + KLEINGEBAECK_OUTDIR + KLEINGEBAECK_LIMIT + KLEINGEBAECK_CONFIG + KLEINGEBAECK_IGNOREERRORS + +Please note, that they take precedence over config file, but +commandline flags take precedence over env! + + + =head1 BUGS In order to report a bug, unexpected behavior, feature requests