Created
May 28, 2019 07:00
-
-
Save tuantranf/77a03ab2b58c2f3bf03ca3fbd776b647 to your computer and use it in GitHub Desktop.
check if user is root
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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