Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (112 loc) · 2.75 KB
/
Copy pathmain.go
File metadata and controls
131 lines (112 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
var (
excludeDirs = flag.String("exclude-dirs", "node_modules,.git", "逗号分隔的目录列表,排除这些目录")
excludeFiles = flag.String("exclude-files", "package-lock.json", "逗号分隔的文件列表,排除这些文件")
showContent = flag.Bool("show-content", true, "是否显示源码文件的内容")
)
func printDirectoryStructure(path string, prefix string) {
entries, err := os.ReadDir(path)
if err != nil {
fmt.Println("读取目录错误:", err)
return
}
for i, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
if shouldExclude(fullPath) {
continue
}
isLastEntry := i == len(entries)-1
newPrefix := prefix
if isLastEntry {
fmt.Print(prefix, "└── ")
newPrefix += " "
} else {
fmt.Print(prefix, "├── ")
newPrefix += "│ "
}
fmt.Println(entry.Name())
if entry.IsDir() {
newPath := filepath.Join(path, entry.Name())
printDirectoryStructure(newPath, newPrefix)
}
}
}
func findSourceFiles(root string) []string {
var sourceFiles []string
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("访问路径错误:", path, err)
return nil
}
if shouldExclude(path) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if !info.IsDir() {
ext := filepath.Ext(path)
if isSourceCodeFile(ext) {
sourceFiles = append(sourceFiles, path)
}
}
return nil
})
return sourceFiles
}
func shouldExclude(path string) bool {
for _, dir := range strings.Split(*excludeDirs, ",") {
if strings.Contains(path, filepath.Join(dir)) {
return true
}
}
for _, file := range strings.Split(*excludeFiles, ",") {
if filepath.Base(path) == file {
return true
}
}
return false
}
func isSourceCodeFile(ext string) bool {
sourceExtensions := []string{
".js", ".go", ".py", ".php", ".html", ".css", ".json",
".c", ".cpp", ".h", ".hpp", ".java", ".rb", ".sh", ".pl",
".swift", ".kt", ".rs", ".scala", ".cs", ".ts", ".jsx", ".tsx",
".vue", ".scss", ".less", ".md", ".yml", ".yaml", ".toml",
}
for _, sourceExt := range sourceExtensions {
if ext == sourceExt {
return true
}
}
return false
}
func main() {
flag.Parse()
root := "."
if len(flag.Args()) > 0 {
root = flag.Arg(0)
}
fmt.Println(root)
printDirectoryStructure(root, "")
if *showContent {
sourceFiles := findSourceFiles(root)
for _, file := range sourceFiles {
relPath, _ := filepath.Rel(root, file)
fmt.Println("\n文件:", relPath, "的内容是:")
content, err := os.ReadFile(file)
if err != nil {
fmt.Println("文件内容读取失败:", file, err)
continue
}
fmt.Println(string(content))
}
}
}