From 17454dc8f4de86f372ccb31fec29a380906e8d97 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 01:49:52 +0000 Subject: [PATCH] Add whitelisting functionality We can now specify our own whitelisting file, overriding the default of 'lenchk-excludes' which must stay in the same directory as lenchk, at least for now. The custom file can be wherever you want it, however. I'd like to use the `readarray`/`mapfile` BASH built-in for populating the array variable, but it didn't seem to want to work, so I just went for another `while read` loop. It's working very nicely as-is, though. I've changed the required whitelist format to one of simplicity: one filename per line. Why the previous format? Because of files which may have a newline character in them, but I realised in this environment that's just not going to happen; force of habit, I guess. I'd like to allow users to be able to whitelist entire directories, but I don't see that being so high on the priority list, for now. What do you think? --- tests/lenchk | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/tests/lenchk b/tests/lenchk index 3591382..5bb3bb2 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -4,14 +4,6 @@ # Author E-Mail - terminalforlife@yahoo.com # Author GitHub - https://github.com/terminalforlife #------------------------------------------------------------------------------ -# Features: -# -#TODO: k -# -# Bugs: -# -# N/A -#------------------------------------------------------------------------------ Progrm=${0##*/} @@ -25,21 +17,23 @@ Usage(){ -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. -N, --no-preview - Omit the preview of each triggered line. -S, --no-summary - Omit the summary before $Progrm exits. + -W, --no-whilelist - Do not use the whitelist file. -c, --colorize - Provide color via esscape sequences. -l, --limit [INT] - Override the limit of 80 columns. -p, --pager - Use less(1) to page the output. - -w, --whitelist [FILE] - Whitelist files stored within. + -w, --wl-file [FILE] - Use an alternative whitelist file. - Whitelisting uses the following format, by way of example: + Files to whitelist must be placed line-by-line; one path per line. - file 'cheat.sheets/sheets/_perl/1line' - file 'cheat.sheets/sheets/find' + cheat.sheets/sheets/_perl/1line + cheat.sheets/sheets/find The file paths to provide must begin with 'cheat.sheets/' to indicate the root of the repository, otherwise this feature will fail. - The location of the whitelisting file ('$Progrm-exclude') must remain - in the same directory in which $Progrm is stored. + The location of the whitelisting file ('$Progrm-excludes') must + remain in the same directory in which $Progrm is stored, unless + otherwise specified. EOF } @@ -48,6 +42,7 @@ Err(){ [ $1 -gt 0 ] && exit $1 } +WLFile='lenchk-excludes' MaxCols=80 while [ "$1" ]; do @@ -70,6 +65,16 @@ while [ "$1" ]; do NoSummary='True' ;; --no-subdirs|-D) NoSubDirs='True' ;; + --no-whitelist|-W) + NoWhiteList='True' ;; + --wl-file|-w) + shift + + if [ -z "$1" ]; then + Err 1 'No alternative whitelist file provided.' + else + WLFile=$1 + fi ;; *) Err 1 'Incorrect option(s) specified.' ;; esac @@ -93,9 +98,24 @@ Main(){ Dirs=(../sheets/*) [ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*) + # If the whitelist file exists, use it, unless whitelisting is disabled. + # Keeping this test outside of the loop to avoid unnecessary processing. + if [ "$NoWhiteList" != 'True' ] && [ -f "$WLFile" ]; then + # Read the whitelist file, line-by-line, generating an array thereof. + Whitelisted=() + while read; do + Whitelisted+=("../${REPLY#cheat.sheets/}") + done < "$WLFile" + fi + for File in "${Dirs[@]}"; { [ -f "$File" ] || continue + # If the current file matches one which is whitelisted, skip it. + for CurWL in "${Whitelisted[@]}"; { + [ "$File" == "$CurWL" ] && continue 2 + } + HaveBeenHit='False' LineNum=0