dev
This commit is contained in:
parent
f8372c7021
commit
b91dfb3eee
79
main.go
79
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)
|
||||
for _, fp := range paths {
|
||||
fmt.Println(fp)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
if exists {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历当前目录以及子目录下所有文件,返回[]string绝对路径
|
||||
func getFiles(rootPath string) []string {
|
||||
var paths []string
|
||||
if matched {
|
||||
continue
|
||||
}
|
||||
|
||||
var walkDir func(dirPath string) error
|
||||
walkDir = func(dirPath string) error {
|
||||
files, err := os.ReadDir(dirPath)
|
||||
if fi.IsDir() {
|
||||
fullDir := pathname + "/" + fi.Name()
|
||||
s, err = GetAllFile(fullDir, s, filter)
|
||||
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
|
||||
fmt.Println("read dir fail:", err)
|
||||
return s, err
|
||||
}
|
||||
} else {
|
||||
paths = append(paths, fullPath)
|
||||
fullName := pathname + "/" + fi.Name()
|
||||
s = append(s, fullName)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := walkDir(rootPath); err != nil {
|
||||
fmt.Println("Error walking directory:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return paths
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue