Skip to content

Commit

Permalink
👔 up(all): update some util func, add some new util func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 30, 2023
1 parent 049780e commit 1784bb5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
6 changes: 4 additions & 2 deletions arrutil/collection_gte118.go
Expand Up @@ -2,12 +2,14 @@ package arrutil

// type MapFn func(obj T) (target V, find bool)

// Map an object list [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...]
// Map a list to new list
//
// eg: mapping [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...]
func Map[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V {
flatArr := make([]V, 0, len(list))

for _, obj := range list {
if target, find := mapFn(obj); find {
if target, ok := mapFn(obj); ok {
flatArr = append(flatArr, target)
}
}
Expand Down
2 changes: 2 additions & 0 deletions fsutil/info_windows.go
@@ -1,6 +1,8 @@
package fsutil

import (
"path/filepath"

"github.com/gookit/goutil/internal/comfunc"
)

Expand Down
21 changes: 12 additions & 9 deletions internal/comfunc/sysfunc.go
Expand Up @@ -78,23 +78,26 @@ var curShell string
//
// eg "/bin/zsh" "/bin/bash".
// if onlyName=true, will return "zsh", "bash"
func CurrentShell(onlyName bool) (path string) {
func CurrentShell(onlyName bool) (binPath string) {
var err error
if curShell == "" {
path, err = ShellExec("echo $SHELL")
if err != nil {
return ""
binPath = os.Getenv("SHELL")
if len(binPath) == 0 {
binPath, err = ShellExec("echo $SHELL")
if err != nil {
return ""
}
}

path = strings.TrimSpace(path)
binPath = strings.TrimSpace(binPath)
// cache result
curShell = path
curShell = binPath
} else {
path = curShell
binPath = curShell
}

if onlyName && len(path) > 0 {
path = filepath.Base(path)
if onlyName && len(binPath) > 0 {
binPath = filepath.Base(binPath)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions structs/value.go
Expand Up @@ -107,7 +107,7 @@ func (v *Value) Strings() (ss []string) {
return
}

// SplitToStrings split string value to strings
// SplitToStrings split string value to strings. sep default is comma(,)
func (v *Value) SplitToStrings(sep ...string) (ss []string) {
if v.V == nil {
return
Expand All @@ -119,7 +119,7 @@ func (v *Value) SplitToStrings(sep ...string) (ss []string) {
return
}

// SplitToInts split string value to []int
// SplitToInts split string value to []int. sep default is comma(,)
func (v *Value) SplitToInts(sep ...string) (ss []int) {
if v.V == nil {
return
Expand Down
8 changes: 6 additions & 2 deletions sysutil/sysutil.go
Expand Up @@ -15,8 +15,12 @@ func Workdir() string {

// BinDir get
func BinDir() string {
binFile := os.Args[0]
return path.Dir(binFile)
return path.Dir(os.Args[0])
}

// BinName get
func BinName() string {
return path.Base(os.Args[0])
}

// BinFile get
Expand Down

0 comments on commit 1784bb5

Please sign in to comment.