2020-06-29 10:55:59 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
########### Super-Linter linting Functions @admiralawkbar ######################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
########################## FUNCTION CALLS BELOW ################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
#### Function LintCodebase #####################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
function LintCodebase() {
|
2020-06-29 10:55:59 -04:00
|
|
|
####################
|
|
|
|
# Pull in the vars #
|
|
|
|
####################
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_TYPE="${1}" && shift # Pull the variable and remove from array path (Example: JSON)
|
|
|
|
LINTER_NAME="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint)
|
|
|
|
LINTER_COMMAND="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file)
|
|
|
|
FILE_EXTENSIONS="${1}" && shift # Pull the variable and remove from array path (Example: *.json)
|
|
|
|
FILE_ARRAY=("$@") # Array of files to validate (Example: ${FILE_ARRAY_JSON})
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
######################
|
|
|
|
# Create Print Array #
|
|
|
|
######################
|
|
|
|
PRINT_ARRAY=()
|
|
|
|
|
|
|
|
################
|
|
|
|
# print header #
|
|
|
|
################
|
|
|
|
PRINT_ARRAY+=("")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
2020-07-21 13:09:07 -04:00
|
|
|
PRINT_ARRAY+=("Linting [${FILE_TYPE}] files...")
|
2020-06-29 10:55:59 -04:00
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
|
2020-06-30 15:32:42 -04:00
|
|
|
#####################################
|
|
|
|
# Validate we have linter installed #
|
|
|
|
#####################################
|
2020-07-21 13:09:07 -04:00
|
|
|
VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# Failed
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Failed to find [${LINTER_NAME}] in system!"
|
|
|
|
fatal "[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
# Success
|
2020-07-30 15:15:42 -04:00
|
|
|
debug "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Initialize empty Array #
|
|
|
|
##########################
|
|
|
|
LIST_FILES=()
|
|
|
|
|
|
|
|
################
|
|
|
|
# Set the flag #
|
|
|
|
################
|
|
|
|
SKIP_FLAG=0
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Check to see if we need to go through array or all files #
|
|
|
|
############################################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${#FILE_ARRAY[@]} -eq 0 ] && [ "${VALIDATE_ALL_CODEBASE}" == "false" ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# No files found in commit and user has asked to not validate code base
|
|
|
|
SKIP_FLAG=1
|
2020-07-30 15:15:42 -04:00
|
|
|
debug " - No files found in changeset to lint for language:[${FILE_TYPE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
elif [ ${#FILE_ARRAY[@]} -ne 0 ]; then
|
|
|
|
# We have files added to array of files to check
|
|
|
|
LIST_FILES=("${FILE_ARRAY[@]}") # Copy the array into list
|
|
|
|
else
|
|
|
|
###############################################################################
|
|
|
|
# Set the file seperator to newline to allow for grabbing objects with spaces #
|
|
|
|
###############################################################################
|
|
|
|
IFS=$'\n'
|
|
|
|
|
|
|
|
#################################
|
|
|
|
# Get list of all files to lint #
|
|
|
|
#################################
|
2020-07-21 13:09:07 -04:00
|
|
|
mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex "${FILE_EXTENSIONS}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
###########################
|
|
|
|
# Set IFS back to default #
|
|
|
|
###########################
|
2020-07-21 13:09:07 -04:00
|
|
|
IFS="${DEFAULT_IFS}"
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Set it back to empty if loaded with blanks from scanning #
|
|
|
|
############################################################
|
|
|
|
if [ ${#LIST_FILES[@]} -lt 1 ]; then
|
|
|
|
######################
|
|
|
|
# Set to empty array #
|
|
|
|
######################
|
|
|
|
LIST_FILES=()
|
|
|
|
#############################
|
|
|
|
# Skip as we found no files #
|
|
|
|
#############################
|
|
|
|
SKIP_FLAG=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
###############################
|
|
|
|
# Check if any data was found #
|
|
|
|
###############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${SKIP_FLAG} -eq 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
######################
|
|
|
|
# Print Header array #
|
|
|
|
######################
|
2020-07-01 17:40:40 -04:00
|
|
|
for LINE in "${PRINT_ARRAY[@]}"; do
|
2020-06-29 10:55:59 -04:00
|
|
|
#########################
|
|
|
|
# Print the header info #
|
|
|
|
#########################
|
2020-07-30 15:15:42 -04:00
|
|
|
info "${LINE}"
|
2020-06-29 10:55:59 -04:00
|
|
|
done
|
|
|
|
|
2020-07-06 13:46:51 -04:00
|
|
|
########################################
|
|
|
|
# Prepare context if TAP format output #
|
|
|
|
########################################
|
|
|
|
if IsTAP ; then
|
|
|
|
TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX")
|
|
|
|
INDEX=0
|
|
|
|
mkdir -p "${REPORT_OUTPUT_FOLDER}"
|
|
|
|
REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}"
|
|
|
|
fi
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
##################
|
|
|
|
# Lint the files #
|
|
|
|
##################
|
2020-07-01 17:40:40 -04:00
|
|
|
for FILE in "${LIST_FILES[@]}"; do
|
2020-07-23 13:52:43 -04:00
|
|
|
###################################
|
|
|
|
# Get the file name and directory #
|
|
|
|
###################################
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_NAME=$(basename "${FILE}" 2>&1)
|
2020-07-23 13:52:43 -04:00
|
|
|
DIR_NAME=$(dirname "${FILE}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#####################################################
|
|
|
|
# Make sure we dont lint node modules or test cases #
|
|
|
|
#####################################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE} == *"node_modules"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# This is a node modules file
|
|
|
|
continue
|
2020-07-21 13:09:07 -04:00
|
|
|
elif [[ ${FILE} == *"${TEST_CASE_FOLDER}"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# This is the test cases, we should always skip
|
|
|
|
continue
|
2020-07-21 13:09:07 -04:00
|
|
|
elif [[ ${FILE} == *".git"* ]]; then
|
2020-07-10 09:53:21 -04:00
|
|
|
# This is likely the .git folder and shouldnt be parsed
|
|
|
|
continue
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
|
2020-07-06 13:46:51 -04:00
|
|
|
##################################
|
|
|
|
# Increase the linted file index #
|
|
|
|
##################################
|
|
|
|
(("INDEX++"))
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
##############
|
|
|
|
# File print #
|
|
|
|
##############
|
2020-07-30 15:15:42 -04:00
|
|
|
info "---------------------------"
|
|
|
|
info "File:[${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
|
2020-07-21 09:23:32 -04:00
|
|
|
#################################
|
|
|
|
# Add the language to the array #
|
|
|
|
#################################
|
2020-07-21 13:09:07 -04:00
|
|
|
LINTED_LANGUAGES_ARRAY+=("${FILE_TYPE}")
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Set the base Var #
|
|
|
|
####################
|
|
|
|
LINT_CMD=''
|
|
|
|
|
2020-07-02 17:31:16 -04:00
|
|
|
####################################
|
|
|
|
# Corner case for pwsh subshell #
|
|
|
|
# - PowerShell (PSScriptAnalyzer) #
|
|
|
|
# - ARM (arm-ttk) #
|
|
|
|
####################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-01 04:13:30 -04:00
|
|
|
# Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell
|
2020-07-01 17:40:40 -04:00
|
|
|
LINT_CMD=$(
|
2020-07-21 13:09:07 -04:00
|
|
|
cd "${GITHUB_WORKSPACE}" || exit
|
|
|
|
pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }"
|
2020-07-01 17:40:40 -04:00
|
|
|
exit $? 2>&1
|
|
|
|
)
|
2020-07-23 13:52:43 -04:00
|
|
|
###############################################################################
|
|
|
|
# Corner case for groovy as we have to pass it as path and file in ant format #
|
|
|
|
###############################################################################
|
|
|
|
elif [[ ${FILE_TYPE} == "GROOVY" ]]; then
|
|
|
|
#######################################
|
|
|
|
# Lint the file with the updated path #
|
|
|
|
#######################################
|
|
|
|
LINT_CMD=$(
|
|
|
|
cd "${GITHUB_WORKSPACE}" || exit
|
|
|
|
${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1
|
|
|
|
)
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-01 17:40:40 -04:00
|
|
|
LINT_CMD=$(
|
2020-07-21 13:09:07 -04:00
|
|
|
cd "${GITHUB_WORKSPACE}" || exit
|
|
|
|
${LINTER_COMMAND} "${FILE}" 2>&1
|
2020-07-01 17:40:40 -04:00
|
|
|
)
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
#########
|
|
|
|
# Error #
|
|
|
|
#########
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Found errors in [${LINTER_NAME}] linter!"
|
|
|
|
error "[${LINT_CMD}]"
|
|
|
|
error "Linter CMD:[${LINTER_COMMAND} ${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
# Increment the error count
|
2020-07-21 13:09:07 -04:00
|
|
|
(("ERRORS_FOUND_${FILE_TYPE}++"))
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}"
|
2020-07-21 13:09:07 -04:00
|
|
|
AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
###########
|
|
|
|
# Success #
|
|
|
|
###########
|
2020-07-30 15:15:42 -04:00
|
|
|
info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully"
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
OkTap "${INDEX}" "${FILE}" "${TMPFILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
done
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#################################
|
|
|
|
# Generate report in TAP format #
|
|
|
|
#################################
|
|
|
|
if IsTAP && [ ${INDEX} -gt 0 ] ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}"
|
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function TestCodebase #####################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
function TestCodebase() {
|
2020-06-29 10:55:59 -04:00
|
|
|
####################
|
|
|
|
# Pull in the vars #
|
|
|
|
####################
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_TYPE="${1}" # Pull the variable and remove from array path (Example: JSON)
|
|
|
|
LINTER_NAME="${2}" # Pull the variable and remove from array path (Example: jsonlint)
|
|
|
|
LINTER_COMMAND="${3}" # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file)
|
|
|
|
FILE_EXTENSIONS="${4}" # Pull the variable and remove from array path (Example: *.json)
|
|
|
|
INDVIDUAL_TEST_FOLDER="${5}" # Folder for specific tests
|
2020-07-01 17:40:40 -04:00
|
|
|
TESTS_RAN=0 # Incremented when tests are ran, this will help find failed finds
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
################
|
|
|
|
# print header #
|
|
|
|
################
|
2020-07-30 15:15:42 -04:00
|
|
|
echo
|
|
|
|
info "----------------------------------------------"
|
|
|
|
info "----------------------------------------------"
|
|
|
|
info "Testing Codebase [${FILE_TYPE}] files..."
|
|
|
|
info "----------------------------------------------"
|
|
|
|
info "----------------------------------------------"
|
|
|
|
echo
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#####################################
|
|
|
|
# Validate we have linter installed #
|
|
|
|
#####################################
|
2020-07-21 13:09:07 -04:00
|
|
|
VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# Failed
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Failed to find [${LINTER_NAME}] in system!"
|
|
|
|
fatal "[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
# Success
|
2020-07-30 15:15:42 -04:00
|
|
|
info "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Initialize empty Array #
|
|
|
|
##########################
|
|
|
|
LIST_FILES=()
|
|
|
|
|
2020-06-30 10:56:15 -04:00
|
|
|
#################################
|
|
|
|
# Get list of all files to lint #
|
|
|
|
#################################
|
2020-07-21 13:09:07 -04:00
|
|
|
mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" -type f -regex "${FILE_EXTENSIONS}" ! -path "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/ansible/ghe-initialize/*" | sort 2>&1)
|
2020-07-14 09:48:28 -04:00
|
|
|
|
|
|
|
########################################
|
|
|
|
# Prepare context if TAP output format #
|
|
|
|
########################################
|
|
|
|
if IsTAP ; then
|
|
|
|
TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX")
|
|
|
|
mkdir -p "${REPORT_OUTPUT_FOLDER}"
|
|
|
|
REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}"
|
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
##################
|
|
|
|
# Lint the files #
|
|
|
|
##################
|
2020-07-01 17:40:40 -04:00
|
|
|
for FILE in "${LIST_FILES[@]}"; do
|
2020-06-29 10:55:59 -04:00
|
|
|
#####################
|
|
|
|
# Get the file name #
|
|
|
|
#####################
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_NAME=$(basename "${FILE}" 2>&1)
|
2020-07-23 13:52:43 -04:00
|
|
|
DIR_NAME=$(dirname "${FILE}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
############################
|
|
|
|
# Get the file pass status #
|
|
|
|
############################
|
|
|
|
# Example: markdown_good_1.md -> good
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_STATUS=$(echo "${FILE_NAME}" | cut -f2 -d'_')
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# If not found, assume it should be linted successfully #
|
|
|
|
#########################################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ -z "${FILE_STATUS}" ] || [[ ${FILE} == *"README"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
##################################
|
|
|
|
# Set to good for proper linting #
|
|
|
|
##################################
|
|
|
|
FILE_STATUS="good"
|
|
|
|
fi
|
|
|
|
|
|
|
|
##############
|
|
|
|
# File print #
|
|
|
|
##############
|
2020-07-30 15:15:42 -04:00
|
|
|
info "---------------------------"
|
|
|
|
info "File:[${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
########################
|
|
|
|
# Set the lint command #
|
|
|
|
########################
|
|
|
|
LINT_CMD=''
|
|
|
|
|
|
|
|
#######################################
|
|
|
|
# Check if docker and get folder name #
|
|
|
|
#######################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE_TYPE} == "DOCKER" ]]; then
|
|
|
|
if [[ ${FILE} == *"good"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
#############
|
|
|
|
# Good file #
|
|
|
|
#############
|
|
|
|
FILE_STATUS='good'
|
|
|
|
else
|
|
|
|
############
|
|
|
|
# Bad file #
|
|
|
|
############
|
|
|
|
FILE_STATUS='bad'
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
#####################
|
|
|
|
# Check for ansible #
|
|
|
|
#####################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE_TYPE} == "ANSIBLE" ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
########################################
|
|
|
|
# Make sure we dont lint certain files #
|
|
|
|
########################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# This is a file we dont look at
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-01 17:40:40 -04:00
|
|
|
LINT_CMD=$(
|
2020-07-21 13:09:07 -04:00
|
|
|
cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" || exit
|
|
|
|
${LINTER_COMMAND} "${FILE}" 2>&1
|
2020-07-01 17:40:40 -04:00
|
|
|
)
|
2020-07-21 13:09:07 -04:00
|
|
|
elif [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-01 04:13:30 -04:00
|
|
|
# Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell
|
2020-07-01 17:40:40 -04:00
|
|
|
LINT_CMD=$(
|
2020-07-21 13:09:07 -04:00
|
|
|
cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit
|
|
|
|
pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }"
|
2020-07-01 17:40:40 -04:00
|
|
|
exit $? 2>&1
|
|
|
|
)
|
2020-07-23 13:52:43 -04:00
|
|
|
###############################################################################
|
|
|
|
# Corner case for groovy as we have to pass it as path and file in ant format #
|
|
|
|
###############################################################################
|
|
|
|
elif [[ ${FILE_TYPE} == "GROOVY" ]]; then
|
|
|
|
#######################################
|
|
|
|
# Lint the file with the updated path #
|
|
|
|
#######################################
|
|
|
|
LINT_CMD=$(
|
|
|
|
cd "${GITHUB_WORKSPACE}" || exit
|
|
|
|
${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1
|
|
|
|
)
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-01 17:40:40 -04:00
|
|
|
LINT_CMD=$(
|
2020-07-21 13:09:07 -04:00
|
|
|
cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit
|
|
|
|
${LINTER_COMMAND} "${FILE}" 2>&1
|
2020-07-01 17:40:40 -04:00
|
|
|
)
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
2020-07-14 09:48:28 -04:00
|
|
|
########################################
|
|
|
|
# Increment counter that check was ran #
|
|
|
|
########################################
|
|
|
|
(("TESTS_RAN++"))
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
########################################
|
|
|
|
# Check for if it was supposed to pass #
|
|
|
|
########################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE_STATUS} == "good" ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
#########
|
|
|
|
# Error #
|
|
|
|
#########
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Found errors in [${LINTER_NAME}] linter!"
|
|
|
|
error "[${LINT_CMD}]"
|
|
|
|
error "Linter CMD:[${LINTER_COMMAND} ${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
# Increment the error count
|
2020-07-21 13:09:07 -04:00
|
|
|
(("ERRORS_FOUND_${FILE_TYPE}++"))
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
###########
|
|
|
|
# Success #
|
|
|
|
###########
|
2020-07-30 15:15:42 -04:00
|
|
|
info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully"
|
2020-07-14 09:48:28 -04:00
|
|
|
fi
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
OkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
#######################################
|
|
|
|
# File status = bad, this should fail #
|
|
|
|
#######################################
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -eq 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
#########
|
|
|
|
# Error #
|
|
|
|
#########
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Found errors in [${LINTER_NAME}] linter!"
|
|
|
|
error "This file should have failed test case!"
|
|
|
|
error "Command run:${NC}[\$${LINT_CMD}]"
|
|
|
|
error "[${LINT_CMD}]"
|
|
|
|
error "Linter CMD:[${LINTER_COMMAND} ${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
# Increment the error count
|
2020-07-21 13:09:07 -04:00
|
|
|
(("ERRORS_FOUND_${FILE_TYPE}++"))
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
###########
|
|
|
|
# Success #
|
|
|
|
###########
|
2020-07-30 15:15:42 -04:00
|
|
|
info " - File:${F[W]}[${FILE_NAME}]${F[B]} failed test case with ${F[W]}[${LINTER_NAME}]${F[B]} successfully"
|
2020-07-14 09:48:28 -04:00
|
|
|
fi
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
NotOkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}"
|
2020-07-21 13:09:07 -04:00
|
|
|
AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2020-06-30 16:23:24 -04:00
|
|
|
|
2020-07-14 09:48:28 -04:00
|
|
|
###########################################################################
|
|
|
|
# Generate report in TAP format and validate with the expected TAP output #
|
|
|
|
###########################################################################
|
|
|
|
if IsTAP && [ ${TESTS_RAN} -gt 0 ] ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
HeaderTap "${TESTS_RAN}" "${REPORT_OUTPUT_FILE}"
|
2020-07-14 09:48:28 -04:00
|
|
|
cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}"
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# If expected TAP report exists then compare with the generated report #
|
|
|
|
########################################################################
|
2020-07-21 13:09:07 -04:00
|
|
|
EXPECTED_FILE="${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}/reports/expected-${FILE_TYPE}.tap"
|
|
|
|
if [ -e "${EXPECTED_FILE}" ] ; then
|
2020-07-14 09:48:28 -04:00
|
|
|
TMPFILE=$(mktemp -q "/tmp/diff-${FILE_TYPE}.XXXXXX")
|
|
|
|
## Ignore white spaces, case sensitive
|
|
|
|
if ! diff -a -w -i "${EXPECTED_FILE}" "${REPORT_OUTPUT_FILE}" > "${TMPFILE}" 2>&1; then
|
|
|
|
#############################################
|
|
|
|
# We failed to compare the reporting output #
|
|
|
|
#############################################
|
2020-07-30 15:15:42 -04:00
|
|
|
error "Failed to assert TAP output:[${LINTER_NAME}]"!
|
|
|
|
info "Please validate the asserts!"
|
2020-07-14 09:48:28 -04:00
|
|
|
cat "${TMPFILE}"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# Success
|
2020-07-30 15:15:42 -04:00
|
|
|
info "Successfully validation in the expected TAP format for ${F[W]}[${LINTER_NAME}]"
|
2020-07-14 09:48:28 -04:00
|
|
|
fi
|
|
|
|
else
|
2020-07-30 15:15:42 -04:00
|
|
|
warn "No TAP expected file found at:[${EXPECTED_FILE}]"
|
|
|
|
info "skipping report assertions"
|
2020-07-19 15:26:20 -04:00
|
|
|
#####################################
|
|
|
|
# Append the file type to the array #
|
|
|
|
#####################################
|
|
|
|
WARNING_ARRAY_TEST+=("${FILE_TYPE}")
|
2020-07-14 09:48:28 -04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-06-30 16:23:24 -04:00
|
|
|
##############################
|
|
|
|
# Validate we ran some tests #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ "${TESTS_RAN}" -eq 0 ]; then
|
2020-06-30 16:23:24 -04:00
|
|
|
#################################################
|
|
|
|
# We failed to find files and no tests were ran #
|
|
|
|
#################################################
|
2020-07-30 15:15:42 -04:00
|
|
|
error "Failed to find any tests ran for the Linter:[${LINTER_NAME}]"!
|
2020-07-27 17:20:06 -04:00
|
|
|
fatal "Please validate logic or that tests exist!"
|
2020-06-30 16:23:24 -04:00
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function RunTestCases #####################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
function RunTestCases() {
|
2020-06-29 10:55:59 -04:00
|
|
|
# This loop will run the test cases and exclude user code
|
|
|
|
# This is called from the automation process to validate new code
|
|
|
|
# When a PR is opened, the new code is validated with the default branch
|
|
|
|
# version of linter.sh, and a new container is built with the latest codebase
|
|
|
|
# for testing. That container is spun up, and ran,
|
|
|
|
# with the flag: TEST_CASE_RUN=true
|
|
|
|
# So that the new code can be validated against the test cases
|
|
|
|
|
|
|
|
#################
|
|
|
|
# Header prints #
|
|
|
|
#################
|
2020-07-30 15:15:42 -04:00
|
|
|
echo
|
|
|
|
info "----------------------------------------------"
|
|
|
|
info "-------------- TEST CASE RUN -----------------"
|
|
|
|
info "----------------------------------------------"
|
|
|
|
echo
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Test case languages #
|
|
|
|
#######################
|
2020-06-29 16:22:52 -04:00
|
|
|
# TestCodebase "Language" "Linter" "Linter-command" "Regex to find files" "Test Folder"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "ANSIBLE" "ansible-lint" "ansible-lint -v -c ${ANSIBLE_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "ansible"
|
|
|
|
TestCodebase "ARM" "arm-ttk" "Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" ".*\.\(json\)\$" "arm"
|
2020-07-06 16:51:17 -04:00
|
|
|
TestCodebase "BASH" "shellcheck" "shellcheck --color" ".*\.\(sh\|bash\|dash\|ksh\)\$" "shell"
|
2020-07-22 15:07:08 -04:00
|
|
|
TestCodebase "CLOUDFORMATION" "cfn-lint" "cfn-lint --config-file ${CLOUDFORMATION_LINTER_RULES}" ".*\.\(json\|yml\|yaml\)\$" "cloudformation"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "CLOJURE" "clj-kondo" "clj-kondo --config ${CLOJURE_LINTER_RULES} --lint" ".*\.\(clj\|cljs\|cljc\|edn\)\$" "clojure"
|
|
|
|
TestCodebase "COFFEESCRIPT" "coffeelint" "coffeelint -f ${COFFEESCRIPT_LINTER_RULES}" ".*\.\(coffee\)\$" "coffeescript"
|
|
|
|
TestCodebase "CSS" "stylelint" "stylelint --config ${CSS_LINTER_RULES}" ".*\.\(css\)\$" "css"
|
|
|
|
TestCodebase "DART" "dart" "dartanalyzer --fatal-infos --fatal-warnings --options ${DART_LINTER_RULES}" ".*\.\(dart\)\$" "dart"
|
|
|
|
TestCodebase "DOCKER" "dockerfilelint" "dockerfilelint -c ${DOCKER_LINTER_RULES}" ".*\(Dockerfile\)\$" "docker"
|
2020-07-19 15:50:40 -04:00
|
|
|
TestCodebase "EDITORCONFIG" "editorconfig-checker" "editorconfig-checker" ".*\.ext$" "editorconfig-checker"
|
2020-06-29 16:22:52 -04:00
|
|
|
TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" "env"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "GO" "golangci-lint" "golangci-lint run -c ${GO_LINTER_RULES}" ".*\.\(go\)\$" "golang"
|
2020-07-23 13:52:43 -04:00
|
|
|
TestCodebase "GROOVY" "npm-groovy-lint" "npm-groovy-lint -c $GROOVY_LINTER_RULES --failon error" ".*\.\(groovy\|jenkinsfile\|gradle\)\$" "groovy"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "HTML" "htmlhint" "htmlhint --config ${HTML_LINTER_RULES}" ".*\.\(html\)\$" "html"
|
|
|
|
TestCodebase "JAVASCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(js\)\$" "javascript"
|
|
|
|
TestCodebase "JAVASCRIPT_STANDARD" "standard" "standard ${JAVASCRIPT_STANDARD_LINTER_RULES}" ".*\.\(js\)\$" "javascript"
|
2020-06-29 16:22:52 -04:00
|
|
|
TestCodebase "JSON" "jsonlint" "jsonlint" ".*\.\(json\)\$" "json"
|
2020-07-19 15:48:04 -04:00
|
|
|
TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "kotlin"
|
2020-07-24 13:27:54 -04:00
|
|
|
TestCodebase "LUA" "lua" "luacheck" ".*\.\(lua\)\$" "lua"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "markdown"
|
2020-06-29 16:22:52 -04:00
|
|
|
TestCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "perl"
|
|
|
|
TestCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "php"
|
2020-07-24 14:54:11 -04:00
|
|
|
TestCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "php"
|
2020-07-21 13:09:07 -04:00
|
|
|
TestCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" ".*\.\(ymlopenapi\|jsonopenapi\)\$" "openapi"
|
|
|
|
TestCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell"
|
|
|
|
TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf"
|
2020-07-21 20:59:05 -04:00
|
|
|
TestCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "python"
|
|
|
|
TestCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config ${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "python"
|
2020-07-19 15:48:04 -04:00
|
|
|
TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku"
|
2020-07-21 15:39:14 -04:00
|
|
|
TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby"
|
2020-07-21 14:50:04 -04:00
|
|
|
TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states"
|
2020-07-21 15:39:14 -04:00
|
|
|
TestCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "terraform"
|
2020-07-24 00:55:31 -04:00
|
|
|
TestCodebase "TERRAFORM_TERRASCAN" "terrascan" "terrascan -f " ".*\.\(tf\)\$" "terraform_terrascan"
|
2020-07-21 15:39:14 -04:00
|
|
|
TestCodebase "TYPESCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(ts\)\$" "typescript"
|
|
|
|
TestCodebase "TYPESCRIPT_STANDARD" "standard" "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin ${TYPESCRIPT_STANDARD_LINTER_RULES}" ".*\.\(ts\)\$" "typescript"
|
2020-07-19 15:48:04 -04:00
|
|
|
TestCodebase "XML" "xmllint" "xmllint" ".*\.\(xml\)\$" "xml"
|
2020-07-24 12:20:22 -04:00
|
|
|
TestCodebase "YAML" "yamllint" "yamllint -c ${YAML_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "yml"
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#################
|
|
|
|
# Footer prints #
|
|
|
|
#################
|
|
|
|
# Call the footer to display run information
|
|
|
|
# and exit with error code
|
|
|
|
Footer
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function LintAnsibleFiles #################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
function LintAnsibleFiles() {
|
2020-06-29 10:55:59 -04:00
|
|
|
######################
|
|
|
|
# Create Print Array #
|
|
|
|
######################
|
|
|
|
PRINT_ARRAY=()
|
|
|
|
|
|
|
|
################
|
|
|
|
# print header #
|
|
|
|
################
|
|
|
|
PRINT_ARRAY+=("")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
PRINT_ARRAY+=("Linting [Ansible] files...")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
PRINT_ARRAY+=("----------------------------------------------")
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Name of the linter #
|
|
|
|
######################
|
|
|
|
LINTER_NAME="ansible-lint"
|
|
|
|
|
|
|
|
###########################################
|
|
|
|
# Validate we have ansible-lint installed #
|
|
|
|
###########################################
|
2020-07-21 13:09:07 -04:00
|
|
|
VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# Failed
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Failed to find ${LINTER_NAME} in system!"
|
|
|
|
fatal "[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
# Success
|
2020-07-30 15:15:42 -04:00
|
|
|
debug "Successfully found binary in system"
|
|
|
|
debug "Location:[${VALIDATE_INSTALL_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Initialize empty Array #
|
|
|
|
##########################
|
|
|
|
LIST_FILES=()
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Create flag to skip #
|
|
|
|
#######################
|
|
|
|
SKIP_FLAG=0
|
|
|
|
|
|
|
|
######################################################
|
|
|
|
# Only go into ansible linter if we have base folder #
|
|
|
|
######################################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ -d "${ANSIBLE_DIRECTORY}" ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
|
2020-06-30 15:27:36 -04:00
|
|
|
#################################
|
|
|
|
# Get list of all files to lint #
|
|
|
|
#################################
|
2020-07-21 13:09:07 -04:00
|
|
|
mapfile -t LIST_FILES < <(ls "${ANSIBLE_DIRECTORY}"/*.{yaml,yml} 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
###############################################################
|
|
|
|
# Set the list to empty if only MD and TXT files were changed #
|
|
|
|
###############################################################
|
|
|
|
# No need to run the full ansible checks on read only file changes
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ "${READ_ONLY_CHANGE_FLAG}" -eq 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
##########################
|
|
|
|
# Set the array to empty #
|
|
|
|
##########################
|
|
|
|
LIST_FILES=()
|
|
|
|
###################################
|
|
|
|
# Send message that were skipping #
|
|
|
|
###################################
|
2020-07-30 15:15:42 -04:00
|
|
|
debug "- Skipping Ansible lint run as file(s) that were modified were read only..."
|
2020-06-29 10:55:59 -04:00
|
|
|
############################
|
|
|
|
# Create flag to skip loop #
|
|
|
|
############################
|
|
|
|
SKIP_FLAG=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# Check if we have data to look at #
|
|
|
|
####################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${SKIP_FLAG} -eq 0 ]; then
|
2020-07-01 17:40:40 -04:00
|
|
|
for LINE in "${PRINT_ARRAY[@]}"; do
|
2020-06-29 10:55:59 -04:00
|
|
|
#########################
|
|
|
|
# Print the header line #
|
|
|
|
#########################
|
2020-07-30 15:15:42 -04:00
|
|
|
info "${LINE}"
|
2020-06-29 10:55:59 -04:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-07-06 13:46:51 -04:00
|
|
|
########################################
|
|
|
|
# Prepare context if TAP output format #
|
|
|
|
########################################
|
|
|
|
if IsTAP ; then
|
|
|
|
TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX")
|
|
|
|
INDEX=0
|
|
|
|
mkdir -p "${REPORT_OUTPUT_FOLDER}"
|
|
|
|
REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}"
|
|
|
|
fi
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
##################
|
|
|
|
# Lint the files #
|
|
|
|
##################
|
2020-07-01 17:40:40 -04:00
|
|
|
for FILE in "${LIST_FILES[@]}"; do
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
########################################
|
|
|
|
# Make sure we dont lint certain files #
|
|
|
|
########################################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
# This is a file we dont look at
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2020-07-06 13:46:51 -04:00
|
|
|
##################################
|
|
|
|
# Increase the linted file index #
|
|
|
|
##################################
|
|
|
|
(("INDEX++"))
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
####################
|
|
|
|
# Get the filename #
|
|
|
|
####################
|
2020-07-21 13:09:07 -04:00
|
|
|
FILE_NAME=$(basename "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
##############
|
|
|
|
# File print #
|
|
|
|
##############
|
2020-07-30 15:15:42 -04:00
|
|
|
info "---------------------------"
|
|
|
|
info "File:[${FILE}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
################################
|
|
|
|
# Lint the file with the rules #
|
|
|
|
################################
|
2020-07-21 13:09:07 -04:00
|
|
|
LINT_CMD=$("${LINTER_NAME}" -v -c "${ANSIBLE_LINTER_RULES}" "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1)
|
2020-06-29 10:55:59 -04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
2020-07-21 13:09:07 -04:00
|
|
|
if [ ${ERROR_CODE} -ne 0 ]; then
|
2020-06-29 10:55:59 -04:00
|
|
|
#########
|
|
|
|
# Error #
|
|
|
|
#########
|
2020-07-27 17:35:53 -04:00
|
|
|
error "Found errors in [${LINTER_NAME}] linter!"
|
|
|
|
error "[${LINT_CMD}]"
|
2020-06-29 10:55:59 -04:00
|
|
|
# Increment error count
|
|
|
|
((ERRORS_FOUND_ANSIBLE++))
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}"
|
2020-07-15 17:37:46 -04:00
|
|
|
AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
fi
|
|
|
|
|
2020-06-29 10:55:59 -04:00
|
|
|
else
|
|
|
|
###########
|
|
|
|
# Success #
|
|
|
|
###########
|
2020-07-30 15:15:42 -04:00
|
|
|
info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully"
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# Store the linting as a temporary file in TAP format #
|
|
|
|
#######################################################
|
|
|
|
if IsTAP ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
OkTap "${INDEX}" "${FILE}" "${TMPFILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
fi
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
done
|
2020-07-06 13:46:51 -04:00
|
|
|
|
|
|
|
#################################
|
|
|
|
# Generate report in TAP format #
|
|
|
|
#################################
|
|
|
|
if IsTAP && [ ${INDEX} -gt 0 ] ; then
|
2020-07-15 17:26:24 -04:00
|
|
|
HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}"
|
2020-07-06 13:46:51 -04:00
|
|
|
cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}"
|
|
|
|
fi
|
2020-07-30 15:15:42 -04:00
|
|
|
else
|
|
|
|
########################
|
|
|
|
# No Ansible dir found #
|
|
|
|
########################
|
|
|
|
warn "No Ansible base directory found at:[${ANSIBLE_DIRECTORY}]"
|
|
|
|
debug "skipping ansible lint"
|
2020-06-29 10:55:59 -04:00
|
|
|
fi
|
|
|
|
}
|
2020-07-06 13:46:51 -04:00
|
|
|
################################################################################
|
|
|
|
#### Function IsTap ############################################################
|
|
|
|
function IsTAP() {
|
|
|
|
if [ "${OUTPUT_FORMAT}" == "tap" ] ; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function TransformTAPDetails ##############################################
|
|
|
|
function TransformTAPDetails() {
|
2020-07-21 13:09:07 -04:00
|
|
|
DATA=${1}
|
2020-07-06 13:46:51 -04:00
|
|
|
if [ -n "${DATA}" ] && [ "${OUTPUT_DETAILS}" == "detailed" ] ; then
|
|
|
|
#########################################################
|
|
|
|
# Transform new lines to \\n, remove colours and colons #
|
|
|
|
#########################################################
|
|
|
|
echo "${DATA}" | awk 'BEGIN{RS="\n";ORS="\\n"}1' | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" | tr ':' ' '
|
|
|
|
fi
|
|
|
|
}
|
2020-07-15 17:26:24 -04:00
|
|
|
################################################################################
|
|
|
|
#### Function HeaderTap ########################################################
|
|
|
|
function HeaderTap() {
|
2020-07-20 09:14:40 -04:00
|
|
|
################
|
|
|
|
# Pull in Vars #
|
|
|
|
################
|
2020-07-21 13:09:07 -04:00
|
|
|
INDEX="${1}" # File being validated
|
|
|
|
OUTPUT_FILE="${2}" # Output location
|
2020-07-20 09:14:40 -04:00
|
|
|
|
|
|
|
###################
|
|
|
|
# Print the goods #
|
|
|
|
###################
|
|
|
|
printf "TAP version 13\n1..%s\n" "${INDEX}" > "${OUTPUT_FILE}"
|
2020-07-15 17:26:24 -04:00
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function OkTap ############################################################
|
|
|
|
function OkTap() {
|
2020-07-20 09:14:40 -04:00
|
|
|
################
|
|
|
|
# Pull in Vars #
|
|
|
|
################
|
2020-07-21 13:09:07 -04:00
|
|
|
INDEX="${1}" # Location
|
|
|
|
FILE="${2}" # File being validated
|
|
|
|
TEMP_FILE="${3}" # Temp file location
|
2020-07-20 09:14:40 -04:00
|
|
|
|
|
|
|
###################
|
|
|
|
# Print the goods #
|
|
|
|
###################
|
|
|
|
echo "ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}"
|
2020-07-15 17:26:24 -04:00
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function NotOkTap #########################################################
|
|
|
|
function NotOkTap() {
|
2020-07-20 09:14:40 -04:00
|
|
|
################
|
|
|
|
# Pull in Vars #
|
|
|
|
################
|
2020-07-21 13:09:07 -04:00
|
|
|
INDEX="${1}" # Location
|
|
|
|
FILE="${2}" # File being validated
|
|
|
|
TEMP_FILE="${3}" # Temp file location
|
2020-07-20 09:14:40 -04:00
|
|
|
|
|
|
|
###################
|
|
|
|
# Print the goods #
|
|
|
|
###################
|
|
|
|
echo "not ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}"
|
2020-07-15 17:37:46 -04:00
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function AddDetailedMessageIfEnabled ######################################
|
|
|
|
function AddDetailedMessageIfEnabled() {
|
2020-07-20 09:14:40 -04:00
|
|
|
################
|
|
|
|
# Pull in Vars #
|
|
|
|
################
|
2020-07-21 13:09:07 -04:00
|
|
|
LINT_CMD="${1}" # Linter command
|
|
|
|
TEMP_FILE="${2}" # Temp file
|
2020-07-20 09:14:40 -04:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Check the return #
|
|
|
|
####################
|
|
|
|
DETAILED_MSG=$(TransformTAPDetails "${LINT_CMD}")
|
2020-07-15 17:37:46 -04:00
|
|
|
if [ -n "${DETAILED_MSG}" ] ; then
|
2020-07-21 13:09:07 -04:00
|
|
|
printf " ---\n message: %s\n ...\n" "${DETAILED_MSG}" >> "${TEMP_FILE}"
|
2020-07-15 17:37:46 -04:00
|
|
|
fi
|
|
|
|
}
|