Skip to content

Commit

Permalink
✨ feat: fsutil - add new func: FilePathInDirs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 16, 2023
1 parent 9723aea commit a2257a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fsutil/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,30 @@ import (
"path/filepath"

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

// FilePathInDirs get full file path in dirs.
//
// Params:
// - file: can be relative path, file name, full path.
// - dirs: dir paths
func FilePathInDirs(file string, dirs ...string) string {
file = comfunc.ExpandHome(file)
if FileExists(file) {
return file
}

for _, dirPath := range dirs {
fPath := JoinSubPaths(dirPath, file)
if FileExists(fPath) {
return fPath
}
}
return "" // not found
}

// FirstExists check multi paths and return first exists path.
func FirstExists(paths ...string) string {
return MatchFirst(paths, PathExists, "")
Expand Down
1 change: 1 addition & 0 deletions fsutil/info_nonwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

// Realpath returns the shortest path name equivalent to path by purely lexical processing.
// Will expand ~ to home dir, and join with workdir if path is relative path.
func Realpath(pathStr string) string {
pathStr = comfunc.ExpandHome(pathStr)

Expand Down

0 comments on commit a2257a1

Please sign in to comment.