genv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 3 Imported by: 1

README

Utility library to read and parse environment variables (Go)

This is a very simple library to read environment variables and parse them to multiple types.

Documentation

Installation

To install the library in your project, run:

go get https://github.com/AgustinSRG/genv

Usage

This library provides several functions that get environment variables and automatically parse them to different types. You can also set default values in case the variable is empty or missing.

Here is en example usage

package main

import (
    "fmt"
    // Import the module
    "github.com/AgustinSRG/genv"
)

func main() {
    // You can get string variables
    // If not set, you get the default value, passed as the second argument
    boolVar := genv.GetEnvBool("TEST_STR_VAR", "default value")
    fmt.Printf("TEST_STR_VAR = %v\n", boolVar)

    // You can parse variables into boolean
    // If they are set to TRUE or YES, you get true.
    // If they are set to FALSE or NO, you get false.
    // If they are not set to any of the above, you get the default value, passed as the second argument
    strVar := genv.GetEnvBool("TEST_BOOL_VAR", false)
    fmt.Printf("Parsed TEST_BOOL_VAR = %v\n", strVar)

    // You can parse variables into integer
    // If not set, or invalid, you get the default value, passed as the second argument
    port := genv.GetEnvInt("PORT", 80)
    fmt.Printf("PORT = %v\n", port)

    // For types like integers, where the value can be invalid,
    // you can use the alternative functions ending with "WithWarning"
    // They do the same, but also return a boolean flag, set to true
    // only if the value is set, but invalid
    port, warning := GetEnvIntWithWarning("PORT", 80)
    if warning {
        fmt.Println("[Warning] PORT has an invalid integer value, using the default value.")
    }
    fmt.Printf("PORT = %v\n", port)

    // Same for the rest of the types. Check the documentation for more
}

Build the library

To install dependencies, run:

go get .

To build the code, run:

go build .

Run linter

To run the code linter, run:

golangci-lint run

Run tests

In order to run the tests for this library, run:

go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnvBool

func GetEnvBool(variableName string, defaultValue bool) bool

Gets environment variable as boolean.

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

The value for the variable must be (case insensitive):

  • "TRUE" or "YES" to be true
  • "FALSE" or "NO" to be false

Returns the parsed value of the variable. If empty, or the value does not match the format, the default value will be returned.

func GetEnvBoolWithWarning

func GetEnvBoolWithWarning(variableName string, defaultValue bool) (val bool, warning bool)

Gets environment variable as boolean. Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

The value for the variable must be (case insensitive):

  • "TRUE" or "YES" to be true
  • "FALSE" or "NO" to be false

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvFloat32

func GetEnvFloat32(variableName string, defaultValue float32) float32

Gets environment variable as float (float32).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvFloat32WithWarning

func GetEnvFloat32WithWarning(variableName string, defaultValue float32) (val float32, warning bool)

Gets environment variable as float (float32). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvFloat64

func GetEnvFloat64(variableName string, defaultValue float64) float64

Gets environment variable as float (float64).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvFloat64WithWarning

func GetEnvFloat64WithWarning(variableName string, defaultValue float64) (val float64, warning bool)

Gets environment variable as float (float64). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvInt

func GetEnvInt(variableName string, defaultValue int) int

Gets environment variable as integer.

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvInt8

func GetEnvInt8(variableName string, defaultValue int8) int8

Gets environment variable as integer (int8).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvInt8WithWarning

func GetEnvInt8WithWarning(variableName string, defaultValue int8) (val int8, warning bool)

Gets environment variable as integer (int8). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvInt16

func GetEnvInt16(variableName string, defaultValue int16) int16

Gets environment variable as integer (int16).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvInt16WithWarning

func GetEnvInt16WithWarning(variableName string, defaultValue int16) (val int16, warning bool)

Gets environment variable as integer (int16). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvInt32

func GetEnvInt32(variableName string, defaultValue int32) int32

Gets environment variable as integer (int32).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvInt32WithWarning

func GetEnvInt32WithWarning(variableName string, defaultValue int32) (val int32, warning bool)

Gets environment variable as integer (int32). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvInt64

func GetEnvInt64(variableName string, defaultValue int64) int64

Gets environment variable as integer (int64).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvInt64WithWarning

func GetEnvInt64WithWarning(variableName string, defaultValue int64) (val int64, warning bool)

Gets environment variable as integer (int64). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvIntWithWarning

func GetEnvIntWithWarning(variableName string, defaultValue int) (val int, warning bool)

Gets environment variable as integer. Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvString

func GetEnvString(variableName string, defaultValue string) string

Gets environment variable as string.

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the value of the variable. If the variable is not set, the default value will be returned.

func GetEnvStringNonEmpty

func GetEnvStringNonEmpty(variableName string, defaultValue string) string

Gets environment variable as string. If the variable is set, but empty, the default value is returned.

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the value of the variable. If the variable is not set, or it is empty, the default value will be returned.

func GetEnvUint

func GetEnvUint(variableName string, defaultValue uint) uint

Gets environment variable as unsigned integer.

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvUint8

func GetEnvUint8(variableName string, defaultValue uint8) uint8

Gets environment variable as unsigned integer (uint8).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvUint8WithWarning

func GetEnvUint8WithWarning(variableName string, defaultValue uint8) (val uint8, warning bool)

Gets environment variable as unsigned integer (uint8). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvUint16

func GetEnvUint16(variableName string, defaultValue uint16) uint16

Gets environment variable as unsigned integer (uint16).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvUint16WithWarning

func GetEnvUint16WithWarning(variableName string, defaultValue uint16) (val uint16, warning bool)

Gets environment variable as unsigned integer (uint16). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvUint32

func GetEnvUint32(variableName string, defaultValue uint32) uint32

Gets environment variable as unsigned integer (uint32).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvUint32WithWarning

func GetEnvUint32WithWarning(variableName string, defaultValue uint32) (val uint32, warning bool)

Gets environment variable as unsigned integer (uint32). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvUint64

func GetEnvUint64(variableName string, defaultValue uint64) uint64

Gets environment variable as unsigned integer (uint64).

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns the parsed value of the variable. If empty, or the value cannot be parsed, the default value will be returned

func GetEnvUint64WithWarning

func GetEnvUint64WithWarning(variableName string, defaultValue uint64) (val uint64, warning bool)

Gets environment variable as unsigned integer (uint64). Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

func GetEnvUintWithWarning

func GetEnvUintWithWarning(variableName string, defaultValue uint) (val uint, warning bool)

Gets environment variable as integer. Also gets a warning flag if the value is invalid

Parameters

  • variableName: Name of the environment variable
  • defaultValue: Default value

Returns

  • val - The parsed value or the default value (if empty or cannot be parsed)
  • warning - True only if the value is set, but has invalid format

Types

This section is empty.

Jump to

Keyboard shortcuts

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