Skip to content

Commit 66da05c

Browse files
committed
review
1 parent a34ef52 commit 66da05c

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

pkg/util/fileops.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,25 @@ func CopyDir(src, dst string) error {
7575
}
7676

7777
// ModifyFile replaces all occurrences of oldStr with newStr in the file.
78-
// Returns an error if no replacements were made
78+
// Returns an error if the pattern is not found in the file
7979
func ModifyFile(path, oldStr, newStr string) error {
8080
content, err := os.ReadFile(path)
8181
if err != nil {
8282
return err
8383
}
8484

8585
original := string(content)
86+
87+
// Check if pattern exists in the file
88+
if !strings.Contains(original, oldStr) {
89+
return fmt.Errorf("pattern %q not found in file %s", oldStr, path)
90+
}
91+
8692
modified := strings.ReplaceAll(original, oldStr, newStr)
8793

88-
// Error if no replacements were made
94+
// Skip writing if the replacement is a no-op (oldStr == newStr or pattern doesn't change content)
8995
if modified == original {
90-
return fmt.Errorf("pattern %q not found in file %s", oldStr, path)
96+
return nil
9197
}
9298

9399
return os.WriteFile(path, []byte(modified), 0644)

0 commit comments

Comments
 (0)