Giter Club home page Giter Club logo

envconfig's People

Contributors

albenik avatar bfontaine avatar bradrydzewski avatar grigoriymikhalkin avatar mikekeilty avatar vrischmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

envconfig's Issues

Default empty string

Is there a possibility to set default empty string? Something like this:

type config struct {
   Something string `envconfig:"default="`
}

[Feature Request] default tag support for slices

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.

Marshal to map[string]string

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",
}

Envconfig panics if the conf struct contains an unexported field

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

Slices are cleared and appended, should be overwritten

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:

  • original slice value from config file - ["foo"]
  • environment variable value - "bar"
  • slice after envconfig - ["", "bar"]

I guess the bug is in function setSliceField on line

slice := reflect.MakeSlice(value.Type(), value.Len(), value.Cap())

There shouldn't be value.Len() but 0 in my opinion.

Best Regards

Pavel

Handling embedded structs - should probably allow shorter name?

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?

Need tag for the last update

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!

Tag new release from master

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.

[Feature request] Print all unset environment variable

Description

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.

Details

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.