@@ -9,6 +9,11 @@ import (
99 "github.com/sudo-bmitch/version-bump/internal/config"
1010)
1111
12+ const (
13+ regexpArgRE = "regexp"
14+ regexpVersion = "Version"
15+ )
16+
1217type reScan struct {
1318 action * action.Action
1419 conf config.Scan
@@ -21,12 +26,12 @@ type reScan struct {
2126
2227func newREScan (conf config.Scan , srcRdr io.ReadCloser , a * action.Action , filename string ) (Scan , error ) {
2328 // validate config, extract and compile regexp
24- if _ , ok := conf .Args ["regexp" ]; ! ok {
29+ if _ , ok := conf .Args [regexpArgRE ]; ! ok {
2530 return nil , fmt .Errorf ("scan regexp arg is missing for %s" , conf .Name )
2631 }
27- re , err := regexp .Compile ("(?m)" + conf .Args ["regexp" ])
32+ re , err := regexp .Compile ("(?m)" + conf .Args [regexpArgRE ])
2833 if err != nil {
29- return nil , fmt .Errorf ("scan regexp does not compile for %s: %s: %w" , conf .Name , conf .Args ["regexp" ], err )
34+ return nil , fmt .Errorf ("scan regexp does not compile for %s: %s: %w" , conf .Name , conf .Args [regexpArgRE ], err )
3035 }
3136 // extract index of each subexp
3237 subNames := re .SubexpNames ()
@@ -35,8 +40,8 @@ func newREScan(conf config.Scan, srcRdr io.ReadCloser, a *action.Action, filenam
3540 nameInd [name ] = i
3641 }
3742 // verify regexp contains a "Version" match
38- if _ , ok := nameInd ["Version" ]; ! ok {
39- return nil , fmt .Errorf ("scan regexp is missing Version submatch (i.e. \" (?P<Version>\\ d+)\" ) for %s: %s" , conf .Name , conf .Args ["regexp" ])
43+ if _ , ok := nameInd [regexpVersion ]; ! ok {
44+ return nil , fmt .Errorf ("scan regexp is missing Version submatch (i.e. \" (?P<Version>\\ d+)\" ) for %s: %s" , conf .Name , conf .Args [regexpArgRE ])
4045 }
4146
4247 // create pipe
@@ -80,20 +85,19 @@ func (r *reScan) handlePipe(pw *io.PipeWriter) {
8085 for name , i := range r .nameInd {
8186 i1 , i2 := i * 2 , (i * 2 )+ 1
8287 if i2 >= len (matchIndexes ) {
83- // skip if not enough entries in match
84- // TODO: should this fail/error
85- continue
88+ pw .CloseWithError (fmt .Errorf ("regexp matches did not match compiled named field list (%d >= %d): %s" , i2 , len (matchIndexes ), r .conf .Args [regexpArgRE ]))
89+ return
8690 }
8791 regexpMatches [name ] = string (b [matchIndexes [i1 ]:matchIndexes [i2 ]])
8892 }
8993 matchData .ScanMatch = regexpMatches
90- change , newVer , err := r .action .HandleMatch (r .filename , r .conf .Name , r .conf .Source , regexpMatches ["Version" ], matchData )
94+ change , newVer , err := r .action .HandleMatch (r .filename , r .conf .Name , r .conf .Source , regexpMatches [regexpVersion ], matchData )
9195 if err != nil {
9296 pw .CloseWithError (err )
9397 return
9498 }
9599 // write up to version field
96- verI1 := r .nameInd ["Version" ] * 2
100+ verI1 := r .nameInd [regexpVersion ] * 2
97101 verI2 := verI1 + 1
98102 if lastIndex < matchIndexes [verI1 ] {
99103 _ , err = pw .Write (b [lastIndex :matchIndexes [verI1 ]])
@@ -103,6 +107,10 @@ func (r *reScan) handlePipe(pw *io.PipeWriter) {
103107 }
104108 lastIndex = matchIndexes [verI1 ]
105109 }
110+ if lastIndex > matchIndexes [verI1 ] {
111+ pw .CloseWithError (fmt .Errorf ("regexp match went backwards in the stream (%d > %d): %s" , lastIndex , matchIndexes [verI1 ], r .conf .Args [regexpArgRE ]))
112+ return
113+ }
106114 // write changed version
107115 if change {
108116 _ , err = pw .Write ([]byte (newVer ))
0 commit comments