envutil

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 11 Imported by: 32

README

Env Util

Provide some commonly system or go ENV util functions.

Install

go get github.com/gookit/goutil/envutil

Go docs

Functions API

Note: doc by run go doc ./envutil

func Environ() map[string]string
func GetBool(name string, def ...bool) bool
func GetInt(name string, def ...int) int
func Getenv(name string, def ...string) string
func HasShellEnv(shell string) bool
func IsConsole(out io.Writer) bool
func IsGithubActions() bool
func IsLinux() bool
func IsMSys() bool
func IsMac() bool
func IsSupport256Color() bool
func IsSupportColor() bool
func IsSupportTrueColor() bool
func IsTerminal(fd uintptr) bool
func IsWSL() bool
func IsWin() bool
func IsWindows() bool
func ParseEnvValue(val string) string
func ParseValue(val string) (newVal string)
func SetEnvs(mp map[string]string)
func StdIsTerminal() bool
func VarParse(val string) string
func VarReplace(s string) string

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./envutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./envutil/...

Documentation

Overview

Package envutil provide some commonly ENV util functions.

Index

Constants

View Source
const DefaultEnvFile = ".env"

DefaultEnvFile default file name

Variables

View Source
var ValueGetter = os.Getenv

ValueGetter Env value provider func.

TIPS: you can custom provide data.

Functions

func DotenvLoad added in v0.7.3

func DotenvLoad(fns ...func(cfg *Dotenv)) error

DotenvLoad load dotenv file and parse to ENV

func EnvMap added in v0.6.7

func EnvMap() map[string]string

EnvMap like os.Environ, but will returns key-value map[string]string data.

func EnvPaths added in v0.6.2

func EnvPaths() []string

EnvPaths get and split $PATH to []string

func Environ added in v0.3.13

func Environ() map[string]string

Environ like os.Environ, but will returns key-value map[string]string data.

func GetBool added in v0.5.2

func GetBool(name string, def ...bool) bool

GetBool get bool ENV value by key name, can with default value

func GetInt added in v0.5.2

func GetInt(name string, def ...int) int

GetInt get int ENV value by key name, can with default value

func GetMulti added in v0.6.3

func GetMulti(names ...string) map[string]string

GetMulti ENV values by input names.

func GetOne added in v0.7.0

func GetOne(names []string, defVal ...string) string

GetOne get one not empty ENV value by input names.

func Getenv added in v0.1.7

func Getenv(name string, def ...string) string

Getenv get ENV value by key name, can with default value

func HasEnv added in v0.7.3

func HasEnv(name string) bool

HasEnv check ENV key exists

func HasShellEnv

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

HasShellEnv("sh")
HasShellEnv("bash")

func IsConsole

func IsConsole(out io.Writer) bool

IsConsole check out is console env. alias of the sysutil.IsConsole()

func IsGithubActions added in v0.6.0

func IsGithubActions() bool

IsGithubActions env

func IsLinux

func IsLinux() bool

IsLinux system

func IsMSys

func IsMSys() bool

IsMSys msys(MINGW64) env. alias of the sysutil.IsMSys()

func IsMac

func IsMac() bool

IsMac system

func IsSupport256Color

func IsSupport256Color() bool

IsSupport256Color render

func IsSupportColor

func IsSupportColor() bool

IsSupportColor check current console is support color.

Supported:

linux, mac, or Windows's ConEmu, Cmder, putty, git-bash.exe

Not support:

windows cmd.exe, powerShell.exe

func IsSupportTrueColor added in v0.2.5

func IsSupportTrueColor() bool

IsSupportTrueColor render. IsSupportRGBColor

func IsTerminal added in v0.3.10

func IsTerminal(fd uintptr) bool

IsTerminal isatty check

Usage:

envutil.IsTerminal(os.Stdout.Fd())

func IsWin

func IsWin() bool

IsWin system. linux windows darwin

func IsWindows added in v0.3.10

func IsWindows() bool

IsWindows system. alias of IsWin

func LoadEnvFiles added in v0.7.3

func LoadEnvFiles(baseDir string, files ...string) error

LoadEnvFiles load dotenv files and parse to ENV

func LoadString added in v0.7.0

func LoadString(line string) bool

LoadString set line to ENV. e.g.: "KEY=VALUE"

func LoadText added in v0.7.0

func LoadText(text string)

LoadText parse multiline text to ENV. Can use to load .env file contents.

Usage:

envutil.LoadText(fsutil.ReadFile(".env"))

func LoadedEnvFiles added in v0.7.3

func LoadedEnvFiles() []string

