Name: go-envstruct
Owner: Cloud Foundry
Description: envstruct is a simple library for populating values on structs from environment variables.
Created: 2017-10-23 17:05:57.0
Updated: 2018-05-08 16:18:43.0
Pushed: 2018-05-08 16:18:42.0
Size: 3389
Language: Go
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
envstruct is a simple library for populating values on structs from environment variables.
Export some environment variables.
port HOST_IP="127.0.0.1"
port HOST_PORT="443"
port PASSWORD="abc123"
Note: The environment variables are case sensitive. The casing of the set environment variable must match the casing in the struct tag.
Write some code. In this example, Ip
requires that the HOST_IP
environment
variable is set to non empty value and Port
defaults to 80
if HOST_PORT
is
an empty value. Then we use the envstruct.WriteReport()
to print a table with
a report of what fields are on the struct, the type, the environment variable
where the value is read from, whether or not it is required, and the value.
All values are omitted by default, if you wish to display the value for a
field you can add report
to the env
struct tag.
age main
rt envstruct "code.cloudfoundry.org/go-envstruct"
Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
(c *Credentials) UnmarshalEnv(data string) error {
return json.Unmarshal([]byte(data), c)
HostInfo struct {
Credentials Credentials `env:"CREDENTIALS, required"`
IP string `env:"HOST_IP, required, report"`
Port int `env:"HOST_PORT, report"`
main() {
hi := HostInfo{Port: 80}
err := envstruct.Load(&hi)
if err != nil {
panic(err)
}
envstruct.WriteReport(&hi)
Run your code and rejoice!
run example/example.go
D NAME: TYPE: ENV: REQUIRED: VALUE:
entials main.Credentials CREDENTIALS true (OMITTED)
string HOST_IP true 10.0.0.1
int HOST_PORT false 80
entials: {Username:my-user Password:my-password}
true
and 1
results in true value, anything else results in false value)key:value
. Keys cannot contain colons and neither key nor value can
contain commas. e.g. key_one:value_one, key_two:value_two
Run tests using ginkgo.
get github.com/onsi/ginkgo/ginkgo
get github.com/onsi/gomega
nkgo