MCPcopy
hub / github.com/wavetermdev/waveterm / ReplaceInFile

Function ReplaceInFile

pkg/util/fileutil/fileutil.go:338–367  ·  view source on GitHub ↗
(filePath string, edits []EditSpec)

Source from the content-addressed store, hash-verified

336}
337
338func ReplaceInFile(filePath string, edits []EditSpec) error {
339 fileInfo, err := os.Stat(filePath)
340 if err != nil {
341 return fmt.Errorf("failed to stat file: %w", err)
342 }
343
344 if !fileInfo.Mode().IsRegular() {
345 return fmt.Errorf("not a regular file: %s", filePath)
346 }
347
348 if fileInfo.Size() > MaxEditFileSize {
349 return fmt.Errorf("file too large for editing: %d bytes (max: %d)", fileInfo.Size(), MaxEditFileSize)
350 }
351
352 contents, err := os.ReadFile(filePath)
353 if err != nil {
354 return fmt.Errorf("failed to read file: %w", err)
355 }
356
357 modifiedContents, err := ApplyEdits(contents, edits)
358 if err != nil {
359 return err
360 }
361
362 if err := os.WriteFile(filePath, modifiedContents, fileInfo.Mode()); err != nil {
363 return fmt.Errorf("failed to write file: %w", err)
364 }
365
366 return nil
367}
368
369// ReplaceInFilePartial applies edits incrementally up to the first failure.
370// Returns the results for each edit and writes the partially modified content.

Callers 2

ReplaceInAppFileFunction · 0.92
editTextFileCallbackFunction · 0.92

Calls 6

ApplyEditsFunction · 0.85
StatMethod · 0.80
ModeMethod · 0.80
ReadFileMethod · 0.80
WriteFileMethod · 0.80
SizeMethod · 0.45

Tested by

no test coverage detected