@@ -88,33 +88,73 @@ func UpdateYAMLFile(cfg VersionFileConfig, currentVersion, newVersion string) er
8888 return nil
8989}
9090
91+ // replacementRule defines a pattern-replacement pair for surgical YAML value replacement.
92+ // Each rule targets a specific quote style or value format in YAML files.
93+ type replacementRule struct {
94+ // name describes what this rule handles (for debugging/documentation)
95+ name string
96+ // pattern returns the regex pattern to match for the given old value
97+ pattern func (oldValue string ) string
98+ // replacement returns the replacement string for the given new value
99+ replacement func (newValue string ) string
100+ }
101+
102+ // replacementRules defines all the rules for surgical YAML value replacement.
103+ // Rules are tried in order; the first matching rule is applied.
104+ var replacementRules = []replacementRule {
105+ {
106+ // Handles double-quoted values: key: "value"
107+ name : "double-quoted" ,
108+ pattern : func (oldValue string ) string {
109+ return fmt .Sprintf (`"(%s)"` , regexp .QuoteMeta (oldValue ))
110+ },
111+ replacement : func (newValue string ) string {
112+ return fmt .Sprintf (`"%s"` , newValue )
113+ },
114+ },
115+ {
116+ // Handles single-quoted values: key: 'value'
117+ name : "single-quoted" ,
118+ pattern : func (oldValue string ) string {
119+ return fmt .Sprintf (`'(%s)'` , regexp .QuoteMeta (oldValue ))
120+ },
121+ replacement : func (newValue string ) string {
122+ return fmt .Sprintf (`'%s'` , newValue )
123+ },
124+ },
125+ {
126+ // Handles unquoted values at end of line: key: value\n
127+ name : "unquoted-eol" ,
128+ pattern : func (oldValue string ) string {
129+ return fmt .Sprintf (`: (%s)(\s*)$` , regexp .QuoteMeta (oldValue ))
130+ },
131+ replacement : func (newValue string ) string {
132+ return fmt .Sprintf (`: %s$2` , newValue )
133+ },
134+ },
135+ {
136+ // Handles unquoted values followed by inline comment: key: value # comment
137+ name : "unquoted-with-comment" ,
138+ pattern : func (oldValue string ) string {
139+ return fmt .Sprintf (`: (%s)(\s*#)` , regexp .QuoteMeta (oldValue ))
140+ },
141+ replacement : func (newValue string ) string {
142+ return fmt .Sprintf (`: %s$2` , newValue )
143+ },
144+ },
145+ }
146+
91147// surgicalReplace performs a targeted replacement of a YAML value while preserving
92148// the original formatting (quotes, whitespace, etc.)
93149func surgicalReplace (data []byte , oldValue , newValue string ) ([]byte , error ) {
94150 content := string (data )
95151
96- // Try different quote styles that might wrap the value
97- patterns := []string {
98- // Double quoted: key: "value"
99- fmt .Sprintf (`"(%s)"` , regexp .QuoteMeta (oldValue )),
100- // Single quoted: key: 'value'
101- fmt .Sprintf (`'(%s)'` , regexp .QuoteMeta (oldValue )),
102- // Unquoted after colon: key: value
103- fmt .Sprintf (`: (%s)(\s*)$` , regexp .QuoteMeta (oldValue )),
104- fmt .Sprintf (`: (%s)(\s*#)` , regexp .QuoteMeta (oldValue )),
105- }
106-
107- replacements := []string {
108- fmt .Sprintf (`"%s"` , newValue ),
109- fmt .Sprintf (`'%s'` , newValue ),
110- fmt .Sprintf (`: %s$2` , newValue ),
111- fmt .Sprintf (`: %s$2` , newValue ),
112- }
113-
114- for i , pattern := range patterns {
152+ // Try each replacement rule in order; use the first one that matches
153+ for _ , rule := range replacementRules {
154+ pattern := rule .pattern (oldValue )
115155 re := regexp .MustCompile (`(?m)` + pattern )
116156 if re .MatchString (content ) {
117- result := re .ReplaceAllString (content , replacements [ i ] )
157+ result := re .ReplaceAllString (content , rule . replacement ( newValue ) )
118158 return []byte (result ), nil
119159 }
120160 }
0 commit comments