LoadedEnvFiles get loaded dotenv files

func MustGet added in v0.7.0

func MustGet(name string) string

MustGet get ENV value by key name, if not exists or empty, will panic

func OnExist added in v0.7.0

func OnExist(name string, fn func(val string)) bool

OnExist check ENV value by key name, will call fn on value exists(not-empty)

func ParseEnvValue added in v0.1.7

func ParseEnvValue(val string) string

ParseEnvValue alias of the ParseValue

func ParseOrErr added in v0.6.13

func ParseOrErr(val string) (string, error)

ParseOrErr parse ENV var value from input string, support default value.

Diff with the ParseValue, this support return error.

With error format: ${VAR_NAME | ?error}

func ParseValue added in v0.5.2

func ParseValue(val string) string

ParseValue parse ENV var value from input string, support default value.

Format:

${var_name}            Only var name
${var_name | default}  With default value

Usage:

envutil.ParseValue("${ APP_NAME }")
envutil.ParseValue("${ APP_ENV | dev }")

func SearchEnv added in v0.6.2

func SearchEnv(keywords string, matchValue bool) map[string]string

SearchEnv values by given keywords

func SearchEnvKeys added in v0.6.2

func SearchEnvKeys(keywords string) map[string]string

SearchEnvKeys values by given keywords

func SetEnvMap added in v0.6.3

func SetEnvMap(mp map[string]string)

SetEnvMap set multi ENV(string-map) to os

func SetEnvs added in v0.6.0

func SetEnvs(kvPairs ...string)

SetEnvs set multi k-v ENV pairs to os

func SplitLineToKv added in v0.7.0

func SplitLineToKv(line string) (string, string)

SplitLineToKv parse ENV line to k-v. eg: 'DEBUG=true' => ['DEBUG', 'true']

func SplitText2map added in v0.7.0

func SplitText2map(text string) map[string]string

SplitText2map parse ENV text to map. Can use to parse .env file contents.

func StdIsTerminal added in v0.3.11

func StdIsTerminal() bool

StdIsTerminal os.Stdout is terminal

func UnsetEnvs added in v0.6.3

func UnsetEnvs(keys ...string)

UnsetEnvs from os

func VarParse added in v0.3.11

func VarParse(val string) string

VarParse alias of the ParseValue

func VarReplace added in v0.3.11

func VarReplace(s string) string

VarReplace replaces ${var} or $var in the string according to the values.

is alias of the os.ExpandEnv()

Types

type Dotenv added in v0.7.3

type Dotenv struct {
	// Files dot env file paths, allow multi files.
	//  - filename support simple glob pattern. eg: ".env.*"
	//
	// default: [".env"]
	Files []string
	// BaseDir base dir for join files path
	//
	// default is workdir
	BaseDir string
	// UpperKey change key to upper on set ENV. default: true
	UpperKey bool
	// IgnoreNotExist only load exists.
	//
	// - default: false - will return error if not exists
	IgnoreNotExist bool
	// LoadFirstExist only load first exists env file on Files
	LoadFirstExist bool
	// contains filtered or unexported fields
}

Dotenv load and parse dotenv files

func NewDotenv added in v0.7.3

func NewDotenv() *Dotenv

NewDotenv create a new dotenv config

func StdDotenv added in v0.7.3

func StdDotenv() *Dotenv

StdDotenv get standard dotenv instance

func (*Dotenv) LoadAndInit added in v0.7.3

func (c *Dotenv) LoadAndInit() error

LoadAndInit load dotenv files and parse to os.Environ

func (*Dotenv) LoadFiles added in v0.7.3

func (c *Dotenv) LoadFiles(files ...string) error

LoadFiles append load dotenv files

  • filename support simple glob pattern. eg: ".env.*"

func (*Dotenv) LoadText added in v0.7.3

func (c *Dotenv) LoadText(contents string) error

LoadText load dotenv contents and parse to os Env

func (*Dotenv) LoadedData added in v0.7.3

func (c *Dotenv) LoadedData() map[string]string

LoadedData get loaded dotenv data map

func (*Dotenv) LoadedFiles added in v0.7.3

func (c *Dotenv) LoadedFiles() []string

LoadedFiles get loaded dotenv files

func (*Dotenv) Reset added in v0.7.3

func (c *Dotenv) Reset()

Reset unload all loaded ENV and reset data

func (*Dotenv) UnloadEnv added in v0.7.3

func (c *Dotenv) UnloadEnv() bool

UnloadEnv remove loaded dotenv data from os.Environ

Jump to

Keyboard shortcuts

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