mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add RemoveDir
This commit is contained in:
@@ -35,6 +35,7 @@ import (
|
|||||||
- [IsDir](#IsDir)
|
- [IsDir](#IsDir)
|
||||||
- [ListFileNames](#ListFileNames)
|
- [ListFileNames](#ListFileNames)
|
||||||
- [RemoveFile](#RemoveFile)
|
- [RemoveFile](#RemoveFile)
|
||||||
|
- [RemoveDir](#RemoveDir)
|
||||||
- [ReadFileToString](#ReadFileToString)
|
- [ReadFileToString](#ReadFileToString)
|
||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
@@ -390,12 +391,12 @@ func main() {
|
|||||||
|
|
||||||
### <span id="RemoveFile">RemoveFile</span>
|
### <span id="RemoveFile">RemoveFile</span>
|
||||||
|
|
||||||
<p>删除文件</p>
|
<p>删除文件,支持传入删除前的回调函数。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string, onDelete ...func(path string)) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
||||||
@@ -416,6 +417,41 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RemoveDir">RemoveDir</span>
|
||||||
|
|
||||||
|
<p>删除目录,支持传入删除前的回调函数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveDir(path string, onDelete ...func(path string)) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var deletedPaths []string
|
||||||
|
|
||||||
|
err := fileutil.RemoveDir("./tempdir", func(p string) {
|
||||||
|
deletedPaths = append(deletedPaths, p)
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(deletedPaths)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>读取文件内容并返回字符串</p>
|
<p>读取文件内容并返回字符串</p>
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import (
|
|||||||
- [IsDir](#IsDir)
|
- [IsDir](#IsDir)
|
||||||
- [ListFileNames](#ListFileNames)
|
- [ListFileNames](#ListFileNames)
|
||||||
- [RemoveFile](#RemoveFile)
|
- [RemoveFile](#RemoveFile)
|
||||||
|
- [RemoveDir](#RemoveDir)
|
||||||
- [ReadFileToString](#ReadFileToString)
|
- [ReadFileToString](#ReadFileToString)
|
||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
@@ -395,7 +396,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string, onDelete ...func(path string)) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
||||||
@@ -416,6 +417,41 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RemoveDir">RemoveDir</span>
|
||||||
|
|
||||||
|
<p>Delete directory.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveDir(path string, onDelete ...func(path string)) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var deletedPaths []string
|
||||||
|
|
||||||
|
err := fileutil.RemoveDir("./tempdir", func(p string) {
|
||||||
|
deletedPaths = append(deletedPaths, p)
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(deletedPaths)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>Return string of file content.</p>
|
<p>Return string of file content.</p>
|
||||||
|
|||||||
@@ -172,10 +172,54 @@ func IsDir(path string) bool {
|
|||||||
|
|
||||||
// RemoveFile remove the path file.
|
// RemoveFile remove the path file.
|
||||||
// Play: https://go.dev/play/p/P2y0XW8a1SH
|
// Play: https://go.dev/play/p/P2y0XW8a1SH
|
||||||
func RemoveFile(path string) error {
|
func RemoveFile(path string, onDelete ...func(path string)) error {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
return fmt.Errorf("%s is a directory", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(onDelete) > 0 && onDelete[0] != nil {
|
||||||
|
onDelete[0](path)
|
||||||
|
}
|
||||||
|
|
||||||
return os.Remove(path)
|
return os.Remove(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveDir remove the path directory.
|
||||||
|
// Play: todo
|
||||||
|
func RemoveDir(path string, onDelete ...func(path string)) error {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
return fmt.Errorf("%s is not a directory", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
var callback func(string)
|
||||||
|
if len(onDelete) > 0 {
|
||||||
|
callback = onDelete[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
||||||
|
if err == nil && callback != nil {
|
||||||
|
callback(p)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.RemoveAll(path)
|
||||||
|
}
|
||||||
|
|
||||||
// CopyFile copy src file to dest file.
|
// CopyFile copy src file to dest file.
|
||||||
// Play: https://go.dev/play/p/Jg9AMJMLrJi
|
// Play: https://go.dev/play/p/Jg9AMJMLrJi
|
||||||
func CopyFile(srcPath string, dstPath string) error {
|
func CopyFile(srcPath string, dstPath string) error {
|
||||||
|
|||||||
@@ -85,14 +85,37 @@ func TestIsDir(t *testing.T) {
|
|||||||
|
|
||||||
func TestRemoveFile(t *testing.T) {
|
func TestRemoveFile(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestRemoveFile")
|
assert := internal.NewAssert(t, "TestRemoveFile")
|
||||||
|
|
||||||
f := "./text.txt"
|
f := "./text.txt"
|
||||||
if !IsExist(f) {
|
CreateFile(f)
|
||||||
CreateFile(f)
|
err := RemoveFile(f)
|
||||||
err := RemoveFile(f)
|
assert.IsNil(err)
|
||||||
assert.IsNil(err)
|
}
|
||||||
|
|
||||||
|
func TestRemoveDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRemoveDir")
|
||||||
|
|
||||||
|
err := os.MkdirAll("./tempdir/a/b", 0755)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var deletedPaths []string
|
||||||
|
|
||||||
|
err = RemoveDir("./tempdir", func(p string) {
|
||||||
|
deletedPaths = append(deletedPaths, p)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal([]string{"./tempdir", "tempdir/a", "tempdir/a/b"}, deletedPaths)
|
||||||
|
assert.Equal(false, IsExist("./tempdir"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyFile(t *testing.T) {
|
func TestCopyFile(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user