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

71
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) }
}
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 { if err != nil {
fmt.Println("Error reading file:", err)
continue continue
} }
content = bytes.ReplaceAll(content, []byte("fonts.googleapis.com"), []byte("fonts.loli.net")) if exists {
content = bytes.ReplaceAll(content, []byte("fonts.gstatic.com"), []byte("fonts.loli.net")) matched = true
os.WriteFile(path, content, 0644) break
} }
} }
// 遍历当前目录以及子目录下所有文件,返回[]string绝对路径 if matched {
func getFiles(rootPath string) []string { continue
var paths []string }
var walkDir func(dirPath string) error if fi.IsDir() {
walkDir = func(dirPath string) error { fullDir := pathname + "/" + fi.Name()
files, err := os.ReadDir(dirPath) s, err = GetAllFile(fullDir, s, filter)
if err != nil { if err != nil {
return err fmt.Println("read dir fail:", err)
} return s, err
for _, file := range files {
fullPath := filepath.Join(dirPath, file.Name())
if file.IsDir() {
if err := walkDir(fullPath); err != nil {
return err
} }
} else { } else {
paths = append(paths, fullPath) fullName := pathname + "/" + fi.Name()
s = append(s, fullName)
} }
} }
return nil return s, nil
} }
if err := walkDir(rootPath); err != nil { func In(target string, array []string) bool {
fmt.Println("Error walking directory:", err) target = strings.Trim(target, "")
return nil for _, v := range array {
if strings.Trim(v, "") == target {
return true
} }
}
return paths return false
} }