|
18 | 18 | package eventwatcher |
19 | 19 |
|
20 | 20 | import ( |
| 21 | + "bytes" |
21 | 22 | "context" |
22 | 23 | "errors" |
23 | 24 | "fmt" |
@@ -789,9 +790,17 @@ func convertStr(value interface{}) (out string, err error) { |
789 | 790 | return |
790 | 791 | } |
791 | 792 |
|
792 | | -// modifyText returns a new text replacing all matches of the given regex with the newValue. |
793 | | -// The only first capturing group enclosed by `()` will be replaced. |
794 | | -// True as a second returned value means it's already up-to-date. |
| 793 | +// modifyText returns a modified text of the file contents by replacing the first capturing group |
| 794 | +// of all matches of the provided regular expression with the specified newValue. |
| 795 | +// The replacement is applied only to the part of each match that corresponds to the first capturing |
| 796 | +// group (the portion enclosed in the first pair of parentheses `()`). |
| 797 | +// |
| 798 | +// It returns the updated content, a boolean indicating whether the file was already up-to-date, |
| 799 | +// and an error if any issue occurs during reading, regular expression parsing, or matching. |
| 800 | +// |
| 801 | +// If no matches are found, an error is returned. |
| 802 | +// If all first capturing groups already equal newValue, the original content is returned, |
| 803 | +// the boolean is true, and no changes are applied. |
795 | 804 | func modifyText(path, regexText, newValue string) ([]byte, bool, error) { |
796 | 805 | content, err := os.ReadFile(path) |
797 | 806 | if err != nil { |
@@ -819,17 +828,32 @@ func modifyText(path, regexText, newValue string) ([]byte, bool, error) { |
819 | 828 | if firstGroup == "" { |
820 | 829 | return nil, false, fmt.Errorf("capturing group not found in the given regex") |
821 | 830 | } |
822 | | - subRegex, err := pool.Get(firstGroup) |
823 | | - if err != nil { |
824 | | - return nil, false, fmt.Errorf("failed to compile the first capturing group: %w", err) |
825 | | - } |
826 | 831 |
|
827 | 832 | var touched, outDated bool |
828 | 833 | newText := regex.ReplaceAllFunc(content, func(match []byte) []byte { |
829 | 834 | touched = true |
830 | | - outDated = string(subRegex.Find(match)) != newValue |
831 | | - // Return text replacing the only first capturing group with the newValue. |
832 | | - return subRegex.ReplaceAll(match, []byte(newValue)) |
| 835 | + submatches := regex.FindSubmatchIndex(match) |
| 836 | + |
| 837 | + if len(submatches) < 4 { |
| 838 | + return match |
| 839 | + } |
| 840 | + |
| 841 | + groupStart, groupEnd := submatches[2], submatches[3] |
| 842 | + |
| 843 | + // no update on the value |
| 844 | + if string(match[groupStart:groupEnd]) == newValue { |
| 845 | + return match |
| 846 | + } |
| 847 | + |
| 848 | + outDated = true |
| 849 | + |
| 850 | + var buf bytes.Buffer |
| 851 | + |
| 852 | + buf.Write(match[:groupStart]) |
| 853 | + buf.WriteString(newValue) |
| 854 | + buf.Write(match[groupEnd:]) |
| 855 | + |
| 856 | + return buf.Bytes() |
833 | 857 | }) |
834 | 858 | if !touched { |
835 | 859 | return nil, false, fmt.Errorf("the content of %s doesn't match %s", path, regexText) |
|
0 commit comments