init commit.
This commit is contained in:
commit
e2856f1074
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# empty-dir
|
||||||
|
|
||||||
|
clean empty dirs.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go env -w GOPROXY=https://proxy.golang.com.cn,direct
|
||||||
|
go env -w GOPRIVATE=git.licsber.site
|
||||||
|
git config --global url."ssh://git@git.m.licsber.site/".insteadOf "https://git.licsber.site/"
|
||||||
|
|
||||||
|
go install git.licsber.site/go/empty-dir@latest
|
||||||
|
```
|
44
empty-dir.go
Normal file
44
empty-dir.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func emptyDir(p string) {
|
||||||
|
files, err := os.ReadDir(p)
|
||||||
|
if err != nil && strings.Contains(err.Error(), "Access is denied") {
|
||||||
|
remindErr(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
PanicErr(err)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
remainFiles, e := os.ReadDir(p)
|
||||||
|
PanicErr(e)
|
||||||
|
|
||||||
|
if len(remainFiles) == 0 {
|
||||||
|
e = Rm(p)
|
||||||
|
remindErr(e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filePath := filepath.Join(p, file.Name())
|
||||||
|
if !file.IsDir() {
|
||||||
|
if strings.HasPrefix(file.Name(), "._") && IsMacMetaFile(filePath) {
|
||||||
|
err = Rm(filePath)
|
||||||
|
remindErr(err)
|
||||||
|
} else if file.Name() == ".DS_Store" {
|
||||||
|
err = Rm(filePath)
|
||||||
|
remindErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
emptyDir(filePath)
|
||||||
|
}
|
||||||
|
}
|
23
main.go
Normal file
23
main.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
_startTime := time.Now()
|
||||||
|
defer func() {
|
||||||
|
fmt.Print("Time cost: ")
|
||||||
|
fmt.Println(time.Now().Sub(_startTime))
|
||||||
|
}()
|
||||||
|
|
||||||
|
p, fi := PathOrPWD()
|
||||||
|
if !fi.IsDir() {
|
||||||
|
fmt.Println(p)
|
||||||
|
fmt.Println("empty-dir: Arg must be dir path.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
emptyDir(p)
|
||||||
|
}
|
33
util_cli_path.go
Normal file
33
util_cli_path.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PathOrPWD() (p string, fi os.FileInfo) {
|
||||||
|
if len(os.Args) != 1 {
|
||||||
|
p = strings.Join(os.Args[1:], " ")
|
||||||
|
} else {
|
||||||
|
pwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
panic("Check if $PWD exist?")
|
||||||
|
}
|
||||||
|
p = pwd
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := filepath.Abs(p)
|
||||||
|
if err != nil {
|
||||||
|
panic("Path not exist: " + p)
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err = os.Stat(p)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Working path: " + p)
|
||||||
|
return
|
||||||
|
}
|
16
util_err.go
Normal file
16
util_err.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func remindErr(err error) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Print("Error: ")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PanicErr(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
22
util_macos.go
Normal file
22
util_macos.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const macMetaStr1 = "com.apple.quarantine"
|
||||||
|
const macMetaStr2 = "This resource fork intentionally left blank"
|
||||||
|
|
||||||
|
func IsMacMetaFile(p string) bool {
|
||||||
|
b, err := os.ReadFile(p)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(string(b), macMetaStr1) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Contains(string(b), macMetaStr2)
|
||||||
|
}
|
11
util_rm.go
Normal file
11
util_rm.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Rm(p string) error {
|
||||||
|
fmt.Println("Remove: " + p)
|
||||||
|
return os.Remove(p)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user