envp

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2021 License: MIT Imports: 4 Imported by: 2

README

envp GoDoc

envp adds simple env var helpers with debug logging. Uses GoDotEnv internally.

Why

  • Reduce env var related boilerplate, thus making code more readable,
  • Basic validation and fallback
  • Log read values in human readible format
  • Use .env file convention

Why not?

There are multiple great pkgs that unmarshal env vars right away into structs, like this or this.

Usage

go get -u github.com/ppp225/envp
import (
  "github.com/ppp225/envp"
)

func main() {
  env := envp.GetEnvStringFrom("APP_ENV", "development", []string{"development", "demo", "production"}) // key, default, allowedValues
  envp.LoadEnvFromEnvFiles(env)

  ip := envp.GetEnvString("IP_ADDR", "localhost:12345") // key, default
  num := envp.GetEnvFloat("NUM", 100.3, 0.1, 200.5) // key, default, MIN, MAX
  nuInt := envp.GetEnvInt("NUM2", 100, 0, 200) // key, default, MIN, MAX
  booo := envp.GetEnvBool("B", false) // key, default
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnvBool

func GetEnvBool(key string, defaultVal bool) bool

GetEnvBool gets env var or fallback, validates range and logs return value. calls log.Fatal when env var does not validate correctly

func GetEnvFloat

func GetEnvFloat(key string, defaultVal float64, min float64, max float64) float64

GetEnvFloat gets env var or fallback, validates range and logs return value. calls log.Fatal when env var does not validate correctly

func GetEnvInt

func GetEnvInt(key string, defaultVal int, min int, max int) int

GetEnvInt gets env var or fallback, validates range and logs return value. calls log.Fatal when env var does not validate correctly

func GetEnvPassword

func GetEnvPassword(key string, defaultVal string) string

GetEnvPassword is GetEnvString, but doesn't log full value, just ****12

func GetEnvString

func GetEnvString(key string, defaultVal string) string

GetEnvString gets env var or fallback, and logs return value

func GetEnvStringFrom

func GetEnvStringFrom(key string, defaultVal string, allowedValues []string) string

GetEnvStringFrom gets env var or fallback, validates if value is allowed and logs return value

func LoadEnvFromEnvFiles

func LoadEnvFromEnvFiles(env string)

LoadEnvFromEnvFiles reads .env* files as in convention. See: https://github.com/joho/godotenv#precedence--conventions

func SetLogLevelFromEnv

func SetLogLevelFromEnv(key string)

SetLogLevelFromEnv reads loglevel from env and sets it. for envp, set to: info, warn, fatal, none. Defaults to info.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL