Skip to content

Commit

Permalink
fix: set owner for created files
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Feb 4, 2024
1 parent 6aac9c3 commit 834d978
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
13 changes: 10 additions & 3 deletions internal/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ func EnsureDirExists(dir string) error {
}

err = os.MkdirAll(dir, 0744)
if os.IsPermission(err) {
return MaybeRunWithSudo(fmt.Sprintf("mkdir -p %s", dir))
if err == nil {
return nil
} else if !os.IsPermission(err) {
return err
}

err = MaybeRunWithSudo(fmt.Sprintf("mkdir -p %s", dir))
if err != nil {
return err
}

return err
return chown(dir, rootUser, rootUser)
}
1 change: 0 additions & 1 deletion internal/filecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func chown(file, user, group string) error {
if isHomePath {
return nil
}

user = rootUser
group = rootUser
}
Expand Down
16 changes: 16 additions & 0 deletions internal/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"fmt"
"os"
"strings"

marecmd "github.com/femnad/mare/cmd"
Expand Down Expand Up @@ -61,3 +63,17 @@ func MaybeRunWithSudoForPath(cmdStr, path string) error {
_, err = marecmd.RunFormatError(cmd)
return err
}

func Move(src, dst string) error {
if IsHomePath(dst) {
return os.Rename(src, dst)
}

mv := fmt.Sprintf("mv %s %s", src, dst)
err := MaybeRunWithSudoForPath(mv, dst)
if err != nil {
return err
}

return chown(src, rootUser, rootUser)
}
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.5"
version = "0.31.6"
)

type args struct {
Expand Down
3 changes: 1 addition & 2 deletions provision/ensurelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func ensureLine(config entity.Config, line entity.LineInFile) error {
return err
}

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

0 comments on commit 834d978

Please sign in to comment.