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
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
}