diff --git a/envutil/README.md b/envutil/README.md new file mode 100644 index 000000000..1f727c7e1 --- /dev/null +++ b/envutil/README.md @@ -0,0 +1,60 @@ +# Env Util + +Provide some commonly ENV util functions. + +## Install + +```shell +go get github.com/gookit/goutil/envutil +``` + +## Go docs + +- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/envutil) + +## Functions API + +```go +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 + +```bash +gofmt -w -l ./ +golint ./... +``` + +**Testing**: + +```shell +go test -v ./envutil/... +``` + +**Test limit by regexp**: + +```shell +go test -v -run ^TestSetByKeys ./envutil/... +``` diff --git a/envutil/envutil.go b/envutil/envutil.go index f56531651..9680cefdd 100644 --- a/envutil/envutil.go +++ b/envutil/envutil.go @@ -40,3 +40,10 @@ func ParseEnvValue(val string) string { func ParseValue(val string) (newVal string) { return comfunc.ParseEnvVar(val, ValueGetter) } + +// SetEnvs to os +func SetEnvs(mp map[string]string) { + for key, value := range mp { + _ = os.Setenv(key, value) + } +} diff --git a/envutil/get.go b/envutil/get.go index b6a562db9..19b7ee439 100644 --- a/envutil/get.go +++ b/envutil/get.go @@ -19,7 +19,7 @@ func Getenv(name string, def ...string) string { // GetInt get int ENV value by key name, can with default value func GetInt(name string, def ...int) int { if val := os.Getenv(name); val != "" { - return strutil.MustInt(val) + return strutil.QuietInt(val) } if len(def) > 0 { @@ -31,7 +31,7 @@ func GetInt(name string, def ...int) int { // GetBool get bool ENV value by key name, can with default value func GetBool(name string, def ...bool) bool { if val := os.Getenv(name); val != "" { - return strutil.MustBool(val) + return strutil.QuietBool(val) } if len(def) > 0 { diff --git a/envutil/info.go b/envutil/info.go index 78a814360..d4f799683 100644 --- a/envutil/info.go +++ b/envutil/info.go @@ -67,7 +67,8 @@ func IsWSL() bool { // IsTerminal isatty check // // Usage: -// envutil.IsTerminal(os.Stdout.Fd()) +// +// envutil.IsTerminal(os.Stdout.Fd()) func IsTerminal(fd uintptr) bool { return isatty.IsTerminal(fd) } @@ -83,22 +84,27 @@ func IsConsole(out io.Writer) bool { } // HasShellEnv has shell env check. +// // Usage: -// HasShellEnv("sh") -// HasShellEnv("bash") +// +// HasShellEnv("sh") +// HasShellEnv("bash") func HasShellEnv(shell string) bool { return comfunc.HasShellEnv(shell) } // Support color: -// "TERM=xterm" -// "TERM=xterm-vt220" -// "TERM=xterm-256color" -// "TERM=screen-256color" -// "TERM=tmux-256color" -// "TERM=rxvt-unicode-256color" +// +// "TERM=xterm" +// "TERM=xterm-vt220" +// "TERM=xterm-256color" +// "TERM=screen-256color" +// "TERM=tmux-256color" +// "TERM=rxvt-unicode-256color" +// // Don't support color: -// "TERM=cygwin" +// +// "TERM=cygwin" var specialColorTerms = map[string]bool{ "alacritty": true, } @@ -106,10 +112,12 @@ var specialColorTerms = map[string]bool{ // IsSupportColor check current console is support color. // // Supported: -// linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe +// +// linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe // // Not support: -// windows cmd.exe, powerShell.exe +// +// windows cmd.exe, powerShell.exe func IsSupportColor() bool { envTerm := os.Getenv("TERM") if strings.Contains(envTerm, "xterm") { @@ -155,3 +163,8 @@ func IsSupportTrueColor() bool { // "COLORTERM=truecolor" return strings.Contains(os.Getenv("COLORTERM"), "truecolor") } + +// IsGithubActions env +func IsGithubActions() bool { + return os.Getenv("GITHUB_ACTIONS") == "true" +} diff --git a/envutil/info_test.go b/envutil/info_test.go index ca9243f08..bffec8c54 100644 --- a/envutil/info_test.go +++ b/envutil/info_test.go @@ -48,6 +48,15 @@ func TestIsConsole(t *testing.T) { is.True(envutil.HasShellEnv("sh")) } +func TestIsGithubActions(t *testing.T) { + is := assert.New(t) + + testutil.MockEnvValue("GITHUB_ACTIONS", "true", func(nv string) { + is.Eq("true", nv) + is.True(envutil.IsGithubActions()) + }) +} + func TestIsSupportColor(t *testing.T) { is := assert.New(t) diff --git a/sysutil/cmdr/cmd.go b/sysutil/cmdr/cmd.go index 45a77c8b4..08b0c3e63 100644 --- a/sysutil/cmdr/cmd.go +++ b/sysutil/cmdr/cmd.go @@ -82,7 +82,7 @@ func (c *Cmd) lookPath(name string) { c.Path = lp } if err != nil { - c.Err = err + panic("lookPath error:" + err.Error()) } } }