From 1d52b3e946d87bd28b95b046e356c1a5eb381071 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 11 Jun 2023 20:28:10 +0800 Subject: [PATCH] :sparkles: feat: fsutil - add new func RemoveSub() quick remove subs in dir --- fsutil/find.go | 9 +++++++++ fsutil/find_test.go | 1 + fsutil/finder/config.go | 34 +++++++++++++++++----------------- fsutil/operate.go | 13 +++++++++++++ fsutil/operate_test.go | 3 +++ 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/fsutil/find.go b/fsutil/find.go index 01bef6bfc..66d76e760 100644 --- a/fsutil/find.go +++ b/fsutil/find.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" + "github.com/gookit/goutil/arrutil" "github.com/gookit/goutil/strutil" ) @@ -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 ) @@ -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 { diff --git a/fsutil/find_test.go b/fsutil/find_test.go index 9b705b6cd..6fa759e9a 100644 --- a/fsutil/find_test.go +++ b/fsutil/find_test.go @@ -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) { diff --git a/fsutil/finder/config.go b/fsutil/finder/config.go index 317afcf14..1d81d3fd5 100644 --- a/fsutil/finder/config.go +++ b/fsutil/finder/config.go @@ -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 @@ -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 diff --git a/fsutil/operate.go b/fsutil/operate.go index 2b3221791..5bf1e600a 100644 --- a/fsutil/operate.go +++ b/fsutil/operate.go @@ -4,6 +4,7 @@ import ( "archive/zip" "fmt" "io" + "io/fs" "os" "path" "path/filepath" @@ -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 // ************************************************************ diff --git a/fsutil/operate_test.go b/fsutil/operate_test.go index e45d3ef96..fb569a2c6 100644 --- a/fsutil/operate_test.go +++ b/fsutil/operate_test.go @@ -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) {