Skip to content

Commit

Permalink
fix: remove replacements they've been found
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Feb 5, 2024
1 parent 307bf60 commit b539417
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
version = "0.31.11"
version = "0.32.0"
)

type args struct {
Expand Down
26 changes: 20 additions & 6 deletions provision/ensurelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,27 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (result ensu
scanner := bufio.NewScanner(srcFile)
scanner.Split(bufio.ScanLines)

replacements := make(map[string]entity.Replacement)
for _, replacement := range line.Replace {
replacements[replacement.Old] = replacement
}

var changed bool
for scanner.Scan() {
var lineToWrite string
l := scanner.Text()

var absent bool
for _, needle := range line.Replace {
absent = needle.Absent
var oldLine string
var newLine string
for _, needle := range replacements {
var regex *regexp.Regexp
absent = needle.Absent
oldLine = needle.Old
newLine = needle.New

if needle.Regex {
regex, err = regexp.Compile(needle.Old)
regex, err = regexp.Compile(oldLine)
if err != nil {
return result, err
}
Expand All @@ -97,16 +107,20 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (result ensu
if absent {
break
}
lineToWrite = needle.New
lineToWrite = newLine
}
} else if l == needle.Old {
} else if l == oldLine {
changed = true
lineToWrite = needle.New
lineToWrite = newLine
} else {
lineToWrite = l
}
}

if changed {
delete(replacements, oldLine)
}

if absent && changed {
continue
}
Expand Down

0 comments on commit b539417

Please sign in to comment.