This commit is contained in:
weidong 2025-01-01 18:02:27 +08:00
parent f8372c7021
commit b91dfb3eee
1 changed files with 49 additions and 38 deletions

87
main.go
View File

@ -1,56 +1,67 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func main() { 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 { for _, fp := range paths {
// 读取文本格式的文件内容自动替换“fonts.googleapis.com”为“fonts.loli.net”“fonts.gstatic.com”为“fonts.loli.net” fmt.Println(fp)
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)
} }
} }
// 遍历当前目录以及子目录下所有文件,返回[]string绝对路径 func GetAllFile(pathname string, s []string, filter []string) ([]string, error) {
func getFiles(rootPath string) []string { rd, err := os.ReadDir(pathname)
var paths []string if err != nil {
fmt.Println("read dir fail:", err)
var walkDir func(dirPath string) error return s, err
walkDir = func(dirPath string) error { }
files, err := os.ReadDir(dirPath) for _, fi := range rd {
if err != nil { // 检查文件名是否匹配任何一个过滤模式
return err matched := false
} for _, item := range filter {
exists, err := filepath.Match(item, fi.Name())
for _, file := range files { if err != nil {
fullPath := filepath.Join(dirPath, file.Name()) continue
if file.IsDir() { }
if err := walkDir(fullPath); err != nil { if exists {
return err matched = true
} break
} else {
paths = append(paths, fullPath)
} }
} }
return nil
}
if err := walkDir(rootPath); err != nil { if matched {
fmt.Println("Error walking directory:", err) continue
return nil }
}
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
} }