mirror of
https://github.com/super-linter/super-linter.git
synced 2024-11-24 17:50:58 -05:00
528 lines
14 KiB
Bash
528 lines
14 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
################################################################################
|
||
|
########### Markup and Markdown Language Linter @AdmiralAwkbar #################
|
||
|
################################################################################
|
||
|
|
||
|
###########
|
||
|
# GLOBALS #
|
||
|
###########
|
||
|
YAML_LINTER_RULES='.automation/yaml-linter-rules.yml' # Path to the yaml lint rules
|
||
|
MD_LINTER_RULES='.automation/md-linter-rules.yml' # Path to the markdown lint rules
|
||
|
|
||
|
############
|
||
|
# Counters #
|
||
|
############
|
||
|
ERRORS_FOUND_YML=0 # Count of errors found
|
||
|
ERRORS_FOUND_JSON=0 # Count of errors found
|
||
|
ERRORS_FOUND_XML=0 # Count of errors found
|
||
|
ERRORS_FOUND_MD=0 # Count of errors found
|
||
|
|
||
|
################################################################################
|
||
|
########################## FUNCTIONS BELOW #####################################
|
||
|
################################################################################
|
||
|
################################################################################
|
||
|
#### Function Header ###########################################################
|
||
|
Header()
|
||
|
{
|
||
|
echo ""
|
||
|
echo "---------------------------------------------"
|
||
|
echo "---- Markup and Markdown Language Linter ----"
|
||
|
echo "---------------------------------------------"
|
||
|
echo ""
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function GetLinterRules ###################################################
|
||
|
GetLinterRules()
|
||
|
{
|
||
|
# Need to validate the rules files exist
|
||
|
|
||
|
#####################################
|
||
|
# Validate we have the linter rules #
|
||
|
#####################################
|
||
|
if [ ! -f "$YAML_LINTER_RULES" ]; then
|
||
|
echo "ERROR! Failed to find:[$YAML_LINTER_RULES] in root of code base!"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
#####################################
|
||
|
# Validate we have the linter rules #
|
||
|
#####################################
|
||
|
if [ ! -f "$MD_LINTER_RULES" ]; then
|
||
|
echo "ERROR! Failed to find:[$MD_LINTER_RULES] in root of code base!"
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function LintJsonFiles ####################################################
|
||
|
LintJsonFiles()
|
||
|
{
|
||
|
################
|
||
|
# print header #
|
||
|
################
|
||
|
echo ""
|
||
|
echo "--------------------------------"
|
||
|
echo "Linting JSON files..."
|
||
|
echo "--------------------------------"
|
||
|
echo ""
|
||
|
|
||
|
######################
|
||
|
# Name of the linter #
|
||
|
######################
|
||
|
LINTER_NAME="jsonlint-php"
|
||
|
|
||
|
#######################################
|
||
|
# Validate we have yamllint installed #
|
||
|
#######################################
|
||
|
# shellcheck disable=SC2230
|
||
|
VALIDATE_INSTALL_CMD=$(which "$LINTER_NAME" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
# Failed
|
||
|
echo "ERROR! Failed to find [$LINTER_NAME] in system!"
|
||
|
echo "ERROR:[$VALIDATE_INSTALL_CMD]"
|
||
|
exit 1
|
||
|
else
|
||
|
# Success
|
||
|
echo "Successfully found binary in system"
|
||
|
echo "Location:[$VALIDATE_INSTALL_CMD]"
|
||
|
fi
|
||
|
|
||
|
#################################
|
||
|
# Get list of all files to lint #
|
||
|
#################################
|
||
|
# shellcheck disable=SC2207
|
||
|
LIST_FILES=($(find . -type f -name "*.json" 2>&1))
|
||
|
|
||
|
##################
|
||
|
# Lint the files #
|
||
|
##################
|
||
|
for FILE in "${LIST_FILES[@]}"
|
||
|
do
|
||
|
#####################
|
||
|
# Get the file name #
|
||
|
#####################
|
||
|
FILE_NAME=$(basename "$FILE" 2>&1)
|
||
|
|
||
|
#######################################
|
||
|
# Make sure we dont lint node modules #
|
||
|
#######################################
|
||
|
if [[ $FILE == *"node_modules"* ]]; then
|
||
|
# This is a node modules file
|
||
|
continue
|
||
|
fi
|
||
|
|
||
|
##############
|
||
|
# File print #
|
||
|
##############
|
||
|
echo "---------------------------"
|
||
|
echo "File:[$FILE]"
|
||
|
|
||
|
################################
|
||
|
# Lint the file with the rules #
|
||
|
################################
|
||
|
LINT_CMD=$("$LINTER_NAME" "$FILE" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
#########
|
||
|
# Error #
|
||
|
#########
|
||
|
echo "ERROR! Found errors in [$LINTER_NAME] linter!"
|
||
|
echo "ERROR:[$LINT_CMD]"
|
||
|
# Increment error count
|
||
|
((ERRORS_FOUND_JSON++))
|
||
|
else
|
||
|
###########
|
||
|
# Success #
|
||
|
###########
|
||
|
echo " - File:[$FILE_NAME] was linted with [$LINTER_NAME] successfully"
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function LintYmlFiles #####################################################
|
||
|
LintYmlFiles()
|
||
|
{
|
||
|
################
|
||
|
# print header #
|
||
|
################
|
||
|
echo ""
|
||
|
echo "--------------------------------"
|
||
|
echo "Linting YAML files..."
|
||
|
echo "--------------------------------"
|
||
|
echo ""
|
||
|
|
||
|
######################
|
||
|
# Name of the linter #
|
||
|
######################
|
||
|
LINTER_NAME="yamllint"
|
||
|
|
||
|
#######################################
|
||
|
# Validate we have yamllint installed #
|
||
|
#######################################
|
||
|
# shellcheck disable=SC2230
|
||
|
VALIDATE_INSTALL_CMD=$(which "$LINTER_NAME" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
# Failed
|
||
|
echo "ERROR! Failed to find [$LINTER_NAME] in system!"
|
||
|
echo "ERROR:[$VALIDATE_INSTALL_CMD]"
|
||
|
exit 1
|
||
|
else
|
||
|
# Success
|
||
|
echo "Successfully found binary in system"
|
||
|
echo "Location:[$VALIDATE_INSTALL_CMD]"
|
||
|
fi
|
||
|
|
||
|
#################################
|
||
|
# Get list of all files to lint #
|
||
|
#################################
|
||
|
# shellcheck disable=SC2207
|
||
|
LIST_FILES=($(find . -type f \( -name "*.yml" -or -name "*.yaml" \) 2>&1))
|
||
|
|
||
|
##################
|
||
|
# Lint the files #
|
||
|
##################
|
||
|
for FILE in "${LIST_FILES[@]}"
|
||
|
do
|
||
|
#####################
|
||
|
# Get the file name #
|
||
|
#####################
|
||
|
FILE_NAME=$(basename "$FILE" 2>&1)
|
||
|
|
||
|
#######################################
|
||
|
# Make sure we dont lint node modules #
|
||
|
#######################################
|
||
|
# if [[ $FILE == *"node_modules"* ]]; then
|
||
|
# # This is a node modules file
|
||
|
# continue
|
||
|
# fi
|
||
|
|
||
|
##############
|
||
|
# File print #
|
||
|
##############
|
||
|
echo "---------------------------"
|
||
|
echo "File:[$FILE]"
|
||
|
|
||
|
################################
|
||
|
# Lint the file with the rules #
|
||
|
################################
|
||
|
LINT_CMD=$("$LINTER_NAME" "$YAML_LINTER_RULES" "$FILE" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
#########
|
||
|
# Error #
|
||
|
#########
|
||
|
echo "ERROR! Found errors in [$LINTER_NAME] linter!"
|
||
|
echo "ERROR:[$LINT_CMD]"
|
||
|
# Increment error count
|
||
|
((ERRORS_FOUND_YML++))
|
||
|
else
|
||
|
###########
|
||
|
# Success #
|
||
|
###########
|
||
|
echo " - File:[$FILE_NAME] was linted with [$LINTER_NAME] successfully"
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function LintXmlFiles #####################################################
|
||
|
LintXmlFiles()
|
||
|
{
|
||
|
################
|
||
|
# print header #
|
||
|
################
|
||
|
echo ""
|
||
|
echo "--------------------------------"
|
||
|
echo "Linting XML files..."
|
||
|
echo "--------------------------------"
|
||
|
echo ""
|
||
|
|
||
|
######################
|
||
|
# Name of the linter #
|
||
|
######################
|
||
|
LINTER_NAME="xmllint"
|
||
|
|
||
|
#######################################
|
||
|
# Validate we have yamllint installed #
|
||
|
#######################################
|
||
|
# shellcheck disable=SC2230
|
||
|
VALIDATE_INSTALL_CMD=$(which "$LINTER_NAME" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
# Failed
|
||
|
echo "ERROR! Failed to find [$LINTER_NAME] in system!"
|
||
|
echo "ERROR:[$VALIDATE_INSTALL_CMD]"
|
||
|
exit 1
|
||
|
else
|
||
|
# Success
|
||
|
echo "Successfully found binary in system"
|
||
|
echo "Location:[$VALIDATE_INSTALL_CMD]"
|
||
|
fi
|
||
|
|
||
|
#################################
|
||
|
# Get list of all files to lint #
|
||
|
#################################
|
||
|
# shellcheck disable=SC2207
|
||
|
LIST_FILES=($(find . -type f -name "*.xml" 2>&1))
|
||
|
|
||
|
##################
|
||
|
# Lint the files #
|
||
|
##################
|
||
|
for FILE in "${LIST_FILES[@]}"
|
||
|
do
|
||
|
#####################
|
||
|
# Get the file name #
|
||
|
#####################
|
||
|
FILE_NAME=$(basename "$FILE" 2>&1)
|
||
|
|
||
|
#######################################
|
||
|
# Make sure we dont lint node modules #
|
||
|
#######################################
|
||
|
# if [[ $FILE == *"node_modules"* ]]; then
|
||
|
# # This is a node modules file
|
||
|
# continue
|
||
|
# fi
|
||
|
|
||
|
##############
|
||
|
# File print #
|
||
|
##############
|
||
|
echo "---------------------------"
|
||
|
echo "File:[$FILE]"
|
||
|
|
||
|
################################
|
||
|
# Lint the file with the rules #
|
||
|
################################
|
||
|
LINT_CMD=$("$LINTER_NAME" "$FILE" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
#########
|
||
|
# Error #
|
||
|
#########
|
||
|
echo "ERROR! Found errors in [$LINTER_NAME] linter!"
|
||
|
echo "ERROR:[$LINT_CMD]"
|
||
|
# Increment error count
|
||
|
((ERRORS_FOUND_XML++))
|
||
|
else
|
||
|
###########
|
||
|
# Success #
|
||
|
###########
|
||
|
echo " - File:[$FILE_NAME] was linted with [$LINTER_NAME] successfully"
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function LintMdFiles ######################################################
|
||
|
LintMdFiles()
|
||
|
{
|
||
|
################
|
||
|
# print header #
|
||
|
################
|
||
|
echo ""
|
||
|
echo "--------------------------------"
|
||
|
echo "Linting Markdown files..."
|
||
|
echo "--------------------------------"
|
||
|
echo ""
|
||
|
|
||
|
######################
|
||
|
# Name of the linter #
|
||
|
######################
|
||
|
LINTER_NAME="markdownlint"
|
||
|
|
||
|
#######################################
|
||
|
# Validate we have yamllint installed #
|
||
|
#######################################
|
||
|
# shellcheck disable=SC2230
|
||
|
VALIDATE_INSTALL_CMD=$(which "$LINTER_NAME" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
# Failed
|
||
|
echo "ERROR! Failed to find [$LINTER_NAME] in system!"
|
||
|
echo "ERROR:[$VALIDATE_INSTALL_CMD]"
|
||
|
exit 1
|
||
|
else
|
||
|
# Success
|
||
|
echo "Successfully found binary in system"
|
||
|
echo "Location:[$VALIDATE_INSTALL_CMD]"
|
||
|
fi
|
||
|
|
||
|
#################################
|
||
|
# Get list of all files to lint #
|
||
|
#################################
|
||
|
# shellcheck disable=SC2207
|
||
|
LIST_FILES=($(find . -type f -name "*.md" 2>&1))
|
||
|
|
||
|
##################
|
||
|
# Lint the files #
|
||
|
##################
|
||
|
for FILE in "${LIST_FILES[@]}"
|
||
|
do
|
||
|
#####################
|
||
|
# Get the file name #
|
||
|
#####################
|
||
|
FILE_NAME=$(basename "$FILE" 2>&1)
|
||
|
|
||
|
#######################################
|
||
|
# Make sure we dont lint node modules #
|
||
|
#######################################
|
||
|
# if [[ $FILE == *"node_modules"* ]]; then
|
||
|
# # This is a node modules file
|
||
|
# continue
|
||
|
# fi
|
||
|
|
||
|
##############
|
||
|
# File print #
|
||
|
##############
|
||
|
echo "---------------------------"
|
||
|
echo "File:[$FILE]"
|
||
|
|
||
|
################################
|
||
|
# Lint the file with the rules #
|
||
|
################################
|
||
|
LINT_CMD=$("$LINTER_NAME" -c "$MD_LINTER_RULES" "$FILE" 2>&1)
|
||
|
|
||
|
#######################
|
||
|
# Load the error code #
|
||
|
#######################
|
||
|
ERROR_CODE=$?
|
||
|
|
||
|
##############################
|
||
|
# Check the shell for errors #
|
||
|
##############################
|
||
|
if [ $ERROR_CODE -ne 0 ]; then
|
||
|
#########
|
||
|
# Error #
|
||
|
#########
|
||
|
echo "ERROR! Found errors in [$LINTER_NAME] linter!"
|
||
|
echo "ERROR:[$LINT_CMD]"
|
||
|
# Increment error count
|
||
|
((ERRORS_FOUND_MD++))
|
||
|
else
|
||
|
###########
|
||
|
# Success #
|
||
|
###########
|
||
|
echo " - File:[$FILE_NAME] was linted with [$LINTER_NAME] successfully"
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
################################################################################
|
||
|
#### Function Footer ###########################################################
|
||
|
Footer()
|
||
|
{
|
||
|
echo ""
|
||
|
echo "---------------------------"
|
||
|
echo "The script has completed"
|
||
|
echo "---------------------------"
|
||
|
echo "ERRORS FOUND in YAML:[$ERRORS_FOUND_YML]"
|
||
|
echo "ERRORS FOUND in JSON:[$ERRORS_FOUND_JSON]"
|
||
|
echo "ERRORS FOUND in XML:[$ERRORS_FOUND_XML]"
|
||
|
echo "ERRORS FOUND IN MD:[$ERRORS_FOUND_MD]"
|
||
|
echo ""
|
||
|
|
||
|
###############################
|
||
|
# Exit with 1 if errors found #
|
||
|
###############################
|
||
|
if [ $ERRORS_FOUND_YML -ne 0 ] || [ $ERRORS_FOUND_JSON -ne 0 ] || [ $ERRORS_FOUND_XML -ne 0 ] || [ $ERRORS_FOUND_MD -ne 0 ]; then
|
||
|
# Failed exit
|
||
|
echo "Exiting with errors found!"
|
||
|
exit 1
|
||
|
else
|
||
|
# Successful exit
|
||
|
exit 0
|
||
|
fi
|
||
|
}
|
||
|
################################################################################
|
||
|
############################### MAIN ###########################################
|
||
|
################################################################################
|
||
|
|
||
|
##########
|
||
|
# Header #
|
||
|
##########
|
||
|
Header
|
||
|
|
||
|
########################
|
||
|
# Get the linter rules #
|
||
|
########################
|
||
|
GetLinterRules
|
||
|
|
||
|
######################
|
||
|
# Lint the Yml Files #
|
||
|
######################
|
||
|
LintYmlFiles
|
||
|
|
||
|
#######################
|
||
|
# Lint the json files #
|
||
|
#######################
|
||
|
LintJsonFiles
|
||
|
|
||
|
######################
|
||
|
# Lint the XML Files #
|
||
|
######################
|
||
|
LintXmlFiles
|
||
|
|
||
|
###########################
|
||
|
# Lint the Markdown Files #
|
||
|
###########################
|
||
|
LintMdFiles
|
||
|
|
||
|
##########
|
||
|
# Footer #
|
||
|
##########
|
||
|
Footer
|