goenvloader

package module
v0.0.0-...-fec0e85 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 5 Imported by: 2

README

go-envloader

A simple go library for loading environment variables with default values.

Install
go get github.com/MartinSimango/go-envloader
Example

package main

import (
	"fmt"

	goenvloader "github.com/MartinSimango/go-envloader"
)

func main() {
	regexParser := goenvloader.NewCustomerEnvironmentRegexParser("&", ":", &goenvloader.EnclosedType{
		LeftEnclosure:  "<",
		RightEnclosure: ">",
	})
	envLoader := goenvloader.NewEnvironmentLoader(regexParser)

	val, err := envLoader.LoadIntFromEnv("&<ENV_VARIABLE:9000>")

	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println(val)
	}
}

The regex parser defines the format of how the environment variable and it's default will be. For the above example the format of environment variable is:
&{ENVIRONMENT_VARIABLE:default_value}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnclosedType

type EnclosedType struct {
	LeftEnclosure  string
	RightEnclosure string
}

type EnvironmentLoader

type EnvironmentLoader interface {
	LoadStringFromEnv(field string) (string, error)
	LoadIntFromEnv(field string) (int, error)
	LoadFloatFromEnv(field string) (float64, error)
	LoadBoolFromEnv(field string) (bool, error)
}

type EnvironmentLoaderImpl

type EnvironmentLoaderImpl struct {
	RegexParser EnvironmentRegexParser
}

func NewBraceEnvironmentLoader

func NewBraceEnvironmentLoader() *EnvironmentLoaderImpl

func NewDefaultEnvironmentLoader

func NewDefaultEnvironmentLoader() *EnvironmentLoaderImpl

func NewEnvironmentLoader

func NewEnvironmentLoader(regexParser EnvironmentRegexParser) *EnvironmentLoaderImpl

func (*EnvironmentLoaderImpl) LoadBoolFromEnv

func (ecfp *EnvironmentLoaderImpl) LoadBoolFromEnv(field string) (bool, error)

func (*EnvironmentLoaderImpl) LoadFloatFromEnv

func (ecfp *EnvironmentLoaderImpl) LoadFloatFromEnv(field string) (float64, error)

func (*EnvironmentLoaderImpl) LoadIntFromEnv

func (ecfp *EnvironmentLoaderImpl) LoadIntFromEnv(field string) (int, error)

func (*EnvironmentLoaderImpl) LoadStringFromEnv

func (ecfp *EnvironmentLoaderImpl) LoadStringFromEnv(field string) (string, error)

type EnvironmentRegexParser

type EnvironmentRegexParser interface {
	GetEnv(field string) (*EnvironmentVariable, error)
}

type EnvironmentRegexParserImpl

type EnvironmentRegexParserImpl struct {
	EnvironmentVariableRegExp string
	Seperator                 string
	Prefix                    string
	EncloseType               *EnclosedType
}

func NewCustomerEnvironmentRegexParser

func NewCustomerEnvironmentRegexParser(prefix, seperator string, enclosedType *EnclosedType) *EnvironmentRegexParserImpl

func NewDefaultEnvironmentRegexParser

func NewDefaultEnvironmentRegexParser() *EnvironmentRegexParserImpl

func (*EnvironmentRegexParserImpl) GetEnv

type EnvironmentVariable

type EnvironmentVariable struct {
	Name         string
	DefaultValue string
}

Jump to

Keyboard shortcuts

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