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

  1. package Helper
  2. import (
  3. "errors"
  4. "os/exec"
  5. "strconv"
  6. )
  7. func CheckRoot() error {
  8. var (
  9. cmd *exec.Cmd
  10. output []byte
  11. i int
  12. err error
  13. )
  14. // TODO Make cross platform
  15. cmd = exec.Command("id", "-u")
  16. output, err = cmd.Output()
  17. if err != nil {
  18. return err
  19. }
  20. // output has trailing \n
  21. // need to remove the \n
  22. // otherwise it will cause error for strconv.Atoi
  23. // log.Println(output[:len(output)-1])
  24. // 0 = root, 501 = non-root user
  25. i, err = strconv.Atoi(string(output[:len(output)-1]))
  26. if err != nil {
  27. return err
  28. }
  29. if i != 0 {
  30. return errors.New("please run as root")
  31. }
  32. return nil
  33. }