PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
613 B

package Helper
import (
"errors"
"os/exec"
"strconv"
)
func CheckRoot() error {
var (
cmd *exec.Cmd
output []byte
i int
e error
)
// TODO Make cross platform
cmd = exec.Command("id", "-u")
output, e = cmd.Output()
if e != nil {
return e
}
// output has trailing \n
// need to remove the \n
// otherwise it will cause error for strconv.Atoi
// log.Println(output[:len(output)-1])
// 0 = root, 501 = non-root user
i, e = strconv.Atoi(string(output[:len(output)-1]))
if e != nil {
return e
}
if i != 0 {
return errors.New("Please run as root")
}
return nil
}