envir

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 8 Imported by: 0

README

envir is a tiny library inspired by Viper. Focused on declarative OOP composition. You can setup desired priority and extend processing logic by adding your own variable expander or even different sources.

Go Report Card Releases LICENSE

Example:

import (
    "github.com/rogozhka/envir"
    "github.com/rogozhka/envir/decoders"
)

const envPrefix = "APPNAME"

func main() {
	env := envir.New(
		envir.WithPrefix(envPrefix),
		envir.WithEnvSubst(), // use "github.com/a8m/envsubst" as a custom vars processing 
		envir.WithLookup(
			envir.NewLookupComposition(
				envir.NewLookupFile(
					"app.yaml", // first priority, unprefixed vars inside env: section
					envir.WithCutPrefix(envPrefix),
					envir.WithDecoder(
						decoders.NewYaml(
							decoders.WithEntriesPath("env"),
						),
					),
				),
				envir.WithOptional( // second priority, prefixed vars in the document root
					envir.NewLookupFile(
						"dev.yaml",
						envir.WithDecoder(decoders.NewYaml()),
					),
				),
				envir.WithOptional( // third priority, shell env w/ prefix
					envir.NewLookupOs(),
				),
			),
		),
	)
	const (
		envPgHost = "DB_SERVER"
		envPgPort = "DB_PORT"
		envHome   = "HOME"
	)

	pgHost := env.String(envPgHost, "localhost")
	pgPort := env.Int(envPgPort, 5432)
	// ...

	home := env.MustString(envHome)
 
  // ...
}

Documentation

Overview

Package envir reads environment variables.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLookup   = NewLookupOs()
	DefaultExpander = asisExpander{}
)
View Source
var DefaultDecoder = decoders.NewEnv()

Functions

func Bool

func Bool(name string, defaultValue bool) bool

Bool read env as unsigned integer w/ defaults if no such env var.

func BoolValue

func BoolValue(name string) bool

BoolValue treats env string as boolean: 1, yes, Y, true, etc are true, exceptions are false. DEPRECATED: use MustBool instead.

func Duration

func Duration(name string, defaultValue time.Duration) time.Duration

Duration read env as time duration w/ defaults if no such env var.

func Float64

func Float64(name string, defaultValue float64) float64

func Int

func Int(name string, defaultValue int) int

Int reads env as integer w/ defaults if no such env var.

func Int64

func Int64(name string, defaultValue int64) int64

func IntValue

func IntValue(name string) int

IntValue returns env variable as integer, panics in case of error. DEPRECATED: use MustInt.

func IsPresent

func IsPresent(name string) bool

IsPresent indicates env var is set (can be empty value). DEPRECATED: use IsSet

func IsSet

func IsSet(name string) bool

IsSet indicates env var is set (can be empty value).

func MustBool

func MustBool(name string) bool

MustBool returns value or panics if there is no variable present.

func MustDuration

func MustDuration(name string) time.Duration

MustDuration returns value or panics if there is no variable present.

func MustFloat64

func MustFloat64(name string) float64

MustFloat64 returns value or panics if there is no variable present.

func MustInt

func MustInt(name string) int

MustInt returns value or panics if there is no variable present.

func MustInt64

func MustInt64(name string) int64

MustInt64 returns value or panics if there is no variable present.

func MustString

func MustString(name string) string

MustString returns value or panics if there is no variable present.

func MustUint

func MustUint(name string) uint

MustUint returns value or panics if there is no variable present.

func MustUint64

func MustUint64(name string) uint64

MustUint64 returns value or panics if there is no variable present.

func New

func New(options ...func(oinment *oinment)) *oinment

func NewLookupComposition

func NewLookupComposition(primary lookupInterface, opts ...func(*lookupComposition)) *lookupComposition

func NewLookupFile

func NewLookupFile(path string, opts ...func(*lookupFile)) *lookupFile

func NewLookupOs

func NewLookupOs() wrapOs

func String

func String(name, defaultValue string) string

String read env as string w/ defaults if no such env var.

func StringValue

func StringValue(name string) string

StringValue returns env variable as string, panics in case of error. DEPRECATED: use MustString instead.

func Uint

func Uint(name string, defaultValue uint) uint

Uint read env as unsigned integer w/ defaults if no such env var.

func Uint64

func Uint64(name string, defaultValue uint64) uint64

func UintValue

func UintValue(name string) uint

UintValue returns env variable as integer, panics in case of error. DEPRECATED: use MustUint.

func Value

func Value(name string) string

Value returns value or empty if not present.

func WithCutPrefix

func WithCutPrefix(cutPrefix string) func(*lookupFile)

func WithDecoder

func WithDecoder(decoder decoderInterface) func(*lookupFile)

func WithEnvSubst

func WithEnvSubst() func(*oinment)

func WithLookup

func WithLookup(lookuper lookupInterface) func(*oinment)

func WithMustDecode

func WithMustDecode() func(*lookupFile)

func WithOptional

func WithOptional(optionals ...lookupInterface) func(*lookupComposition)

func WithPrefix

func WithPrefix(prefix string) func(*oinment)

Types

This section is empty.

Directories

Path Synopsis
examples
env command
withflags command
yaml command
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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