ScanFiles scans specific files and returns a JSON representation
(files []string, instructions string)
| 126 | |
| 127 | // ScanFiles scans specific files and returns a JSON representation |
| 128 | func ScanFiles(files []string, instructions string) ([]byte, error) { |
| 129 | fileCount := 0 |
| 130 | dirSet := make(map[string]bool) |
| 131 | |
| 132 | rootItem := FileItem{ |
| 133 | Type: "directory", |
| 134 | Name: ".", |
| 135 | Contents: []FileItem{}, |
| 136 | } |
| 137 | |
| 138 | for _, filePath := range files { |
| 139 | info, err := os.Stat(filePath) |
| 140 | if err != nil { |
| 141 | return nil, fmt.Errorf("error accessing file %s: %v", filePath, err) |
| 142 | } |
| 143 | if info.IsDir() { |
| 144 | continue |
| 145 | } |
| 146 | |
| 147 | dir := filepath.Dir(filePath) |
| 148 | if dir != "." { |
| 149 | dirSet[dir] = true |
| 150 | } |
| 151 | |
| 152 | fileCount++ |
| 153 | |
| 154 | content, err := os.ReadFile(filePath) |
| 155 | if err != nil { |
| 156 | return nil, fmt.Errorf("error reading file %s: %v", filePath, err) |
| 157 | } |
| 158 | |
| 159 | cleanPath := filepath.Clean(filePath) |
| 160 | if strings.HasPrefix(cleanPath, "./") { |
| 161 | cleanPath = cleanPath[2:] |
| 162 | } |
| 163 | |
| 164 | addFileToDirectory(&rootItem, cleanPath, string(content), ".") |
| 165 | } |
| 166 | |
| 167 | var data []any |
| 168 | data = append(data, rootItem) |
| 169 | |
| 170 | reportItem := map[string]any{ |
| 171 | "type": "report", |
| 172 | "directories": len(dirSet) + 1, |
| 173 | "files": fileCount, |
| 174 | } |
| 175 | data = append(data, reportItem) |
| 176 | |
| 177 | instructionsItem := map[string]any{ |
| 178 | "type": "instructions", |
| 179 | "name": "code_change_instructions", |
| 180 | "details": instructions, |
| 181 | } |
| 182 | data = append(data, instructionsItem) |
| 183 | |
| 184 | return json.MarshalIndent(data, "", " ") |
| 185 | } |