Categorygithub.com/jfarleyx/go-env-vars
modulepackage
1.0.0
Repository: https://github.com/jfarleyx/go-env-vars.git
Documentation: pkg.go.dev

# README

go-env-vars

GoDoc Go Report

Go module to fetch environment variables by desired Go data type. The module can return env vars as the following data types:

  • string
  • bool
  • v4 UUID
  • Rune
  • []Byte
  • time.Duration
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • complex64, complex128

The module can return env vars containing comma delimited lists of data as the following types:

  • []string
  • []v4 UUID
  • []time.Duration
  • []int, []int8, []int16, []int32, []int64
  • []uint, []uint8, []uint16, []uint32, []uint64
  • []float32, []float64
  • []complex64, []complex128

Last, but not least, env vars containing JSON can be returned as:

  • json.RawMessage
  • User defined struct

Requirements

Tested using Go version 1.15

Install

go get github.com/jfarleyx/go-env-vars

Usage

Fetching environment variables and converting them to the appropriate data type can be a tedious task when there are a lot of them. With go-env-vars, it can be much easier and faster to get your env vars as the data type you want.

import (
    "github.com/jfarleyx/go-env-vars"
)

...

// Set an environment variable
env.SetVar("TEST_BOOL", "true")

// Get env var as a boolean
v, err := env.GetBool("TEST_BOOL")
if err != nil {    
    fmt.Printf("error type: %s; error message: %s", err.Type, err.Err.Error())
    // evaluate the error type to determine root cause (e.g. missing env var, empty env var, invalid type)
    if err.Type == env.EmptyValue {
        ...
    }
}

////////////////////////////////////////////////////////////////////////////////
env.SetVar("TEST_UUID", "d1b0eaca-3b7c-4fef-88fe-75c9db4bf90a")

v, err := env.GetUUID("TEST_UUID")
if err != nil {
    ...
}

////////////////////////////////////////////////////////////////////////////////
env.SetVar("TEST_DURATION", "3h")

v, err := env.GetDuration("TEST_DURATION")
if err != nil {
    ...
}

////////////////////////////////////////////////////////////////////////////////
// Convert comma delimited values to slices of native Go types
env.SetVar("TEST_SLICE_INT", "1,2,3")

v, err := env.GetIntSlice("TEST_SLICE_INT")
if err != nil {
    ...
}

////////////////////////////////////////////////////////////////////////////////
// Convert JSON formatted env variable values
env.SetVar("TEST_JSON_RAW", "{\"key\":\"my value\"}")

// Get JSON data as json.RawMessage
v, err := env.GetJSONRaw("TEST_JSON_RAW")
if err != nil {
    ...
}

// Or unmarshal the data to a struct
type Test struct {
    Key string `json:"key"`
}

s := Test{}
err := env.Unmarshal("TEST_JSON_RAW", &s)
if err != nil {
    ...
}

Errors

The go-env-vars module methods return a custom error struct that defines the type of error as one of the following:

// ErrorType defines the type of error the env module returns.
type ErrorType string

const (
	// NotFound ErrorType indicates the requested env variable was not found.
	NotFound ErrorType = "NotFound"
	// EmptyValue ErrorType indicates the requested env variable's value was an empty string.
	EmptyValue ErrorType = "EmptyValue"
	// InvalidType ErrorType indicates the requested env variable's value was not of the requested data type.
	InvalidType ErrorType = "InvalidType"
)

// Error wraps a Go error and defines an ErrorType.
type Error struct {
	// Type is the ErrorType assigned to this Error.
	Type ErrorType
	// Msg is the message assigned to this Error.
	Err error
}

# Functions

GetBool returns the environment variable value as a boolean.
GetBytes returns the environment variable value as a byte slice.
GetComplex128 returns the environment variable value as a complex128 type.
GetComplex128Slice gets a comma delimited list of complex128 values and returns them as a slice of complex128.
GetComplex64 returns the environment variable value as a complex64 type.
GetComplex64Slice gets a comma delimited list of complex64 values and returns them as a slice of complex64.
GetDuration returns the environment variable value as a time.Duration type.
GetDurationSlice gets a comma delimited list of time.Duration values and returns them as a slice of time.Duration.
GetFloat32 returns the environment variable value as a float32.
GetFloat32Slice gets a comma delimited list of float32 values and returns them as a slice of float32.
GetFloat64 returns the environment variable value as a float64.
GetFloat64Slice gets a comma delimited list of float64 values and returns them as a slice of float64.
GetInt returns the environment variable value as an int.
GetInt16 returns the environment variable value as an int16.
GetInt16Slice gets a comma delimited list of int16 and returns them as a slice of int16.
GetInt32 returns the environment variable value as an int32.
GetInt32Slice gets a comma delimited list of int32 and returns them as a slice of int32.
GetInt64 returns the environment variable value as an int64.
GetInt64Slice gets a comma delimited list of int64 and returns them as a slice of int64.
GetInt8 returns the environment variable value as an int8.
GetInt8Slice gets a comma delimited list of int8 and returns them as a slice of int8.
GetIntSlice gets a comma delimited list of integers and returns them as a slice of int.
GetJSONRaw returns the environment variable's JSON formatted string as a json.RawMessage type.
GetRune returns the environment variable value as a Rune type.
GetString returns the environment variable value as a string.
GetStringSlice gets a comma delimited list of strings and returns them as a slice of string.
GetUint returns the environment variable value as an uint.
GetUint16 returns the environment variable value as an uint16.
GetUint16Slice gets a comma delimited list of uint16 and returns them as a slice of uint16.
GetUint32 returns the environment variable value as an uint32.
GetUint32Slice gets a comma delimited list of uint32 and returns them as a slice of uint32.
GetUint64 returns the environment variable value as an uint64.
GetUint64Slice gets a comma delimited list of uint64 and returns them as a slice of uint64.
GetUint8 returns the environment variable value as an uint8.
GetUint8Slice gets a comma delimited list of uint8 and returns them as a slice of uint8.
GetUintSlice gets a comma delimited list of uint and returns them as a slice of uint.
GetUUID returns the environment variable value as a UUID.
GetUUIDSlice gets a comma delimted list of UUID's and returns them as a slice of UUID.
NewError creates and returns a new env.Error{}.
SetVar sets an environment variable and its value.
Unmarshal unmarshals the JSON formatted env variable value to v.

# Constants

EmptyValue ErrorType indicates the requested env variable's value was an empty string.
InvalidType ErrorType indicates the requested env variable's value was not of the requested data type.
NotFound ErrorType indicates the requested env variable was not found.

# Structs

Error wraps a Go error and defines an ErrorType.

# Type aliases

ErrorType defines the type of error the env module returns.