cloudfoundry/go-envstruct

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

Homepage:

Size: 3389

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

envstruct

GoDoc travis slack.cloudfoundry.org

envstruct is a simple library for populating values on structs from environment variables.

Usage

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}
Supported Types
Running Tests

Run tests using ginkgo.

 get github.com/onsi/ginkgo/ginkgo
 get github.com/onsi/gomega
nkgo

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.