gaia

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MIT Imports: 5 Imported by: 1

README

Build Status

gaia

Simplified environment management for golang apps.

The gaia library is intended as a way to minimize writing boilerplate code when reading and parsing environment variables in an application. It provides methods that can either apply a sane default or abort the applicatino (fail-fast) when an environment variable is missing or malformed.

functions

  • GetEnvOrDie(envStr string) string
  • GetEnvWithDefault(envStr, defaultStr string) string
  • GetDirectoryNameOrDie(envStr string) string ... (will exit if adirectory ${envStr} is not exist)
  • GetInteger(envStr string) (int, error)
  • GetIntegerWithDefault(envStr string, defaultInt int) int
  • GetIntegerInRange(envStr string, minInt, maxInt int) (int, error) ... (the valid range is inclusive of both maxInt and minInt)
  • GetPort(envStr string) (int, error) ... (an error will be raised if the port number is outside the range 1 to 65535)
  • GetPortWithDefault(envStr string, defaultPort int) int
  • GetPortOrDie(envStr string) int

In all cases, the function parameter envStr is the name of the environmnt variable.

Example

from the command line (assumes you are using bash)
export YourEnvVar=25

then create a main.go with the following content

package main

import (
  "fmt"
  "github.com/bpmason1/gaia"
)

func main() {
  fmt.Println("Hello")

  minInt := 20
  maxInt := 30
  i, err := GetIntegerInRange("YourEnvVar", minInt, maxInt)

  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println("Success ... %d is in the range %d to %d", i, minInt, maxInt)
  }
}

This simple example shows gaia's ability to verify not only that the integer in the environment variable is within bounds but to also perform the initial type casting to ensure the input string can be turned into in integer at all.

Documentation

Index

Constants

View Source
const INVALID_PORT = -1
View Source
const MAX_PORT = 65535
View Source
const MIN_PORT = 1

Variables

This section is empty.

Functions

func GetDirectoryNameOrDie

func GetDirectoryNameOrDie(envStr string) string

func GetEnvOrDie

func GetEnvOrDie(envStr string) string

func GetEnvWithDefault

func GetEnvWithDefault(envStr, defaultStr string) string

func GetInteger

func GetInteger(envStr string) (int, error)

func GetIntegerInRange

func GetIntegerInRange(envStr string, minInt, maxInt int) (int, error)

func GetIntegerWithDefault

func GetIntegerWithDefault(envStr string, defaultInt int) int

func GetPort

func GetPort(envStr string) (int, error)

func GetPortOrDie

func GetPortOrDie(envStr string) int

func GetPortWithDefault

func GetPortWithDefault(envStr string, defaultPort int) int

Types

This section is empty.

Jump to

Keyboard shortcuts

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