From b91dfb3eee892511124c6dcbd8d9e19ce458274d Mon Sep 17 00:00:00 2001 From: weidong Date: Wed, 1 Jan 2025 18:02:27 +0800 Subject: [PATCH] dev --- main.go | 87 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/main.go b/main.go index cabbb8b..8076920 100644 --- a/main.go +++ b/main.go @@ -1,56 +1,67 @@ package main import ( - "bytes" "fmt" "os" "path/filepath" + "strings" ) func main() { - paths := getFiles(".") + var paths []string + rootDir, _ := os.Getwd() + paths, _ = GetAllFile(rootDir, paths, []string{".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"}) - for _, path := range paths { - // 读取文本格式的文件内容,自动替换“fonts.googleapis.com”为“fonts.loli.net”,“fonts.gstatic.com”为“fonts.loli.net” - content, err := os.ReadFile(path) - if err != nil { - fmt.Println("Error reading file:", err) - continue - } - content = bytes.ReplaceAll(content, []byte("fonts.googleapis.com"), []byte("fonts.loli.net")) - content = bytes.ReplaceAll(content, []byte("fonts.gstatic.com"), []byte("fonts.loli.net")) - os.WriteFile(path, content, 0644) + for _, fp := range paths { + fmt.Println(fp) } } -// 遍历当前目录以及子目录下所有文件,返回[]string绝对路径 -func getFiles(rootPath string) []string { - var paths []string - - var walkDir func(dirPath string) error - walkDir = func(dirPath string) error { - files, err := os.ReadDir(dirPath) - if err != nil { - return err - } - - for _, file := range files { - fullPath := filepath.Join(dirPath, file.Name()) - if file.IsDir() { - if err := walkDir(fullPath); err != nil { - return err - } - } else { - paths = append(paths, fullPath) +func GetAllFile(pathname string, s []string, filter []string) ([]string, error) { + rd, err := os.ReadDir(pathname) + if err != nil { + fmt.Println("read dir fail:", err) + return s, err + } + for _, fi := range rd { + // 检查文件名是否匹配任何一个过滤模式 + matched := false + for _, item := range filter { + exists, err := filepath.Match(item, fi.Name()) + if err != nil { + continue + } + if exists { + matched = true + break } } - return nil - } - if err := walkDir(rootPath); err != nil { - fmt.Println("Error walking directory:", err) - return nil - } + if matched { + continue + } - return paths + if fi.IsDir() { + fullDir := pathname + "/" + fi.Name() + s, err = GetAllFile(fullDir, s, filter) + if err != nil { + fmt.Println("read dir fail:", err) + return s, err + } + } else { + fullName := pathname + "/" + fi.Name() + s = append(s, fullName) + } + } + return s, nil +} + +func In(target string, array []string) bool { + target = strings.Trim(target, "") + for _, v := range array { + if strings.Trim(v, "") == target { + return true + } + } + return false }