Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
fix(format): make regex for command search ungreedy and reset value o…
Browse files Browse the repository at this point in the history
…n each instance

regex for sed command search was greedy and match multiple markers if on the same line, change regex to not be greedy

Closes #110, #111

Signed-off-by: Jeff Davis <[email protected]>
  • Loading branch information
JefeDavis authored Jul 27, 2021
1 parent 47932aa commit 0bf6213
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/actions/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func doFormat(s string, values ...interface{}) string {
}
}

re := regexp.MustCompile(`(?P<marker>%[vklfh])(?P<format>{(?P<command>.*)})?`)
re := regexp.MustCompile(`(?P<marker>%[vklfh])(?P<format>{(?P<command>.*?)})?`)

matches := re.FindAllStringSubmatch(s, -1)

Expand All @@ -84,16 +84,18 @@ func doFormat(s string, values ...interface{}) string {
}

if result["marker"] != "" {
marker := valueMap[result["marker"]]

if result["command"] != "" {
var err error

valueMap[result["marker"]], err = processSedCommand(result["command"], valueMap[result["marker"]])
marker, err = processSedCommand(result["command"], valueMap[result["marker"]])
if err != nil {
log.Warningf("Skipping additional format on [%s] due to invalid sed exp [%s], %s", s, result["command"], err)
}
}

s = strings.Replace(s, match[0], valueMap[result["marker"]], 1)
s = strings.Replace(s, match[0], marker, 1)
}
}

Expand Down

0 comments on commit 0bf6213

Please sign in to comment.