Skip to content

Commit

Permalink
✨ feat: fsutil - add some new util func MatchPaths(), MatchFirst()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 25, 2023
1 parent d2fc75f commit e3b3bfd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 36 deletions.
36 changes: 36 additions & 0 deletions fsutil/find.go
Expand Up @@ -11,6 +11,42 @@ import (
"github.com/gookit/goutil/strutil"
)

// FirstExists check multi paths and return first exists path.
func FirstExists(paths ...string) string {
return MatchFirst(paths, PathExists, "")
}

// FirstExistsDir check multi paths and return first exists dir.
func FirstExistsDir(paths ...string) string {
return MatchFirst(paths, IsDir, "")
}

// FirstExistsFile check multi paths and return first exists file.
func FirstExistsFile(paths ...string) string {
return MatchFirst(paths, IsFile, "")
}

// MatchPaths given paths by custom mather func.
func MatchPaths(paths []string, matcher PathMatcher) []string {
var ret []string
for _, p := range paths {
if matcher(p) {
ret = append(ret, p)
}
}
return ret
}

// MatchFirst filter paths by filter func and return first match path.
func MatchFirst(paths []string, matcher PathMatcher, defaultPath string) string {
for _, p := range paths {
if matcher(p) {
return p
}
}
return defaultPath
}

// SearchNameUp find file/dir name in dirPath or parent dirs,
// return the name of directory path
//
Expand Down
11 changes: 11 additions & 0 deletions fsutil/find_test.go
Expand Up @@ -12,6 +12,17 @@ import (
"github.com/gookit/goutil/testutil/fakeobj"
)

func TestMatchFirst(t *testing.T) {
assert.Eq(t, "testdata", fsutil.MatchFirst([]string{"testdata"}, fsutil.IsDir, ""))

assert.Eq(t, "testdata", fsutil.FirstExists("not-exists", "testdata"))
assert.Eq(t, "testdata", fsutil.FirstExistsDir("not-exists", "testdata"))
assert.Eq(t, "testdata/test.jpg", fsutil.FirstExistsFile("not-exists", "testdata/test.jpg"))

ps := fsutil.MatchPaths([]string{"testdata", "testdata/test.jpg"}, fsutil.IsDir)
assert.Eq(t, []string{"testdata"}, ps)
}

func TestSearchNameUp(t *testing.T) {
p := fsutil.SearchNameUp("testdata", "finder")
assert.NotEmpty(t, p)
Expand Down
40 changes: 5 additions & 35 deletions fsutil/fsutil.go
Expand Up @@ -16,41 +16,12 @@ const (
MimeSniffLen = 512
)

// OSTempFile create a temp file on os.TempDir()
//
// Usage:
//
// fsutil.OSTempFile("example.*.txt")
func OSTempFile(pattern string) (*os.File, error) {
return os.CreateTemp(os.TempDir(), pattern)
}

// TempFile is like os.CreateTemp, but can custom temp dir.
//
// Usage:
//
// fsutil.TempFile("", "example.*.txt")
func TempFile(dir, pattern string) (*os.File, error) {
return os.CreateTemp(dir, pattern)
}
// PathMatcher path matcher func
type PathMatcher func(path string) bool

// OSTempDir creates a new temp dir on os.TempDir and return the temp dir path
//
// Usage:
//
// fsutil.OSTempDir("example.*")
func OSTempDir(pattern string) (string, error) {
return os.MkdirTemp(os.TempDir(), pattern)
}

// TempDir creates a new temp dir and return the temp dir path
//
// Usage:
//
// fsutil.TempDir("", "example.*")
// fsutil.TempDir("testdata", "example.*")
func TempDir(dir, pattern string) (string, error) {
return os.MkdirTemp(dir, pattern)
// DetectMime detect file mime type. alias of MimeType()
func DetectMime(path string) string {
return MimeType(path)
}

// MimeType get File Mime Type name. eg "image/png"
Expand All @@ -59,7 +30,6 @@ func MimeType(path string) (mime string) {
if err != nil {
return
}

return ReaderMimeType(file)
}

Expand Down
2 changes: 1 addition & 1 deletion fsutil/fsutil_test.go
Expand Up @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
}

func TestMimeType(t *testing.T) {
assert.Eq(t, "", fsutil.MimeType(""))
assert.Eq(t, "", fsutil.DetectMime(""))
assert.Eq(t, "", fsutil.MimeType("not-exist"))
assert.Eq(t, "image/jpeg", fsutil.MimeType("testdata/test.jpg"))

Expand Down
37 changes: 37 additions & 0 deletions fsutil/opwrite.go
Expand Up @@ -7,6 +7,43 @@ import (
"github.com/gookit/goutil/basefn"
)

// OSTempFile create a temp file on os.TempDir()
//
// Usage:
//
// fsutil.OSTempFile("example.*.txt")
func OSTempFile(pattern string) (*os.File, error) {
return os.CreateTemp(os.TempDir(), pattern)
}

// TempFile is like os.CreateTemp, but can custom temp dir.
//
// Usage:
//
// fsutil.TempFile("", "example.*.txt")
func TempFile(dir, pattern string) (*os.File, error) {
return os.CreateTemp(dir, pattern)
}

// OSTempDir creates a new temp dir on os.TempDir and return the temp dir path
//
// Usage:
//
// fsutil.OSTempDir("example.*")
func OSTempDir(pattern string) (string, error) {
return os.MkdirTemp(os.TempDir(), pattern)
}

// TempDir creates a new temp dir and return the temp dir path
//
// Usage:
//
// fsutil.TempDir("", "example.*")
// fsutil.TempDir("testdata", "example.*")
func TempDir(dir, pattern string) (string, error) {
return os.MkdirTemp(dir, pattern)
}

// ************************************************************
// write, copy files
// ************************************************************
Expand Down

0 comments on commit e3b3bfd

Please sign in to comment.