vrischmann / envconfig Goto Github PK
View Code? Open in Web Editor NEWSmall library to read your configuration from environment variables
License: MIT License
Small library to read your configuration from environment variables
License: MIT License
Is there a possibility to set default empty string? Something like this:
type config struct {
Something string `envconfig:"default="`
}
Currently default
tag is unsupported on slices. As it explained here:
The
default
tag is unsupported on slices because slice parsing uses , as the separator, as does the envconfig tags separator.
What I propose is to change default
tag to support following format:
default=value1;value2;etc
If this value will be applied to slice, it will be split by ;
. If applied to string, value is applied as is. Else, it will panic. I would gladly implement this feature.
it would be interesting to make this bidirectional. For example, could I generate environment variables from a structure (and return as map[string]string
):
v := struct {
User string
Pass string
}{ "root", "pa55word" }
envs, err := envconfig.Marshal(&v)
envs, err := envconfig.MarshalPrefix("MYSQL", &v)
// where envs looks like this
map[string]string {
"MYSQL_USER": "root",
"MYSQL_PASS": "pa55word",
}
This test shows the issue:
func TestUnexportedField(t *testing.T) {
var conf struct {
name string
}
ok(t, envconfig.Init(&conf))
}
Result:
$ go test ./...
--- FAIL: TestTest (0.00s)
panic: reflect: reflect.Value.SetString using value obtained using unexported field [recovered]
panic: reflect: reflect.Value.SetString using value obtained using unexported field
goroutine 19 [running]:
testing.func·006()
/usr/local/Cellar/go/1.4.2/libexec/src/testing/testing.go:441 +0x181
reflect.flag.mustBeAssignable(0xf8)
/usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:219 +0x155
reflect.Value.SetString(0x109660, 0x2082793e0, 0xf8, 0x2082792e5, 0x3)
/usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:1476 +0x28
github.com/vrischmann/envconfig.parseValue(0x109660, 0x2082793e0, 0xf8, 0x2082792e5, 0x3, 0x0, 0x0)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig.go:181 +0x5e8
github.com/vrischmann/envconfig.setField(0x109660, 0x2082793e0, 0xf8, 0x16c030, 0x4, 0x0, 0x0, 0x0)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig.go:114 +0x134
github.com/vrischmann/envconfig.readStruct(0x10eb20, 0x2082793e0, 0xd9, 0x0, 0x0, 0x0, 0x0, 0x0)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig.go:93 +0x4c1
github.com/vrischmann/envconfig.InitWithPrefix(0xff9c0, 0x2082793e0, 0x0, 0x0, 0x0, 0x0)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig.go:39 +0x3a3
github.com/vrischmann/envconfig.Init(0xff9c0, 0x2082793e0, 0x0, 0x0)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig.go:21 +0x58
github.com/vrischmann/envconfig_test.TestTest(0x2082d0b40)
/Users/baptiste/Go/src/github.com/vrischmann/envconfig/envconfig_test.go:276 +0x58
testing.tRunner(0x2082d0b40, 0x227090)
/usr/local/Cellar/go/1.4.2/libexec/src/testing/testing.go:447 +0xbf
created by testing.RunTests
/usr/local/Cellar/go/1.4.2/libexec/src/testing/testing.go:555 +0xa8b
goroutine 1 [chan receive]:
testing.RunTests(0x1aaf88, 0x226f40, 0xf, 0xf, 0xa1ae35b5e0f21c01)
/usr/local/Cellar/go/1.4.2/libexec/src/testing/testing.go:556 +0xad6
testing.(*M).Run(0x2082a40a0, 0x226920)
/usr/local/Cellar/go/1.4.2/libexec/src/testing/testing.go:485 +0x6c
main.main()
github.com/vrischmann/envconfig/_test/_testmain.go:86 +0x1d5
FAIL github.com/vrischmann/envconfig 0.012s
Curious about the relationship to Kelsey Hightower's project, if any?
Hi
I've a scenario, where at first configuration struct
is loaded from config file and then envconfig is applied to overwrite some settings from environment variables, when they exist.
One of the fields in the config struct is a slice of string
. When an environment variable for this slice exists, the slice is not replaced by value from the environment variable. Instead, resulting slice contains as many items as it was in config file set to empty string followed by environment variable value.
For example:
["foo"]
"bar"
["", "bar"]
I guess the bug is in function setSliceField
on line
Line 252 in e689cc5
There shouldn't be value.Len()
but 0
in my opinion.
Best Regards
Pavel
Hi! Thanks for this useful tool. I have minor suggestion (happy to do PR as well). Currently this fails
package main
import (
"fmt"
"os"
"github.com/vrischmann/envconfig"
)
type OtherConfig struct {
Address string
}
type Config struct {
OtherConfig
}
func main() {
os.Setenv("CONFIG_ADDRESS", "example.com")
var conf struct {
Config
}
if err := envconfig.Init(&conf); err != nil {
fmt.Printf("err=%s\n", err)
}
}
with
err=envconfig: keys CONFIG_OTHERCONFIG_ADDRESS, CONFIG_OTHER_CONFIG_ADDRESS, config_other_config_address, config_otherconfig_address not found
I think it should be reasonable to include CONFIG_ADDRESS
in env var names searched. This is also inline with Go semantics where you can write embedded fields without specifying full path, e.g.
conf.Config.Address = "123"
conf.Config.OtherConfig.Address = "123"
both work.
WDYT?
Please consider to add a version tag on the top of the master. Currently I can refer latest version of package only by branch name.
Thanks!
If this project truly is "done", could you please tag a new release now that #20 has been merged? This is making for some awkward dependency management on our end. Thanks.
Right now parsing the environment variable process is failing fast. That's mean that only first uninitialized env is printed. If you have 5 environments variable then you can even need to execute the application 5 times to get info about all variables. I know that every application should have proper documentation but still, IMO it will be super useful to have an option to change that behavior and print all unset environment right after the first execution.
Assumption that app has the following config:
type Config struct {
Debug bool
ClientID string
}
and user didn't provide both envs.
Current solution
Output:
envconfig: keys DEBUGBB, DEBUG_BB, debug_bb, debugbb not found
Desired solution
Add new option param
envconfig.InitWithOptions(&conf, envconfig.Options{
PrintAllUnsetEnvs: true,
})
Output:
envconfig:
* key DEBUG not found
* keys CLIENTID, CLIENT_ID, client_id, clientid not found
I you will agree with me then I can also provide the implementation for that.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.