-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
281 lines (250 loc) · 7.62 KB
/
main.go
File metadata and controls
281 lines (250 loc) · 7.62 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
// ShouldSkipDir determines if a directory should be skipped based on .gitignore patterns.
func shouldSkipDir(relPath string, patterns []string) bool {
// Esxplicitly skip node_modules
if strings.Contains(relPath, "lib") || strings.Contains(relPath, "node_modules")|| strings.Contains(relPath, "vender") || strings.Contains(relPath, "vender") || strings.Contains(relPath, "tmp") || strings.Contains(relPath, "log") {
return true
}
for _, pattern := range patterns {
if strings.HasSuffix(pattern, "/") || strings.HasPrefix(pattern, "/") {
dirPattern := strings.TrimSuffix(pattern, "/")
dirPattern = strings.TrimPrefix(dirPattern, "/")
if match, _ := filepath.Match(dirPattern, relPath); match {
return true
}
} else {
if match, _ := filepath.Match(pattern, relPath); match {
return true
}
}
}
return false
}
// shouldSkipFile determines if a file should be skipped based on .gitignore patterns.
func shouldSkipFile(relPath string, name string, patterns []string) bool {
// Skip all files in the bin/directory
if strings.HasPrefix(relPath, "bin/") {
return true
}
// Skip specific SQLite database files
if name == "development.sqlite3" {
return true
}
for _, pattern := range patterns {
if !strings.HasSuffix(pattern, "/") || strings.HasPrefix(pattern, "/"){
if strings.Contains(pattern, "/") {
if match, _ := filepath.Match(pattern, relPath); match {
return true
}
} else {
if match, _ := filepath.Match(pattern, name); match {
return true
}
}
}
}
return false
}
func checkIfGitOrFilePath(input string) (repoURL string, branch string, isRepo bool) {
if strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "git@") {
parts := strings.Split(input, "@")
if strings.HasPrefix(input, "git@") {
// SSH format: git@github.com:user/repo.git@branch
if len(parts) > 2 {
repoURL = strings.Join(parts[:2], "@")
branch = parts[2]
} else {
repoURL = input
}
} else {
// HTTPS format: https://github.com/user/repo.git
if len(parts) > 1 {
repoURL = parts[0]
branch = parts[1]
} else {
repoURL = input
}
}
return repoURL, branch, true
}
return "", "", false
}
func cloneRepo(repoURL, branch string) (string, error) {
tempDir, err := os.MkdirTemp("", "go-reader-cli-*")
if err != nil {
return "", fmt.Errorf("failed to create temp directory: %v", err)
}
CloneOptions := &git.CloneOptions{
URL: repoURL,
}
if branch != "" {
CloneOptions.ReferenceName = plumbing.ReferenceName("refs/heads/" + branch)
CloneOptions.SingleBranch = true
}
_, err = git.PlainClone(tempDir, false, CloneOptions)
if err != nil {
os.RemoveAll(tempDir)
return "", fmt.Errorf("failed to clone repository: %v", err)
}
return tempDir, nil
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: go run . <directory-path> <output-md-file>\n go run . <git repo url> <output-md-filename>")
return
}
dirPath := os.Args[1]
outputFile := os.Args[2]
// Check if the dirPath is a git repository
repoURL, branch, isRepo := checkIfGitOrFilePath(dirPath)
if isRepo {
tempDir, err := cloneRepo(repoURL, branch)
if err != nil {
fmt.Printf("Error Cloning repo - '%s': %s\n", repoURL, err)
return
}
dirPath = tempDir
}
// Check if the directory exists
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
fmt.Printf("Directory '%s' does not exist.\n", dirPath)
return
}
// Open or create the output .md file
file, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
fmt.Printf("Failed to open or create the output file: %v\n", err)
return
}
defer file.Close()
// Extensions to skip (images and config files)
skipExtensions := map[string]struct{} {
".jpg": {},
".jpeg": {},
".png": {},
".gif": {},
".bmp": {},
"tiff": {},
".svg": {},
".mp3": {},
".mp4": {},
// ".config": {},
// ".config.ts": {},
".config.mjs": {},
".ini": {},
".yaml": {},
".yml": {},
".toml": {},
".ico": {},
".sqlite3": {},
".db": {},
// ".json": {},
}
// Read .gitigone if it exists
patterns := []string{}
gitignorePath := filepath.Join(dirPath, ".gitignore")
if _, err := os.Stat(gitignorePath); err == nil {
content, err := os.ReadFile(gitignorePath)
if err == nil {
lines := strings.Split(string(content), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
patterns = append(patterns, line)
}
}
}
}
// Walk through the directory
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Compute relative path
relPath, err := filepath.Rel(dirPath, path)
if err != nil {
fmt.Printf("Failed to get the relative path for %s: %v\n", path, err)
relPath = path
}
// Skip directories matching .gitignore patterns or starting with a dot
if info.IsDir() {
if (path != dirPath && strings.HasPrefix(info.Name(), ".")) || shouldSkipDir(relPath, patterns) {
return filepath.SkipDir
}
return nil
}
// Skip files matching .gitignore patterns, starting with a .dot, in bin/, or with skipped extensions
if shouldSkipFile(relPath, info.Name(), patterns) || strings.HasPrefix(info.Name(), ".") {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if _, ok := skipExtensions[ext]; ok {
return nil
}
// Read the file content
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file '%s': %v", path, err)
}
// Write the file path as a header using relative Path
header := fmt.Sprintf("\n# %s\n", relPath)
if _, err := file.WriteString(header); err != nil {
return fmt.Errorf("failed to write header for file '%s': %v", path, err)
}
// Write the opening backticks with appropirate language identifier
lang := getLanguageIdentifier(ext)
if _, err := file.WriteString(fmt.Sprintf("```%s\n", lang)); err != nil {
return fmt.Errorf("failed to write opening backticks for file '%s': %v", path, err)
}
// Write the file content
if _, err := file.Write(content); err != nil {
return fmt.Errorf("failed to write content for file '%s': %v", path, err)
}
// Write the closing backticks
if _, err := file.WriteString("\n```\n"); err != nil {
return fmt.Errorf("failed to write closing backticks for file '%s': %v", path, err)
}
fmt.Printf("Processed file: %s\n", path)
return nil
})
if err != nil {
fmt.Printf("Error processing files: %v\n", err)
return
}
fmt.Println("All files processed successfully and appended to the markdown file.")
}
// getLanguageIdentifier returns the appropriate Markdown code block language base on the file extentions
func getLanguageIdentifier(ext string) string {
switch strings.ToLower(ext) {
case ".rb":
return "ruby"
case ".js":
return "javascript"
case ".scss":
return "scss"
case ".html", "erb":
return "html"
case ".md":
return "markdown"
case ".yml", ".yaml":
return "yaml"
case ".rs":
return "rust"
case ".go":
return "go"
case ".py":
return "python"
case ".sol":
return "solidity"
default:
return ""
}
}