From f2567a5a8bee150e78f41542ac3aa7f4fced67e9 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 4 Nov 2023 11:06:03 +1000 Subject: [PATCH] Refactor --- entrypoint.sh | 304 +++++++++++++++++++++++--------------------------- 1 file changed, 142 insertions(+), 162 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index ce08240..95a8ce1 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,21 +2,14 @@ set -eu +# Declare some variables for the color codes NC='\033[0m' # No Color GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' RED='\033[0;31m' -npm i -g markdown-link-check@3.11.1 -echo "::group::Debug information" -npm -g list --depth=1 -echo "::endgroup::" - -declare -a FIND_CALL -declare -a COMMAND_DIRS COMMAND_FILES -declare -a COMMAND_FILES - +# Declare some variables for the options and arguments USE_QUIET_MODE="$1" USE_VERBOSE_MODE="$2" CONFIG_FILE="$3" @@ -24,189 +17,176 @@ FOLDER_PATH="$4" MAX_DEPTH="$5" CHECK_MODIFIED_FILES="$6" BASE_BRANCH="$7" -if [ -z "$8" ]; then - FILE_EXTENSION=".md" -else - FILE_EXTENSION="$8" -fi +FILE_EXTENSION="$8" FILE_PATH="$9" -if [ -f "$CONFIG_FILE" ]; then - echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}" -else - echo -e "${BLUE}Cannot find ${YELLOW}$CONFIG_FILE${NC}" - echo -e "${YELLOW}NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about" - echo -e "customizing markdown-link-check by using a configuration file.${NC}" -fi - -FOLDERS="" -FILES="" +# Declare some variables for the default values +DEFAULT_FILE_EXTENSION=".md" +DEFAULT_FOLDER_PATH="." +DEFAULT_MAX_DEPTH="-1" +DEFAULT_BASE_BRANCH="master" +DEFAULT_ERROR_FILE="error.txt" +DEFAULT_ERROR_CODE="113" -echo -e "${BLUE}USE_QUIET_MODE: $1${NC}" -echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}" -echo -e "${BLUE}FOLDER_PATH: $4${NC}" -echo -e "${BLUE}MAX_DEPTH: $5${NC}" -echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}" -echo -e "${BLUE}FILE_EXTENSION: $8${NC}" -echo -e "${BLUE}FILE_PATH: $9${NC}" - -handle_dirs () { +# Declare some arrays to store the directories and files to check +declare -a COMMAND_DIRS COMMAND_FILES - IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH" +# Install markdown-link-check globally +npm i -g markdown-link-check@3.11.2 - for index in "${!DIRLIST[@]}" - do - if [ ! -d "${DIRLIST[index]}" ]; then - echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}" - exit 2 - fi - COMMAND_DIRS+=("${DIRLIST[index]}") - done - FOLDERS="${COMMAND_DIRS[*]}" +# Show the global npm packages installed +echo "::group::Debug information" +npm -g list --depth=1 +echo "::endgroup::" +# A function to print a message with a color +print_message () { + local COLOR="$1" # Get the first argument as the color + local MESSAGE="$2" # Get the second argument as the message + echo -e "${COLOR}${MESSAGE}${NC}" # Print the message with the color and no color } -handle_files () { - - IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" - - for index in "${!FILELIST[@]}" - do - if [ ! -f "${FILELIST[index]}" ]; then - echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}" - exit 2 - fi - if [ "$index" == 0 ]; then - COMMAND_FILES+=("-wholename ${FILELIST[index]}") - else - COMMAND_FILES+=("-o -wholename ${FILELIST[index]}") - fi - done - FILES="${COMMAND_FILES[*]}" - +# A function to run the markdown-link-check command with the options +run_markdown_link_check () { + local FILE="$1" # Get the first argument as the file name + local OPTIONS=() # Declare an array to store the options + if [ -f "$CONFIG_FILE" ]; then # Check if the config file exists + OPTIONS+=('--config' "${CONFIG_FILE}") # Add the --config option to the options array + fi + if [ "$USE_QUIET_MODE" = "yes" ]; then # Check if the quiet mode is enabled + OPTIONS+=('-q') # Add the -q option to the options array + fi + if [ "$USE_VERBOSE_MODE" = "yes" ]; then # Check if the verbose mode is enabled + OPTIONS+=('-v') # Add the -v option to the options array + fi + markdown-link-check "${OPTIONS[@]}" "$FILE" &>> "$DEFAULT_ERROR_FILE" || true # Run the markdown-link-check command with the options and the file name, and append the output to the error file, ignore the exit code } -check_errors () { - - if [ -e error.txt ] ; then - if grep -q "ERROR:" error.txt; then - echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}" - cat error.txt - printf "\n" - echo -e "${YELLOW}=========================================================================${NC}" - exit 113 - else - echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}" - printf "\n" - echo -e "${GREEN}[✔] All links are good!${NC}" - printf "\n" - echo -e "${YELLOW}=========================================================================${NC}" - fi - else - echo -e "${GREEN}All good!${NC}" - fi - +# A function to parse and validate the arguments +parse_arguments () { + # Set the default values for the arguments + if [ -z "$FILE_EXTENSION" ]; then + FILE_EXTENSION="$DEFAULT_FILE_EXTENSION" + fi + if [ -z "$FOLDER_PATH" ]; then + FOLDER_PATH="$DEFAULT_FOLDER_PATH" + fi + if [ -z "$MAX_DEPTH" ]; then + MAX_DEPTH="$DEFAULT_MAX_DEPTH" + fi + if [ -z "$BASE_BRANCH" ]; then + BASE_BRANCH="$DEFAULT_BASE_BRANCH" + fi + # Print the arguments + print_message "$BLUE" "USE_QUIET_MODE: $USE_QUIET_MODE" + print_message "$BLUE" "USE_VERBOSE_MODE: $USE_VERBOSE_MODE" + print_message "$BLUE" "CONFIG_FILE: $CONFIG_FILE" + print_message "$BLUE" "FOLDER_PATH: $FOLDER_PATH" + print_message "$BLUE" "MAX_DEPTH: $MAX_DEPTH" + print_message "$BLUE" "CHECK_MODIFIED_FILES: $CHECK_MODIFIED_FILES" + print_message "$BLUE" "FILE_EXTENSION: $FILE_EXTENSION" + print_message "$BLUE" "FILE_PATH: $FILE_PATH" + # Check if the config file exists + if [ -f "$CONFIG_FILE" ]; then + print_message "$BLUE" "Using markdown-link-check configuration file: $CONFIG_FILE" + else + print_message "$BLUE" "Cannot find $CONFIG_FILE" + print_message "$YELLOW" "NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about customizing markdown-link-check by using a configuration file." + fi } -add_options () { - - if [ -f "$CONFIG_FILE" ]; then - FIND_CALL+=('--config' "${CONFIG_FILE}") - fi - - if [ "$USE_QUIET_MODE" = "yes" ]; then - FIND_CALL+=('-q') - fi - - if [ "$USE_VERBOSE_MODE" = "yes" ]; then - FIND_CALL+=('-v') - fi - +# A function to handle the directories +handle_dirs () { + IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH" # Split the folder path by comma and store it in an array + for index in "${!DIRLIST[@]}" + do + if [ ! -d "${DIRLIST[index]}" ]; then # Check if the directory exists + print_message "$RED" "ERROR [✖] Can't find the directory: ${DIRLIST[index]}" + exit 2 # Exit with error code 2 + fi + COMMAND_DIRS+=("${DIRLIST[index]}") # Add the directory to the command dirs array + done } -check_additional_files () { - - if [ -n "$FILES" ]; then - if [ "$MAX_DEPTH" -ne -1 ]; then - FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') - else - FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') - fi - - add_options - - FIND_CALL+=(';') - - set -x - "${FIND_CALL[@]}" &>> error.txt - set +x - - fi - +# A function to handle the files +handle_files () { + IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" # Split the file path by comma and store it in an array + for index in "${!FILELIST[@]}" + do + if [ ! -f "${FILELIST[index]}" ]; then # Check if the file exists + print_message "$RED" "ERROR [✖] Can't find the file: ${FILELIST[index]}" + exit 2 # Exit with error code 2 + fi + if [ "$index" == 0 ]; then + COMMAND_FILES+=("-wholename ${FILELIST[index]}") # Add the file name with -wholename option to the command files array + else + COMMAND_FILES+=("-o -wholename ${FILELIST[index]}") # Add the file name with -o and -wholename options to the command files array + fi + done } -if [ -z "$8" ]; then - FOLDERS="." -else - handle_dirs -fi - -if [ -n "$9" ]; then - handle_files -fi +# A function to compare the file extensions +compare_file_extensions () { + local FILE="$1" # Get the first argument as the file name + local EXTENSION="$2" # Get the second argument as the file extension + [ "${FILE##*.}" == "${EXTENSION#.}" ] # Compare the file extension with the argument, ignoring the dot +} -if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then +# A function to check and delete the error file +check_error_file () { + if [ -e "$DEFAULT_ERROR_FILE" ] ; then # Check if the error file exists + if grep -q "ERROR:" "$DEFAULT_ERROR_FILE"; then # Check if the error file contains any ERROR: lines + print_message "$YELLOW" "=========================> MARKDOWN LINK CHECK <=========================" + cat "$DEFAULT_ERROR_FILE" # Print the error file + printf "\n" + print_message "$YELLOW" "=========================================================================" + rm "$DEFAULT_ERROR_FILE" # Delete the error file + exit "$DEFAULT_ERROR_CODE" # Exit with the default error code + else + print_message "$YELLOW" "=========================> MARKDOWN LINK CHECK <=========================" + printf "\n" + print_message "$GREEN" "[✔] All links are good!" + printf "\n" + print_message "$YELLOW" "=========================================================================" + rm "$DEFAULT_ERROR_FILE" # Delete the error file + fi + else + print_message "$GREEN" "All good!" + fi +} - echo -e "${BLUE}BASE_BRANCH: $7${NC}" +# Parse and validate the arguments +parse_arguments - git config --global --add safe.directory '*' +# Handle the directories +handle_dirs - git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null - MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}") +# Handle the files +handle_files - if [ -z "$FOLDERS" ]; then - FOLDERS="." - fi +if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then # Check if the check modified files option is enabled - FIND_CALL=('markdown-link-check') + print_message "$BLUE" "BASE_BRANCH: $BASE_BRANCH" # Print the base branch name - add_options + git config --global --add safe.directory '*' # Add a global git config to allow any directory name - FOLDER_ARRAY=(${FOLDER_PATH//,/ }) - mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}") + git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null # Fetch the base branch from origin with depth 1 and discard the output + MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}") # Get the hash of the base branch - for i in "${FILE_ARRAY[@]}" - do - if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then - FIND_CALL+=("${i}") - COMMAND="${FIND_CALL[*]}" - $COMMAND &>> error.txt || true - unset 'FIND_CALL[${#FIND_CALL[@]}-1]' - fi - done + mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${COMMAND_DIRS[@]}") # Get the modified files from the git diff and store them in an array - check_additional_files + # Use xargs to run the markdown-link-check command on multiple files at once + printf '%s\n' "${FILE_ARRAY[@]}" | xargs -I {} bash -c 'compare_file_extensions "$1" "$2" && run_markdown_link_check "$1"' _ {} "$FILE_EXTENSION" - check_errors + # Check and delete the error file + check_error_file else - if [ "$5" -ne -1 ]; then - FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') - else - FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') - fi - - add_options - - FIND_CALL+=(';') - - set -x - "${FIND_CALL[@]}" &>> error.txt - set +x - - check_additional_files + # Use xargs to run the markdown-link-check command on multiple files at once + find "${COMMAND_DIRS[@]}" -name "*${FILE_EXTENSION}" -not -path './node_modules/*' -maxdepth "${MAX_DEPTH}" -print0 | xargs -0 -I {} bash -c 'run_markdown_link_check "$1"' _ {} - check_errors + # Check and delete the error file + check_error_file fi