envutil

package
v0.0.0-...-f5003fd Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 11 Imported by: 0

README

Env Util

Provide some commonly system or go ENV util functions.

Install

go get github.com/Joaolfc0/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

This section is empty.

Variables

View Source
var ValueGetter = os.Getenv

ValueGetter Env value provider func.

TIPS: you can custom provide data.

Functions

func EnvMap

func EnvMap() map[string]string

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

func EnvPaths

func EnvPaths() []string

EnvPaths get and split $PATH to []string

func Environ

func Environ() map[string]string

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

func GetBool

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

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

func GetInt

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

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

func GetMulti

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

GetMulti ENV values by input names.

func Getenv

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

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

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

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

func IsSupportTrueColor() bool

IsSupportTrueColor render. IsSupportRGBColor

func IsTerminal

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

func IsWindows() bool

IsWindows system. alias of IsWin

func ParseEnvValue

func ParseEnvValue(val string) string

ParseEnvValue alias of the ParseValue

func ParseOrErr

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

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

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

SearchEnv values by given keywords

func SearchEnvKeys

func SearchEnvKeys(keywords string) map[string]string

SearchEnvKeys values by given keywords

func SetEnvMap

func SetEnvMap(mp map[string]string)

SetEnvMap set multi ENV(string-map) to os

func SetEnvs

func SetEnvs(kvPairs ...string)

SetEnvs set multi k-v ENV pairs to os

func StdIsTerminal

func StdIsTerminal() bool

StdIsTerminal os.Stdout is terminal

func UnsetEnvs

func UnsetEnvs(keys ...string)

UnsetEnvs from os

func VarParse

func VarParse(val string) string

VarParse alias of the ParseValue

func VarReplace

func VarReplace(s string) string

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

is alias of the os.ExpandEnv()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