Skip to content

Commit

Permalink
fix: set owner if the file is new
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Feb 4, 2024
1 parent 5c203b6 commit f8fd934
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
8 changes: 6 additions & 2 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func MaybeRunWithSudoForPath(cmdStr, path string) error {
return err
}

func Move(src, dst string) error {
func Move(src, dst string, setOwner bool) error {
if IsHomePath(dst) {
return os.Rename(src, dst)
}
Expand All @@ -75,5 +75,9 @@ func Move(src, dst string) error {
return err
}

return chown(src, rootUser, rootUser)
if setOwner {
return chown(dst, rootUser, rootUser)
}

return nil
}
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.7"
version = "0.31.8"
)

type args struct {
Expand Down
26 changes: 16 additions & 10 deletions provision/ensurelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ import (
)

var (
ensureFns = map[string]func(string, *os.File, entity.LineInFile) (bool, error){
ensureFns = map[string]func(string, *os.File, entity.LineInFile) (ensureResult, error){
"ensure": ensure,
"replace": replace,
}
)

func ensure(file string, tmpFile *os.File, line entity.LineInFile) (changed bool, err error) {
type ensureResult struct {
changed bool
new bool
}

func ensure(file string, tmpFile *os.File, line entity.LineInFile) (result ensureResult, err error) {
content := mapset.NewSet[string]()
for _, l := range line.Content {
content.Add(l)
Expand All @@ -46,7 +51,7 @@ func ensure(file string, tmpFile *os.File, line entity.LineInFile) (changed bool
}

if content.Cardinality() == 0 {
return false, nil
return
}

content.Each(func(l string) bool {
Expand All @@ -55,13 +60,13 @@ func ensure(file string, tmpFile *os.File, line entity.LineInFile) (changed bool
})

if err != nil {
return false, fmt.Errorf("error ensuring lines in file %s", file)
return result, fmt.Errorf("error ensuring lines in file %s", file)
}

return true, nil
return ensureResult{changed: true, new: newFile}, nil
}

func replace(file string, tmpFile *os.File, line entity.LineInFile) (changed bool, err error) {
func replace(file string, tmpFile *os.File, line entity.LineInFile) (result ensureResult, err error) {
srcFile, err := os.Open(file)
if err != nil {
return
Expand All @@ -82,6 +87,7 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (changed boo
replacements[replacement.Old] = replacement.New
}

var changed bool
for scanner.Scan() {
var lineToWrite string
l := scanner.Text()
Expand All @@ -106,7 +112,7 @@ func replace(file string, tmpFile *os.File, line entity.LineInFile) (changed boo
}
}

return changed, nil
return ensureResult{changed: changed}, nil
}

func ensureLine(config entity.Config, line entity.LineInFile) error {
Expand All @@ -126,13 +132,13 @@ func ensureLine(config entity.Config, line entity.LineInFile) error {
return fmt.Errorf("no method for %s'ing a line", line.Name)
}

changed, err := ensureFn(target, tmpFile, line)
result, err := ensureFn(target, tmpFile, line)
if err != nil {
return err
}

tmpPath := tmpFile.Name()
if !changed {
if !result.changed {
internal.Log.Debugf("Not modifying %s as no changes were found", target)
err = os.Remove(tmpPath)
if err != nil {
Expand All @@ -147,7 +153,7 @@ func ensureLine(config entity.Config, line entity.LineInFile) error {
return err
}

err = internal.Move(tmpPath, target)
err = internal.Move(tmpPath, target, result.new)
if err != nil {
return fmt.Errorf("error renaming %s to %s: %v", tmpPath, target, err)
}
Expand Down

0 comments on commit f8fd934

Please sign in to comment.