lint_script.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # Includes
  3. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  4. source $HESTIA/conf/hestia.conf
  5. # Variables and arguments
  6. HESTIA="/usr/local/hestia"
  7. script=$1
  8. log=$2
  9. scroll=$3
  10. if [ "$DEBUG_MODE" = "no" ] || [ -z "$DEBUG_MODE" ]; then
  11. echo "ERROR: Developer mode is disabled."
  12. echo "Enable with v-change-sys-config-value DEBUG_MODE yes"
  13. exit 1
  14. fi
  15. if [ -z "$script" ]; then
  16. echo "ERROR: No script specified."
  17. echo "Usage: ./lint_script ~/path/to/bin/v-script-name LOG_OUTPUT"
  18. exit 1
  19. fi
  20. # Install shellcheck
  21. if [ -f /etc/redhat-release ]; then
  22. package_check=$(rpm -qi ShellCheck)
  23. if [ $? -eq 1 ]; then
  24. echo "[ * ] Updating DNF package cache..."
  25. dnf makecache -q > /dev/null 2>&1
  26. echo "[ * ] Installing ShellCheck code linter..."
  27. dnf install -q -y ShellCheck > /dev/null 2>&1
  28. fi
  29. else
  30. package_check=$(dpkg -l | grep shellcheck)
  31. if [ -z "$package_check" ]; then
  32. echo "[ * ] Updating APT package cache..."
  33. apt-get -qq update > /dev/null 2>&1
  34. echo "[ * ] Installing shellcheck code linter..."
  35. apt-get -qq install -y shellcheck > /dev/null 2>&1
  36. fi
  37. fi
  38. # Set debug path and ensure it exists
  39. DEBUG_PATH="$HOME/hst-debug/"
  40. if [ ! -d "$DEBUG_PATH" ]; then
  41. mkdir "$DEBUG_PATH"
  42. fi
  43. # Generate timestamp
  44. time_n_date=$(date +'%F %T')
  45. time_n_date=$(echo $time_n_date | sed "s|:||g" | sed "s| |_|g")
  46. # If logging specified, export shellcheck output to log
  47. if [ "$log" = "yes" ]; then
  48. shellcheck -x "$script" > "$DEBUG_PATH/${script}_$date-$time.log"
  49. else
  50. # Prompt user to scroll output from shellcheck
  51. if [ "$scroll" == "no" ] || [ -z "$scroll" ]; then
  52. clear
  53. shellcheck -x "$script"
  54. else
  55. clear
  56. shellcheck -x "$script" | less
  57. fi
  58. fi