1
0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-25 09:41:20 +08:00

refactor(installer): simplify user_can_sudo check

This commit is contained in:
Marc Cornellà 2022-01-11 19:53:50 +01:00
parent a0a949de56
commit c63ba17525
No known key found for this signature in database
GPG Key ID: 0314585E776A9C1B

View File

@ -64,18 +64,18 @@ user_can_sudo() {
# • without privilege: exits with error code 1 and prints the message: # • without privilege: exits with error code 1 and prints the message:
# Sorry, user <username> may not run sudo on <hostname> # Sorry, user <username> may not run sudo on <hostname>
# #
# 2. Pass `-S` to `sudo` to tell it to get the password from stdin # 2. Pass `-n` to `sudo` to tell it to not ask for a password. If the
# instead of from a tty, and pipe `true` to `sudo`, since it doesn't # password is not required, the command will finish with exit code 0.
# output anything. This will make sudo exit with error code 1 and print # If one is required, sudo will exit with error code 1 and print the
# the message: # message:
# sudo: no password was provided # sudo: a password is required
# #
# 3. Check for the words "may not run sudo" in the output to really tell # 3. Check for the words "may not run sudo" in the output to really tell
# whether the user has privileges or not. For that we have to make sure # whether the user has privileges or not. For that we have to make sure
# to run `sudo` in the default locale (with `LANG=`) so that the message # to run `sudo` in the default locale (with `LANG=`) so that the message
# stays consistent regardless of the user's locale. # stays consistent regardless of the user's locale.
# #
true | LANG= sudo -v -S 2>&1 | grep -q "may not run sudo" LANG= sudo -n -v 2>&1 | grep -q "may not run sudo"
} }
# The [ -t 1 ] check only works when the function is not called from # The [ -t 1 ] check only works when the function is not called from