Skip to content

joedursun/gogreen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoGreen

Go Report Card PkgGoDev Build Status

Keep your environment green! The green package provides a way to add runtime checks for your environment variables (AKA env vars). Its purpose is to provide guarantees around environment variable values and to help standardize configuration via env vars. If a struct field is marked as required and isn't found in either the environment or the .env file then gogreen.LoadEnv will panic.

Features:

  • require env vars at runtime
  • provide defaults for env vars
  • load env vars from .env file

Order of precedence:

  1. environment variables
  2. .env file variables

Motivation

Many apps load some portion of their configuration from environment variables and often have sprawling logic to check for their presence and provide defaults. By centralizing environment lookup and validation we get a single struct that guarantees all necessary variables are accounted for and can be tested against.

Example Usage

package main

import(
  "github.com/joedursun/gogreen"
)

// Use struct tags to inform green to track these fields. It's fine
// to use alongside JSON struct tags as well.
type MyEnv struct {
	Hostname   string `json:"-" green:"HOSTNAME,required"`
	Database   string `json:"database" green:"DATABASE,default=myuser"`
	AuthToken  string `green:"AUTH_TOKEN"`
}

// assuming HOSTNAME is present in the environment and nothing else
myenv := MyEnv{}
_ = gogreen.UnmarshalENV(&myenv)
myenv.Hostname // same as os.Getenv("HOSTNAME")
myenv.Database // "myuser"
myenv.AuthToken // ""

About

A better way to load environment variables

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published