format-code: option to explicitly enable linters
Sometimes it is useful to explicitly enable a single (or set of)
linters. Add an option (`--enable <tool>`) such that if any enable
is set then only those linters will be utilized.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I49e4b760217e6a0df5c16275c466958b3a2422b3
diff --git a/scripts/format-code.sh b/scripts/format-code.sh
index fefbae3..4554c85 100755
--- a/scripts/format-code.sh
+++ b/scripts/format-code.sh
@@ -10,8 +10,8 @@
#
function display_help()
{
- echo "usage: format-code.sh [-h | --help] [--no-diff] [--disable <tool>]"
- echo " [--list-tools] [<path>]"
+ echo "usage: format-code.sh [-h | --help] [--no-diff] [--list-tools]"
+ echo " [--disable <tool>] [--enable <tool>] [<path>]"
echo
echo "Format and lint a repository."
echo
@@ -19,6 +19,7 @@
echo " --list-tools Display available linters and formatters"
echo " --no-diff Don't show final diff output"
echo " --disable <tool> Disable linter"
+ echo " --enable <tool> Enable only specific linters"
echo " --allow-missing Run even if linters are not all present"
echo " path Path to git repository (default to pwd)"
}
@@ -35,7 +36,7 @@
LINTERS_ENABLED=()
declare -A LINTERS_FAILED=()
-eval set -- "$(getopt -o 'h' --long 'help,list-tools,no-diff,disable:,allow-missing' -n 'format-code.sh' -- "$@")"
+eval set -- "$(getopt -o 'h' --long 'help,list-tools,no-diff,disable:,enable:,allow-missing' -n 'format-code.sh' -- "$@")"
while true; do
case "$1" in
'-h'|'--help')
@@ -60,6 +61,11 @@
shift && shift
;;
+ '--enable')
+ LINTERS_ENABLED+=("$2")
+ shift && shift
+ ;;
+
'--allow-missing')
ALLOW_MISSING=yes
shift
@@ -215,6 +221,7 @@
echo "unknown"
}
+LINTERS_AVAILABLE=()
function check_linter()
{
TITLE="$1"
@@ -224,6 +231,12 @@
return
fi
+ if [ 0 -ne "${#LINTERS_ENABLED[@]}" ]; then
+ if ! [[ "${LINTERS_ENABLED[*]}" =~ $1 ]]; then
+ return
+ fi
+ fi
+
EXE="${ARGS[0]}"
if [ ! -x "${EXE}" ]; then
if ! which "${EXE}" > /dev/null 2>&1 ; then
@@ -250,7 +263,7 @@
fi
fi
- LINTERS_ENABLED+=( "${TITLE}" )
+ LINTERS_AVAILABLE+=( "${TITLE}" )
}
# Check for a global .linter-ignore file.
@@ -281,7 +294,7 @@
done
# Call each linter.
-for op in "${LINTERS_ENABLED[@]}"; do
+for op in "${LINTERS_AVAILABLE[@]}"; do
# Determine the linter-specific ignore file(s).
LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter")