Skip to content

Commit

Permalink
✨ feat: fsutil - add new func RemoveSub() quick remove subs in dir
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 11, 2023
1 parent 3c90863 commit 1d52b3e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
9 changes: 9 additions & 0 deletions fsutil/find.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

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

Expand Down Expand Up @@ -69,6 +70,7 @@ type (
//
// - return False will skip handle the file.
FilterFunc func(fPath string, ent fs.DirEntry) bool

// HandleFunc type for FindInDir
HandleFunc func(fPath string, ent fs.DirEntry) error
)
Expand All @@ -83,6 +85,13 @@ func OnlyFindFile(_ string, ent fs.DirEntry) bool {
return !ent.IsDir()
}

// ExcludeNames on find
func ExcludeNames(names ...string) FilterFunc {
return func(_ string, ent fs.DirEntry) bool {
return !arrutil.StringsHas(names, ent.Name())
}
}

// IncludeSuffix on find
func IncludeSuffix(ss ...string) FilterFunc {
return func(_ string, ent fs.DirEntry) bool {
Expand Down
1 change: 1 addition & 0 deletions fsutil/find_test.go
Expand Up @@ -60,6 +60,7 @@ func TestApplyFilters(t *testing.T) {
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.OnlyFindFile}))
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.ExcludeDotFile}))
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.IncludeSuffix("-backup")}))
assert.True(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.ExcludeNames("some-backup")}))
}

func TestFindInDir(t *testing.T) {
Expand Down
34 changes: 17 additions & 17 deletions fsutil/finder/config.go
Expand Up @@ -36,19 +36,19 @@ type Config struct {
depth int

// ScanDirs scan dir paths for find.
ScanDirs []string
ScanDirs []string `json:"scan_dirs"`
// FindFlags for find result. default is FlagFile
FindFlags FindFlag
FindFlags FindFlag `json:"find_flags"`
// MaxDepth for find result. default is 0 - not limit
MaxDepth int
MaxDepth int `json:"max_depth"`
// UseAbsPath use abs path for find result. default is false
UseAbsPath bool
UseAbsPath bool `json:"use_abs_path"`
// CacheResult cache result for find result. default is false
CacheResult bool
CacheResult bool `json:"cache_result"`
// ExcludeDotDir exclude dot dir. default is true
ExcludeDotDir bool
ExcludeDotDir bool `json:"exclude_dot_dir"`
// ExcludeDotFile exclude dot dir. default is false
ExcludeDotFile bool
ExcludeDotFile bool `json:"exclude_dot_file"`

// Matchers generic include matchers for file/dir elems
Matchers []Matcher
Expand All @@ -66,26 +66,26 @@ type Config struct {
// commonly settings for build matchers

// IncludeDirs include dir name list. eg: {"model"}
IncludeDirs []string
IncludeDirs []string `json:"include_dirs"`
// IncludeExts include file ext name list. eg: {".go", ".md"}
IncludeExts []string
IncludeExts []string `json:"include_exts"`
// IncludeFiles include file name list. eg: {"go.mod"}
IncludeFiles []string
IncludeFiles []string `json:"include_files"`
// IncludePaths include file/dir path list. eg: {"path/to"}
IncludePaths []string
IncludePaths []string `json:"include_paths"`
// IncludeNames include file/dir name list. eg: {"test", "some.go"}
IncludeNames []string
IncludeNames []string `json:"include_names"`

// ExcludeDirs exclude dir name list. eg: {"test"}
ExcludeDirs []string
ExcludeDirs []string `json:"exclude_dirs"`
// ExcludeExts exclude file ext name list. eg: {".go", ".md"}
ExcludeExts []string
ExcludeExts []string `json:"exclude_exts"`
// ExcludeFiles exclude file name list. eg: {"go.mod"}
ExcludeFiles []string
ExcludeFiles []string `json:"exclude_files"`
// ExcludePaths exclude file/dir path list. eg: {"path/to"}
ExcludePaths []string
ExcludePaths []string `json:"exclude_paths"`
// ExcludeNames exclude file/dir name list. eg: {"test", "some.go"}
ExcludeNames []string
ExcludeNames []string `json:"exclude_names"`
}

// NewConfig create a new Config
Expand Down
13 changes: 13 additions & 0 deletions fsutil/operate.go
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -194,6 +195,18 @@ func DeleteIfFileExist(fPath string) error {
return nil
}

// RemoveSub removes all sub files and dirs of dirPath, but not remove dirPath.
func RemoveSub(dirPath string, fns ...FilterFunc) error {
return FindInDir(dirPath, func(fPath string, ent fs.DirEntry) error {
if ent.IsDir() {
if err := RemoveSub(fPath, fns...); err != nil {
return err
}
}
return os.Remove(fPath)
}, fns...)
}

// ************************************************************
// other operates
// ************************************************************
Expand Down
3 changes: 3 additions & 0 deletions fsutil/operate_test.go
Expand Up @@ -62,6 +62,9 @@ func TestCreateFile(t *testing.T) {
assert.NoErr(t, fsutil.RmFileIfExist(fpath))
file = fsutil.MustCreateFile(fpath, 0, 0766)
assert.NoErr(t, file.Close())

err = fsutil.RemoveSub("./testdata/sub")
assert.NoErr(t, err)
}

func TestQuickOpenFile(t *testing.T) {
Expand Down

0 comments on commit 1d52b3e

Please sign in to comment.