Skip to content

Instantly share code, notes, and snippets.

@tuantranf
Created May 28, 2019 07:00
Show Gist options
  • Save tuantranf/77a03ab2b58c2f3bf03ca3fbd776b647 to your computer and use it in GitHub Desktop.
Save tuantranf/77a03ab2b58c2f3bf03ca3fbd776b647 to your computer and use it in GitHub Desktop.
check if user is root
package helpers
import (
"os/exec"
"strconv"
)
const WindowsOs = "windows"
const DarwinOs = "darwin"
const LinuxOs = "linux"
const MacOSX = "macosx"
const Debian = "debian"
func isRootUser() (isRoot bool, err error) {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
return false, 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
id, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
return false, err
}
return id == 0, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment