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
|
|
}
|