-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesearch.go
124 lines (106 loc) · 3.02 KB
/
filesearch.go
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
package filesearch
import (
"bytes"
"os"
"path/filepath"
"strings"
gitignore "github.com/sabhiram/go-gitignore"
)
func loadGitIgnorePatterns() (*gitignore.GitIgnore, error) {
gitIgnorePath := ".gitignore"
if _, err := os.Stat(gitIgnorePath); os.IsNotExist(err) {
return nil, nil // No .gitignore file, so no patterns to load.
}
patterns, err := os.ReadFile(gitIgnorePath)
if err != nil {
return nil, err
}
return gitignore.CompileIgnoreLines(strings.Split(string(patterns), "\n")...), nil
}
func isExcludedDir(path string, excludes []string, ignoreObject *gitignore.GitIgnore) bool {
dirName := filepath.Base(path)
for _, exclude := range excludes {
if matched, err := filepath.Match(exclude, dirName); err == nil && matched {
return true
}
}
if ignoreObject != nil && ignoreObject.MatchesPath(path) {
return true
}
return false
}
func isExcludedFile(path string, extensions, excludes []string, ignoreObject *gitignore.GitIgnore) bool {
fileName := filepath.Base(path)
if len(extensions) > 0 && !hasValidExtension(path, extensions) {
return true
}
for _, exclude := range excludes {
if matched, err := filepath.Match(exclude, fileName); err == nil && matched {
return true
}
}
if ignoreObject != nil && ignoreObject.MatchesPath(path) {
return true
}
return false
}
func hasValidExtension(path string, extensions []string) bool {
for _, ext := range extensions {
if strings.HasSuffix(path, ext) {
return true
}
}
return false
}
func walkFileFunc(files *[]string, extensions, excludes []string, ignoreObject *gitignore.GitIgnore) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if isExcludedDir(path, excludes, ignoreObject) {
return filepath.SkipDir
}
} else if !isExcludedFile(path, extensions, excludes, ignoreObject) {
*files = append(*files, path)
}
return nil
}
}
func findFiles(extensions, excludes []string) ([]string, error) {
var files []string
ignoreObject, err := loadGitIgnorePatterns()
if err != nil {
return nil, err
}
excludes = append(excludes, ".gitignore", ".git", "LICENSE")
err = filepath.Walk(".", walkFileFunc(&files, extensions, excludes, ignoreObject))
if err != nil {
return nil, err
}
return files, nil
}
func concatenateFiles(files []string) (string, error) {
var buffer bytes.Buffer
for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
return "", err
}
buffer.WriteString("### filename: " + file + "\n")
buffer.Write(content)
buffer.WriteString("\n\n")
}
return buffer.String(), nil
}
func SearchAndConcatenateFiles(extensions, excludes []string) ([]string, string, error) {
files, err := findFiles(extensions, excludes)
if err != nil {
return []string{}, "", err
}
concatenatedContent, err := concatenateFiles(files)
if err != nil {
return []string{}, "", err
}
return files, concatenatedContent, nil
}