Skip to content

Commit

Permalink
up: env - add some env func, update readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 18, 2022
1 parent f13aac2 commit f5ee49b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 15 deletions.
60 changes: 60 additions & 0 deletions envutil/README.md
Original file line number Diff line number Diff line change
@@ -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/...
```
7 changes: 7 additions & 0 deletions envutil/envutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions envutil/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
37 changes: 25 additions & 12 deletions envutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -83,33 +84,40 @@ 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,
}

// 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") {
Expand Down Expand Up @@ -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"
}
9 changes: 9 additions & 0 deletions envutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sysutil/cmdr/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Cmd) lookPath(name string) {
c.Path = lp
}
if err != nil {
c.Err = err
panic("lookPath error:" + err.Error())
}
}
}
Expand Down

0 comments on commit f5ee49b

Please sign in to comment.