ScanDirectory scans a directory and returns a JSON representation of its structure
(rootDir string, maxDepth int, instructions string, ignoreList []string)
| 33 | |
| 34 | // ScanDirectory scans a directory and returns a JSON representation of its structure |
| 35 | func ScanDirectory(rootDir string, maxDepth int, instructions string, ignoreList []string) ([]byte, error) { |
| 36 | // Count totals for report |
| 37 | dirCount := 1 |
| 38 | fileCount := 0 |
| 39 | |
| 40 | // Create root directory item |
| 41 | rootItem := FileItem{ |
| 42 | Type: "directory", |
| 43 | Name: rootDir, |
| 44 | Contents: []FileItem{}, |
| 45 | } |
| 46 | |
| 47 | // Walk through the directory |
| 48 | err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | |
| 53 | // Skip .git directory |
| 54 | if strings.Contains(path, ".git") { |
| 55 | if info.IsDir() { |
| 56 | return filepath.SkipDir |
| 57 | } |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | // Check if path matches any ignore pattern |
| 62 | relPath, err := filepath.Rel(rootDir, path) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | for _, pattern := range ignoreList { |
| 68 | if strings.Contains(relPath, pattern) { |
| 69 | if info.IsDir() { |
| 70 | return filepath.SkipDir |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if relPath == "." { |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | depth := len(strings.Split(relPath, string(filepath.Separator))) |
| 81 | if depth > maxDepth { |
| 82 | if info.IsDir() { |
| 83 | return filepath.SkipDir |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // Create directory structure |
| 89 | if info.IsDir() { |
| 90 | dirCount++ |
| 91 | } else { |
| 92 | fileCount++ |