lint_script.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. package_check=$(dpkg -l | grep shellcheck)
  22. if [ -z "$package_check" ]; then
  23. echo "[ * ] Updating APT package cache..."
  24. apt-get -qq update > /dev/null 2>&1
  25. echo "[ * ] Installing shellcheck code linter..."
  26. apt-get -qq install -y shellcheck > /dev/null 2>&1
  27. fi
  28. # Set debug path and ensure it exists
  29. DEBUG_PATH="$HOME/hst-debug/"
  30. if [ ! -d "$DEBUG_PATH" ]; then
  31. mkdir "$DEBUG_PATH"
  32. fi
  33. # Generate timestamp
  34. time_n_date=$(date +'%F %T')
  35. time_n_date=$(echo $time_n_date | sed "s|:||g" | sed "s| |_|g")
  36. # If logging specified, export shellcheck output to log
  37. if [ "$log" = "yes" ]; then
  38. shellcheck -x "$script" > "$DEBUG_PATH/${script}_$date-$time.log"
  39. else
  40. # Prompt user to scroll output from shellcheck
  41. if [ "$scroll" == "no" ] || [ -z "$scroll" ]; then
  42. clear
  43. shellcheck -x "$script"
  44. else
  45. clear
  46. shellcheck -x "$script" | less
  47. fi
  48. fi