Advertisement
Saichovsky

shell-linters

Oct 17th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.24 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # This installs shellcheck and shfmt linters into your computer.
  3. # This script is shellcheck and "shfmt -i 2" compliant ;-)
  4. # Copyright (C) 2018 Saichovsky. All rights reserved
  5.  
  6. ARCH=$(uname -m)
  7. if [[ $ARCH =~ .*386.* ]]; then
  8.   ARCH=386
  9. elif [[ $ARCH == x86_64 ]]; then
  10.   ARCH=amd64
  11. else
  12.   echo "I only support 386 and amd64 platforms. Feel free to extend. Will exit now."
  13.   exit 4
  14. fi
  15.  
  16. what_os() {
  17.   echo "Please use a more decent OS." >&2
  18.   exit 1
  19. }
  20.  
  21. download() {
  22.   FNAME=$(echo "$1" | awk -F '/' '{print $NF}')
  23.   if command -v curl >/dev/null; then
  24.     curl -skLo "$FNAME" "$1" || (
  25.       echo "Failed to download $FNAME"
  26.       exit 2
  27.     )
  28.   elif command -v wget >/dev/null; then
  29.     wget -q -O "$FNAME" "$1" || (
  30.       echo "Failed to download $FNAME"
  31.       exit 2
  32.     )
  33.   else
  34.     echo "Couldn't find a suitable HTTP client"
  35.     exit 2
  36.   fi
  37. }
  38.  
  39. get_shfmt() {
  40.   CMD=
  41.   command -v curl >/dev/null && CMD="curl -sL" || CMD="wget -q -O -"
  42.   BINPATH=$($CMD "$1" | grep -oE '/mvdan/.*linux_'${ARCH} | head -1)
  43.   RV=$?
  44.  
  45.   [ $RV -eq 0 ] || (
  46.     echo "Failed to retrieve the shfmt URL"
  47.     exit 3
  48.   )
  49.   (download "https://github.com/${BINPATH}" &&
  50.     sudo install -m 0755 -o 0 -g 0 shfmt_v* /usr/bin/shfmt && rm -f shfmt_v* &&
  51.     echo "Installed shfmt successfully") ||
  52.     (
  53.       echo "Error downloading shfmt."
  54.       exit 3
  55.     )
  56. }
  57.  
  58. # Install shfmt and shellcheck
  59. printf 'Please wait while I install some dependencies... \n'
  60. if [ "$(uname)" = "Darwin" ]; then
  61.   command -v shfmt >/dev/null || brew install shfmt
  62.   command -v shellcheck >/dev/null || brew install shellcheck
  63.   echo "Done!"
  64. elif [ "$(uname)" = "Linux" ]; then
  65.   pushd /tmp || true
  66.   # Install shellcheck
  67.   if ! command -v shellcheck >/dev/null; then
  68.     download "https://storage.googleapis.com/shellcheck/shellcheck-stable.linux.x86_64.tar.xz"
  69.     tar -Jxvf shellcheck-stable.linux.x86_64.tar.xz >/dev/null 2>&1
  70.     sudo cp shellcheck-stable/shellcheck /usr/bin/ && rm -fr shellcheck-stable* &&
  71.       echo "Install shellcheck successfully"
  72.   fi
  73.   # Install shfmt
  74.   if ! command -v shfmt >/dev/null; then
  75.     get_shfmt "https://github.com/mvdan/sh/releases/latest/"
  76.   fi
  77.   popd >/dev/null || true
  78.   echo "Done!"
  79. else
  80.   what_os
  81. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement