envconf

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

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 10 Imported by: 8

README

Build Status GoDoc codecov

Env Conf

Environment Variable config loader for go

The import path "gopkg.daemonl.com/envconf" is equivalent to "github.com/daemonl/envconf.go", but some tools aren't keen on the .go suffix. (And what if I wanted to write this for another language?)

Simple Usage


import "gopkg.daemonl.com/envconf"

var config struct {
	Bind string `env:"BIND" default:":8080"`
}

func main() {
	if err := envconf.Parse(&config); err != nil {
		log.Fatal(err.Error())
	}
}

Documentation

Overview

Package envconf parses environment variables into structs. It supports multiple types, however the core type is always a string.

Translators are available which manipulate the string value before setting, e.g. `!base64:SGVsbG8gV29ybGQ=` will cast to a string as "Hello World".

There is no specific handling for bytes when using this method, it is handled as a string entirely, if you are expecting actual bytes use a type like Hex or Base64 which will directly translate the env var string to bytes

Standard conversion from string to int, bool etc work, as well as custom types which satisfy `SetterFromEnv` (on a pointer, like JSON)

Combining translators and custom types is perfectly fine. The string translations will happen first, then the output will be passed into FromEnvString

Index

Constants

This section is empty.

Variables

View Source
var DefaultParser = Parser{
	Translators: map[string]Translator{
		"base64": TranslatorFunc(Base64Translator),
	},
}

DefaultParser is used by the Parse and Translate funcs for convenience

Functions

func Base64Translator

func Base64Translator(in string) (string, error)

Base64Translator decodes a URL Base64 encoded string. Not safe for byte data, use the Base64 type instead

func Parse

func Parse(dest interface{}) error

Parse uses DefaultParser.Parse

func SetFromString

func SetFromString(fieldInterface interface{}, stringVal string) error

SetFromString attempts to translate a string to the given interface. Must be a pointer. Standard Types string, bool, int, int(8-64) float(32, 64) and []string. Custom types must have method FromEnvString(string) error

func Translate

func Translate(in string) (string, error)

Translate uses the DefaultParser.Translate

Types

type Base64

type Base64 []byte

Base64 is a byte array which is loaded from a URL base64 string

func (*Base64) FromEnvString

func (b64 *Base64) FromEnvString(in string) error

FromEnvString Satisfies SetterFromEnv

type Hex

type Hex []byte

Hex is a byte array which is loaded from a Hex string

func (*Hex) FromEnvString

func (h *Hex) FromEnvString(in string) error

FromEnvString Satisfies SetterFromEnv

type Parser

type Parser struct {
	Translators map[string]Translator
}

Parser holds a list of Translator functions

func New

func New() *Parser

New returns a new Parser with an empty translator set

func (Parser) Parse

func (p Parser) Parse(dest interface{}) error

Parse reads the tags of dest to set any fields which should be parsed from the environment. The `env` tag gives the name of the variable. If the environment variable evaluates to an empty string, the value of `default` is used, or an error is thrown if the `default` tag is omitted. To allow optional parameters, set default to an empty string

func (*Parser) RegisterTranslatorFunc

func (p *Parser) RegisterTranslatorFunc(name string, translator func(string) (string, error))

RegisterTranslatorFunc adds a translator function to the list of translators. It replaces any existing function with the given name

func (Parser) Translate

func (p Parser) Translate(val string) (string, error)

Translate runs the parser's translators on the string

type SetterFromEnv

type SetterFromEnv interface {
	FromEnvString(string) error
}

SetterFromEnv is used by SetFromString for custom types

type Translator

type Translator interface {
	Translate(in string) (string, error)
}

Translator is responsible for taking a string and converting it to the output string, either by looking it up in an external service, decrypring, or anything else you can think of

type TranslatorFunc

type TranslatorFunc func(in string) (string, error)

TranslatorFunc is an adaptor to allow the use of ordinary functions as Translators

func (TranslatorFunc) Translate

func (tf TranslatorFunc) Translate(in string) (string, error)

Translate satisfies the Translator interface

Jump to

Keyboard shortcuts

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