package envutil

import "github.com/bitrise-io/go-utils/envutil"

Index

Functions

func GetenvWithDefault

func GetenvWithDefault(envKey, defValue string) string

GetenvWithDefault - returns the env if specified, default value otherwise

func RequiredEnv

func RequiredEnv(envKey string) (string, error)

RequiredEnv - returns the env's value if specified, otherwise it returns an error that explains the key is required. Use this function to reduce error prone code duplication. E.g. instead of doing this in your code:

myVar1 := os.Getenv("MY_ENV1")
if len(myVar1) < 1 {
	return nil, errors.New("MY_ENV1 required")
}

You can use this function like:

myVar1, err := requiredEnv("MY_ENV1")
if err != nil {
	return nil, errors.WithStack(err)
}

In the first example you have to specify myVar1 and MY_ENV1 two times, which can lead to issues if you copy-paste that code but e.g. forget to change the var name in the

if len(myVar1) < 1

line, or if you forget to change the var key in the error message/string.

func RevokableSetenv

func RevokableSetenv(envKey, envValue string) (func() error, error)

RevokableSetenv ...

func RevokableSetenvs

func RevokableSetenvs(envs map[string]string) (func() error, error)

RevokableSetenvs ...

func SetenvForFunction

func SetenvForFunction(envKey, envValue string, fn func()) error

SetenvForFunction ...

func SetenvsForFunction

func SetenvsForFunction(envs map[string]string, fn func()) error

SetenvsForFunction ...

func StringFlagOrEnv

func StringFlagOrEnv(flagValue *string, envKey string) string

StringFlagOrEnv - returns the value of the flag if specified, otherwise the env's value. Empty string counts as not specified!

Source Files

envutil.go

Version
v1.0.15 (latest)
Published
May 13, 2025
Platform
linux/amd64
Imports
2 packages
Last checked
2 months ago

Tools for package owners.