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