b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(installer): run `chsh` with sudo if user has privileges

This fixes the error in Google Cloud Shell, where a password prompt
appears when running `chsh` but the user (hello) does not have a
password.

If ran with `sudo`, the `chsh` command happens without a password
prompt.

Diff

 1diff --git a/tools/install.sh b/tools/install.sh
 2index 5009bd586b60eaae495b526aae123fa64499018b..d3be1ace4570e476734355fc022dbcafb87868c0 100755
 3--- a/tools/install.sh
 4+++ b/tools/install.sh
 5@@ -317,7 +317,7 @@ EOF
 6     "$YELLOW" "$RESET"
 7   read -r opt
 8   case $opt in
 9-    y*|Y*|"") echo "Changing the shell..." ;;
10+    y*|Y*|"") ;;
11     n*|N*) echo "Shell change skipped."; return ;;
12     *) echo "Invalid choice. Shell change skipped."; return ;;
13   esac
14@@ -355,11 +355,20 @@ EOF
15   if [ -n "$SHELL" ]; then
16     echo "$SHELL" > ~/.shell.pre-oh-my-zsh
17   else
18-    grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
19+    grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
20   fi
21 
22-  # Actually change the default shell to zsh
23-  if ! chsh -s "$zsh"; then
24+  echo "Changing your shell to $zsh..."
25+
26+  # Check if user has sudo privileges and run `chsh` or `sudo chsh`
27+  if LANG= sudo -l -U "$USER" 2>/dev/null | grep -q "is not allowed to run"; then
28+    chsh -s "$zsh" "$USER"          # run chsh normally
29+  else
30+    sudo -k chsh -s "$zsh" "$USER"  # -k forces the password prompt
31+  fi
32+
33+  # Check if the shell change was successful
34+  if [ $? -ne 0 ]; then
35     fmt_error "chsh command unsuccessful. Change your default shell manually."
36   else
37     export SHELL="$zsh"