Skip to content

Commit

Permalink
fix: don't replace file with empty lines
Browse files Browse the repository at this point in the history
d'oh!
  • Loading branch information
femnad committed Feb 6, 2024
1 parent 0c532f9 commit c212580
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
4 changes: 1 addition & 3 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"fmt"
"strings"

marecmd "github.com/femnad/mare/cmd"
)
Expand All @@ -13,8 +12,7 @@ func maybeWarnPasswordRequired(cmdStr string) {
return
}

cmdHead := strings.Split(cmdStr, " ")[0]
Log.Warningf("Sudo authentication required for escalating privileges to run command %s", cmdHead)
Log.Warningf("Sudo authentication required for escalating privileges to run command `%s`", cmdStr)
}

func needsSudoForPath(dst string) (bool, error) {
Expand Down
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.32.0"
version = "0.33.0"
)

type args struct {
Expand Down
26 changes: 12 additions & 14 deletions provision/ensurelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (result ensu

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

var absent bool
var found bool
var oldLine string
var newLine string
l := scanner.Text()

for _, needle := range replacements {
var regex *regexp.Regexp
absent = needle.Absent
Expand All @@ -103,29 +103,27 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (result ensu
}

if regex.MatchString(l) {
changed = true
if absent {
break
}
lineToWrite = newLine
found = true
l = newLine
break
}
} else if l == oldLine {
changed = true
lineToWrite = newLine
} else {
lineToWrite = l
found = true
l = newLine
break
}
}

if changed {
if found {
delete(replacements, oldLine)
changed = true
}

if absent && changed {
continue
}

_, err = tmpFile.WriteString(lineToWrite + "\n")
_, err = tmpFile.WriteString(l + "\n")
if err != nil {
return
}
Expand Down

0 comments on commit c212580

Please sign in to comment.