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
625 B

package Helper
import (
"errors"
"os/exec"
"strconv"
)
func CheckRoot() error {
var (
cmd *exec.Cmd
output []byte
i int
err error
)
// TODO Make cross platform
cmd = exec.Command("id", "-u")
output, err = cmd.Output()
if err != nil {
return err
}
// 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, err = strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
return err
}
if i != 0 {
return errors.New("please run as root")
}
return nil
}