-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
257 lines (203 loc) · 6.73 KB
/
Copy pathmain.go
File metadata and controls
257 lines (203 loc) · 6.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/denormal/go-gitignore"
"github.com/yargevad/filepathx"
"github.com/num30/config"
"github.com/pkoukk/tiktoken-go"
)
var ignore gitignore.GitIgnore
type Config struct {
Model string `flag:"model" envvar:"MODEL" default:"gpt-4"`
OutputFile string `flag:"output" envvar:"FILE" default:"file-for-ai.txt"`
IgnoreGitIgnore bool `flag:"ignore-gitignore" envvar:"IGNORE_GITIGNORE" default:"false"`
ProcessNonText bool `flag:"process-non-text" envvar:"PROCESS_NON_TEXT" default:"false"`
}
var conf Config
func main() {
err := config.NewConfReader("file-for-ai").Read(&conf)
if err != nil {
panic(err)
}
if len(os.Args) < 2 {
fmt.Println("Error: Directory path or glob pattern is required.")
fmt.Println("Usage: file-for-ai <directory|pattern> --output [output file]")
fmt.Println("\nExamples:")
fmt.Println(" file-for-ai /path/to/directory")
fmt.Println(" file-for-ai './*.txt'")
fmt.Println(" file-for-ai /path/to/directory --output custom-output.txt")
fmt.Println("\nFor more information, visit https://github.com/num30/file-for-ai?tab=readme-ov-file#file-for-ai")
os.Exit(1)
}
inputPath := os.Args[1]
outputFileName := conf.OutputFile
outputFile, err := os.Create(outputFileName)
if err != nil {
fmt.Println("Error creating output file:", err)
os.Exit(1)
}
defer outputFile.Close()
encoding := conf.Model
tkm, err := tiktoken.EncodingForModel(encoding)
if err != nil {
fmt.Println("Error getting encoding for model:", err)
os.Exit(1)
}
tokens := 0
fmt.Println("Merging files:")
if isDirectory(inputPath) {
if !conf.IgnoreGitIgnore {
fmt.Print("Filtering files using .gitignore... ")
ignore, err = gitignore.NewRepository(inputPath)
if err != nil {
fmt.Println("Error creating gitignore repository:", err)
os.Exit(1)
}
}
err = filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("Error accessing path:", path, err)
return err
}
return processFile(inputPath, path, info, outputFile, tkm, &tokens, outputFileName)
})
} else {
files, err := filepathx.Glob(inputPath)
if err != nil {
fmt.Println("Error parsing glob pattern:", err)
os.Exit(1)
}
for _, path := range files {
info, err := os.Stat(path)
if err != nil {
fmt.Println("Error accessing file:", path, err)
continue
}
err = processFile(filepath.Dir(path), path, info, outputFile, tkm, &tokens, outputFileName)
if err != nil {
fmt.Println("Error processing file:", path, err)
continue
}
}
}
fmt.Println()
if err != nil {
fmt.Println("Error walking through the directory or processing pattern:", err)
return
}
fmt.Printf("Files merged successfully into %s\n", outputFileName)
fmt.Printf("Total tokens for model %s: %s\n", conf.Model, formatIntNumber(tokens))
}
func isDirectory(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
func processFile(basePath, path string, info os.FileInfo, outputFile *os.File, tkm *tiktoken.Tiktoken, tokens *int, outputFileName string) error {
if info.Name() == outputFileName {
return nil
}
relativePath, err := filepath.Rel(basePath, path)
if err != nil {
fmt.Println("Error processing relative path:", path, err)
return err
}
if !info.IsDir() && !isGitIgnored(relativePath, info.IsDir()) && extensionBasedFilter(path) && !strings.HasPrefix(path, ".") {
fileContents, err := os.ReadFile(path)
if err != nil {
fmt.Println("Error reading file:", path, err)
return err
}
fmt.Println(relativePath)
*tokens += len(tkm.Encode(string(fileContents), nil, nil))
separator := fmt.Sprintf("\n\n>>>>>> %s <<<<<<\n\n", relativePath)
if _, err := outputFile.WriteString(separator); err != nil {
fmt.Println("Error writing separator to output file:", err)
return err
}
if _, err := outputFile.Write(fileContents); err != nil {
fmt.Println("Error writing file contents to output file:", err)
return err
}
}
return nil
}
func formatIntNumber(n int) string {
s := fmt.Sprintf("%d", n)
if len(s) <= 3 {
return s
}
var result string
for i, c := range s {
if i != 0 && (len(s)-i)%3 == 0 {
result += ","
}
result += string(c)
}
return result
}
func isGitIgnored(path string, isDir bool) bool {
if ignore == nil {
return false
}
m := ignore.Relative(path, isDir)
if m != nil {
return m.Ignore()
}
return false
}
func extensionBasedFilter(path string) bool {
if conf.ProcessNonText {
return true
}
ext := strings.ToLower(filepath.Ext(path))
return !nonTextFileExtensions[ext]
}
// List of common non-text file extensions based on MIME types and practical file handling.
var nonTextFileExtensions = map[string]bool{
// Images
".png": true, ".jpg": true, ".jpeg": true, ".gif": true, ".bmp": true, ".tiff": true, ".svg": true, ".psd": true, ".ai": true, ".webp": true, ".heic": true,
// Audio
".mp3": true, ".wav": true, ".flac": true, ".aac": true, ".ogg": true, ".m4a": true, ".wma": true,
// Video
".mp4": true, ".avi": true, ".mkv": true, ".mov": true, ".flv": true, ".wmv": true, ".m4v": true, ".mpg": true, ".mpeg": true, ".h264": true,
// Compressed files
".zip": true, ".rar": true, ".7z": true, ".gz": true, ".tar": true, ".bz2": true, ".xz": true, ".tgz": true, ".zipx": true, ".iso": true,
// Executables and binaries
".exe": true, ".bin": true, ".dll": true, ".so": true, ".rpm": true, ".deb": true, ".dmg": true, ".bat": true, ".jar": true,
// Documents and PDFs
".pdf": true, ".doc": true, ".docx": true, ".ppt": true, ".pptx": true, ".xls": true, ".xlsx": true, ".odt": true, ".ods": true,
".odp": true, ".epub": true, ".mobi": true,
// 3D Models
".obj": true, ".stl": true, ".dae": true, ".blend": true,
// Database files
".sqlite": true, ".db": true, ".sql": true, ".mdb": true, ".accdb": true,
// Code and Script binaries
".pyc": true, ".class": true, ".o": true, ".a": true, ".dylib": true, ".lib": true,
// Other formats
".ps": true, ".eps": true, ".xps": true, ".swf": true, ".fla": true, // Adobe & Microsoft
".indd": true,
// Cryptographic files
".pem": true, ".key": true, ".cert": true, ".crt": true,
// Virtual Machine files
".vmdk": true, ".ovf": true, ".vdi": true,
// Game files
".pak": true, ".bsp": true, ".wad": true,
// Fonts
".ttf": true, ".otf": true, ".woff": true, ".woff2": true,
// Email
".pst": true, ".eml": true, ".msg": true,
// Disk Images
".img": true, ".vhdx": true,
// No extension (commonly used for binary files)
"": true, // representing no-extension files as potentially non-text
// Additional generic binary/data files
".dat": true,
// Specific project files
".xd": true, // Adobe XD
".sketch": true, // Sketch App
// CAD Files
".dwg": true, ".dxf": true,
}