ReplaceInFilePartial applies edits incrementally up to the first failure. Returns the results for each edit and writes the partially modified content.
(filePath string, edits []EditSpec)
| 369 | // ReplaceInFilePartial applies edits incrementally up to the first failure. |
| 370 | // Returns the results for each edit and writes the partially modified content. |
| 371 | func ReplaceInFilePartial(filePath string, edits []EditSpec) ([]EditResult, error) { |
| 372 | fileInfo, err := os.Stat(filePath) |
| 373 | if err != nil { |
| 374 | return nil, fmt.Errorf("failed to stat file: %w", err) |
| 375 | } |
| 376 | |
| 377 | if !fileInfo.Mode().IsRegular() { |
| 378 | return nil, fmt.Errorf("not a regular file: %s", filePath) |
| 379 | } |
| 380 | |
| 381 | if fileInfo.Size() > MaxEditFileSize { |
| 382 | return nil, fmt.Errorf("file too large for editing: %d bytes (max: %d)", fileInfo.Size(), MaxEditFileSize) |
| 383 | } |
| 384 | |
| 385 | contents, err := os.ReadFile(filePath) |
| 386 | if err != nil { |
| 387 | return nil, fmt.Errorf("failed to read file: %w", err) |
| 388 | } |
| 389 | |
| 390 | modifiedContents, results := ApplyEditsPartial(contents, edits) |
| 391 | |
| 392 | if err := os.WriteFile(filePath, modifiedContents, fileInfo.Mode()); err != nil { |
| 393 | return nil, fmt.Errorf("failed to write file: %w", err) |
| 394 | } |
| 395 | |
| 396 | return results, nil |
| 397 | } |
no test coverage detected