95a06f3927a286db257dc99791b02caba757fe33

Author
Léa Gris <lea.gris@noiraude.net>
Committer
GitHub <noreply@github.com>
Date

Message

refactor(installer): fix static analysis warnings (#9529)

* refactor(install.sh): fix static analysis warnings

Clear all warnings and errors raised by shellcheck.net static analysis.

- Replace non-POSIX shell use of `$OSTYPE` by POSIX compliant check on `uname -o`.
- Move variables out of`printf` format string.
- Refactor/simplify string formatters for error and underline.
- Fix expansion of arguments to a single string `$*` rather than individual elements `$@` within the error and underline formatters.

* fix(uname): non-posix -o option

* fix(install.sh): non portable which

Replaced non-portable `which zsh` by portable `command -v zsh`

Diff

  1diff --git a/tools/install.sh b/tools/install.sh
  2index 953c68baf78692f7a8e07c6fe2d507a54da46510..d4ecb7afed2be061b85c907f25e824aace9fff13 100755
  3--- a/tools/install.sh
  4+++ b/tools/install.sh
  5@@ -57,15 +57,16 @@ command_exists() {
  6 }
  7 
  8 fmt_error() {
  9-  echo ${RED}"Error: $@"${RESET} >&2
 10+  printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2
 11 }
 12 
 13 fmt_underline() {
 14-  echo "$(printf '\033[4m')$@$(printf '\033[24m')"
 15+  printf '\033[4m%s\033[24m\n' "$*"
 16 }
 17 
 18 fmt_code() {
 19-  echo "\`$(printf '\033[38;5;247m')$@${RESET}\`"
 20+  # shellcheck disable=SC2016 # backtic in single-quote
 21+  printf '`\033[38;5;247m%s%s`\n' "$*" "$RESET"
 22 }
 23 
 24 setup_color() {
 25@@ -102,7 +103,8 @@ setup_ohmyzsh() {
 26     exit 1
 27   }
 28 
 29-  if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
 30+  ostype=$(uname)
 31+  if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then
 32     fmt_error "Windows/MSYS Git is not supported on Cygwin"
 33     fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
 34     exit 1
 35@@ -130,7 +132,7 @@ setup_zshrc() {
 36   OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
 37   if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
 38     # Skip this if the user doesn't want to replace an existing .zshrc
 39-    if [ $KEEP_ZSHRC = yes ]; then
 40+    if [ "$KEEP_ZSHRC" = yes ]; then
 41       echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
 42       return
 43     fi
 44@@ -162,7 +164,7 @@ export ZSH=\"$ZSH\"
 45 
 46 setup_shell() {
 47   # Skip setup if the user wants or stdin is closed (not running interactively).
 48-  if [ $CHSH = no ]; then
 49+  if [ "$CHSH" = no ]; then
 50     return
 51   fi
 52 
 53@@ -183,8 +185,9 @@ EOF
 54   echo "${BLUE}Time to change your default shell to zsh:${RESET}"
 55 
 56   # Prompt for user choice on changing the default login shell
 57-  printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
 58-  read opt
 59+  printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \
 60+    "$YELLOW" "$RESET"
 61+  read -r opt
 62   case $opt in
 63     y*|Y*|"") echo "Changing the shell..." ;;
 64     n*|N*) echo "Shell change skipped."; return ;;
 65@@ -211,7 +214,7 @@ EOF
 66     # Get the path to the right zsh binary
 67     # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
 68     # 2. If that fails, get a zsh path from the shells file, then check it actually exists
 69-    if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
 70+    if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then
 71       if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
 72         fmt_error "no zsh binary found or not present in '$shells_file'"
 73         fmt_error "change your default shell manually."
 74@@ -222,7 +225,7 @@ EOF
 75 
 76   # We're going to change the default shell, so back up the current one
 77   if [ -n "$SHELL" ]; then
 78-    echo $SHELL > ~/.shell.pre-oh-my-zsh
 79+    echo "$SHELL" > ~/.shell.pre-oh-my-zsh
 80   else
 81     grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
 82   fi
 83@@ -288,7 +291,7 @@ EOF
 84   setup_zshrc
 85   setup_shell
 86 
 87-  printf "$GREEN"
 88+  printf %s "$GREEN"
 89   cat <<'EOF'
 90          __                                     __
 91   ____  / /_     ____ ___  __  __   ____  _____/ /_
 92@@ -307,7 +310,7 @@ Before you scream Oh My Zsh! please look over the ~/.zshrc file to select plugin
 93 • Get stickers, shirts, coffee mugs and other swag: $(fmt_underline https://shop.planetargon.com/collections/oh-my-zsh)
 94 
 95 EOF
 96-  printf "$RESET"
 97+  printf %s "$RESET"
 98 
 99   if [ $RUNZSH = no ]; then
100     echo "${YELLOW}Run zsh to try it out.${RESET}"