ScanFiles scans specific files and returns a JSON representation
(files []string, instructions string)
| 133 | |
| 134 | // ScanFiles scans specific files and returns a JSON representation |
| 135 | func ScanFiles(files []string, instructions string) ([]byte, error) { |
| 136 | fileCount := 0 |
| 137 | dirSet := make(map[string]bool) |
| 138 | |
| 139 | // Create root directory item |
| 140 | rootItem := FileItem{ |
| 141 | Type: "directory", |
| 142 | Name: ".", |
| 143 | Contents: []FileItem{}, |
| 144 | } |
| 145 | |
| 146 | for _, filePath := range files { |
| 147 | // Skip directories |
| 148 | info, err := os.Stat(filePath) |
| 149 | if err != nil { |
| 150 | return nil, fmt.Errorf("error accessing file %s: %v", filePath, err) |
| 151 | } |
| 152 | if info.IsDir() { |
| 153 | continue |
| 154 | } |
| 155 | |
| 156 | // Track unique directories |
| 157 | dir := filepath.Dir(filePath) |
| 158 | if dir != "." { |
| 159 | dirSet[dir] = true |
| 160 | } |
| 161 | |
| 162 | fileCount++ |
| 163 | |
| 164 | // Read file content |
| 165 | content, err := os.ReadFile(filePath) |
| 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("error reading file %s: %v", filePath, err) |
| 168 | } |
| 169 | |
| 170 | // Clean path for consistent handling |
| 171 | cleanPath := filepath.Clean(filePath) |
| 172 | if strings.HasPrefix(cleanPath, "./") { |
| 173 | cleanPath = cleanPath[2:] |
| 174 | } |
| 175 | |
| 176 | // Add file to the structure |
| 177 | addFileToDirectory(&rootItem, cleanPath, string(content), ".") |
| 178 | } |
| 179 | |
| 180 | // Create final data structure |
| 181 | var data []any |
| 182 | data = append(data, rootItem) |
| 183 | |
| 184 | // Add report |
| 185 | reportItem := map[string]any{ |
| 186 | "type": "report", |
| 187 | "directories": len(dirSet) + 1, |
| 188 | "files": fileCount, |
| 189 | } |
| 190 | data = append(data, reportItem) |
| 191 | |
| 192 | // Add instructions |