diff --git a/internal/fs.go b/internal/fs.go index b531baf..232f096 100644 --- a/internal/fs.go +++ b/internal/fs.go @@ -12,9 +12,12 @@ func ReplaceOnce(path string, re *regexp.Regexp, replace string) error { return err } orig := string(data) + if !re.MatchString(orig) { + return fmt.Errorf("pattern not found in %s", path) + } out := re.ReplaceAllString(orig, replace) if out == orig { - return fmt.Errorf("pattern not found in %s", path) + return nil } return os.WriteFile(path, []byte(out), 0644) } @@ -25,9 +28,12 @@ func ReplaceOnceFunc(path string, re *regexp.Regexp, fn func(string) string) err return err } orig := string(data) + if !re.MatchString(orig) { + return fmt.Errorf("pattern not found in %s", path) + } out := re.ReplaceAllStringFunc(orig, fn) if out == orig { - return fmt.Errorf("pattern not found in %s", path) + return nil } return os.WriteFile(path, []byte(out), 0644) }