Skip to content

Commit

Permalink
chore: consolidate dir creation and cmd running w optional sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 26, 2023
1 parent cfaa209 commit 238b0b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
12 changes: 3 additions & 9 deletions internal/filecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,17 @@ func chown(file, user, group string) error {
}

func ensureDir(dir string) error {
hasPerms, err := hasPerms(dir)
hasDirPerms, err := hasPerms(dir)
if err != nil {
return err
}

if hasPerms {
if hasDirPerms {
return os.MkdirAll(dir, 0o755)
}

Log.Warningf("escalating privileges to create directory %s", dir)
mkdirCmd := fmt.Sprintf("mkdir -p %s", dir)

isRoot, err := IsUserRoot()
if err != nil {
return err
}
return marecmd.RunNoOutput(marecmd.Input{Command: mkdirCmd, Sudo: !isRoot})
return MaybeRunWithSudo(mkdirCmd)
}

func getent(key, database string) (int, error) {
Expand Down
9 changes: 7 additions & 2 deletions internal/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package internal

import marecmd "github.com/femnad/mare/cmd"
import (
"strings"

marecmd "github.com/femnad/mare/cmd"
)

func maybeWarnPasswordRequired(cmdStr string) {
out, _ := marecmd.Run(marecmd.Input{Command: "sudo -Nnv"})
Expand All @@ -18,7 +22,8 @@ func MaybeRunWithSudo(cmdStr string) error {
}

if !isRoot {
maybeWarnPasswordRequired(cmdStr)
cmdHead := strings.Split(cmdStr, "/")[0]
maybeWarnPasswordRequired(cmdHead)
}

cmd := marecmd.Input{Command: cmdStr, Sudo: !isRoot}
Expand Down

0 comments on commit 238b0b4

Please sign in to comment.