Skip to content

Commit

Permalink
✨ feat(fs): add new util func ResolvePath() and update IsAbsPath() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 16, 2023
1 parent 4120075 commit 7797e83
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 110 deletions.
5 changes: 2 additions & 3 deletions fsutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"os"
"path"
"path/filepath"
)

// perm for create dir or file
Expand Down Expand Up @@ -71,9 +72,7 @@ func IsFile(path string) bool {
}

// IsAbsPath is abs path.
func IsAbsPath(aPath string) bool {
return path.IsAbs(aPath)
}
func IsAbsPath(aPath string) bool { return filepath.IsAbs(aPath) }

// ImageMimeTypes refer net/http package
var ImageMimeTypes = map[string]string{
Expand Down
79 changes: 9 additions & 70 deletions fsutil/info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fsutil

import (
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -42,82 +41,22 @@ func Suffix(fpath string) string { return path.Ext(fpath) }

// Expand will parse first `~` as user home dir path.
func Expand(pathStr string) string {
return comfunc.ExpandPath(pathStr)
return comfunc.ExpandHome(pathStr)
}

// ExpandPath will parse `~` as user home dir path.
func ExpandPath(pathStr string) string {
return comfunc.ExpandPath(pathStr)
return comfunc.ExpandHome(pathStr)
}

// ResolvePath will parse `~` and env var in path
func ResolvePath(pathStr string) string {
pathStr = comfunc.ExpandHome(pathStr)
// return comfunc.ParseEnvVar()
return os.ExpandEnv(pathStr)
}

// SplitPath splits path immediately following the final Separator, separating it into a directory and file name component
func SplitPath(pathStr string) (dir, name string) {
return filepath.Split(pathStr)
}

// GlobWithFunc handle matched file
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error) {
files, err := filepath.Glob(pattern)
if err != nil {
return err
}

for _, filePath := range files {
err = fn(filePath)
if err != nil {
break
}
}
return
}

type (
// FilterFunc type for FindInDir
FilterFunc func(fPath string, ent fs.DirEntry) bool
// HandleFunc type for FindInDir
HandleFunc func(fPath string, ent fs.DirEntry) error
)

// FindInDir code refer the go pkg: path/filepath.glob()
// - tip: will be not find in subdir.
//
// filters: return false will skip the file.
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error) {
fi, err := os.Stat(dir)
if err != nil || !fi.IsDir() {
return // ignore I/O error
}

// names, _ := d.Readdirnames(-1)
// sort.Strings(names)

des, err := os.ReadDir(dir)
if err != nil {
return
}

for _, ent := range des {
baseName := ent.Name()
filePath := dir + "/" + baseName

// call filters
if len(filters) > 0 {
var filtered = false
for _, filter := range filters {
if !filter(filePath, ent) {
filtered = true
break
}
}

if filtered {
continue
}
}

if err := handleFn(filePath, ent); err != nil {
return err
}
}
return nil
}
38 changes: 1 addition & 37 deletions fsutil/info_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package fsutil_test

import (
"io/fs"
"strings"
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
)
Expand All @@ -16,40 +12,8 @@ func TestExpandPath(t *testing.T) {

assert.NotEq(t, path, fsutil.Expand(path))
assert.NotEq(t, path, fsutil.ExpandPath(path))
assert.NotEq(t, path, fsutil.ResolvePath(path))

assert.Eq(t, "", fsutil.Expand(""))
assert.Eq(t, "/path/to", fsutil.Expand("/path/to"))
}

func TestFindInDir(t *testing.T) {
err := fsutil.FindInDir("path-not-exist", nil)
assert.NoErr(t, err)

err = fsutil.FindInDir("testdata/test.jpg", nil)
assert.NoErr(t, err)

files := make([]string, 0, 8)
err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
files = append(files, fPath)
return nil
})

dump.P(files)
assert.NoErr(t, err)
assert.True(t, len(files) > 0)

files = files[:0]
err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
files = append(files, fPath)
return nil
}, func(fPath string, de fs.DirEntry) bool {
return !strings.HasPrefix(de.Name(), ".")
})
assert.NoErr(t, err)
assert.True(t, len(files) > 0)

err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
return errorx.Raw("handle error")
})
assert.Err(t, err)
}

0 comments on commit 7797e83

Please sign in to comment.