ApplyEditsPartial applies edits incrementally, continuing until the first failure. Returns the modified content (potentially partially applied) and results for each edit.
(originalContent []byte, edits []EditSpec)
| 311 | // ApplyEditsPartial applies edits incrementally, continuing until the first failure. |
| 312 | // Returns the modified content (potentially partially applied) and results for each edit. |
| 313 | func ApplyEditsPartial(originalContent []byte, edits []EditSpec) ([]byte, []EditResult) { |
| 314 | modifiedContents := originalContent |
| 315 | results := make([]EditResult, len(edits)) |
| 316 | failed := false |
| 317 | |
| 318 | for i, edit := range edits { |
| 319 | if failed { |
| 320 | results[i].Desc = edit.Desc |
| 321 | if results[i].Desc == "" { |
| 322 | results[i].Desc = fmt.Sprintf("Edit %d", i+1) |
| 323 | } |
| 324 | results[i].Applied = false |
| 325 | results[i].Error = "previous edit failed" |
| 326 | continue |
| 327 | } |
| 328 | |
| 329 | modifiedContents, results[i] = applyEdit(modifiedContents, edit, i) |
| 330 | if !results[i].Applied { |
| 331 | failed = true |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return modifiedContents, results |
| 336 | } |
| 337 | |
| 338 | func ReplaceInFile(filePath string, edits []EditSpec) error { |
| 339 | fileInfo, err := os.Stat(filePath) |
no test coverage detected