-
Notifications
You must be signed in to change notification settings - Fork 20
/
file-trailing-newline
executable file
·916 lines (762 loc) · 22.4 KB
/
file-trailing-newline
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#!/usr/bin/env bash
set -u
############################################################
# Adjust for each check
############################################################
# Name and messages
MY_NAME="file-trailing-newline"
MY_DESC="Scans recursively for files not containing a trailing newline."
MY_FINISH_OK="All files have a trailing newline."
MY_FINISH_ERR="Some files do not have a trailing newline."
# Configuration file prefix
MY_CONF_PRE="FILE_TRAILING_NEWLINE_"
# Custom required binaries
REQUIRED_CUST_BINS="aci-trailing-newline"
# Binaries required for fixing
REQUIRED_FIX_BINS=""
# Enable custom options (cmd arguments)
# that can be parsed to the actual check binary?
ENABLE_CUST_OPS=0
# When not specifying --custom,
# always use this as the default options
# to parse to the check binary.
DEFAULT_CUST_OPS=""
# How to add your check here:
# ---------------------------
# $1 This comes from xargs/sh and represents
# the current file to work on:
# xargs sh '${MY_CHECK}' --
#
# __CUSTOM_OPT_PLACEHOLDER__
# This will be replaced either with custom options
# or with the default options.
MY_CHECK="aci-trailing-newline -n \"\$1\" || echo \"\$1: No trailing newline at EOF.\""
# Can this check fix the problems?
ENABLE_FIX=1
# Command for fixing
MY_FIX_CMD="aci-trailing-newline -n \"\$1\" || (echo \"\" >> \"\$1\" && echo \"Fixing: \$1\" || echo \"FAILED: \$1\")"
# Command to be displayed for --info
MY_INFO="aci-trailing-newline -v"
############################################################
# Do not edit from here
############################################################
#
# Version
#
MY_VERSION="0.15"
MY_DATE="2018-04-09"
#
# Credits
#
MY_AUTHOR="cytopia"
MY_EMAIL="[email protected]"
#
# Required system binaries
#
REQUIRED_BINS="awk grep find sed tr xargs"
#
# Variables populated by cmd args or config file
#
MY_PATH=
MY_SHE=
MY_EXT=0
MY_IGN=
MY_TXT=0
MY_SIZ=0
MY_DRY=0
MY_LST=0
MY_VER=0
MY_DEB=0
MY_FIX=0
MY_CFG=
MY_CUS=
CLR_SUC="\033[0;32m"
CLR_ERR="\033[0;31m"
CLR_CLS="\033[0m"
################################################################################
#
# F U N C T I O N S
#
################################################################################
print_usage() {
if [ "${ENABLE_CUST_OPS}" = "1" ];then
_custom=" [--custom=\"opts\"]"
else
_custom=""
fi
if [ "${ENABLE_FIX}" = "1" ];then
_fix=" [--fix]"
else
_fix=""
fi
echo "Usage: ${MY_NAME} [--text] [--size] [--shebang=php] [--extension=tpl,htm,html,php,...] [--ignore=dir1,dir2] [--config=conf] [--confpre=${MY_CONF_PRE}]${_custom}${_fix} [--verbose] [--debug] [--dry] [--list] --path=dir"
echo " ${MY_NAME} --info"
echo " ${MY_NAME} --help"
echo " ${MY_NAME} --version"
echo
echo "${MY_DESC}"
echo "Will return 1 on occurance, otherwise 0."
echo
echo "Required arguments:"
echo
echo " --path= Specify directory where to scan."
echo
echo
echo "Optional run arguments:"
if [ "${ENABLE_FIX}" = "1" ];then
echo " --fix Fixable :-)"
echo " Fix the problems for the specified files."
echo " Note, all other options below also apply"
echo
fi
echo " --text Limit search to text files only (non-binary)."
echo " Can be narrowed further with '--extension'"
echo
echo " --size Limit search to files which are not empty (bigger than 0 bytes)."
echo
echo " --shebang= Only find files (shell scripts) with this specific shebang."
echo " It is useful to combine this with --text and --size for faster searches."
echo " Use with --dry to see how this search command is generated."
echo " Example:"
echo " --shebang=bash"
echo " --shebang=php"
echo " --shebang=sh"
echo
echo " --extension= Only find files matching those extensions."
echo " Comma separated list of file extensions."
echo " Only find files matching those extensions."
echo " Defaults to all files if not specified or empty."
echo " Example:"
echo " --extension=html,php,inc"
echo " --extension=php"
echo
echo " --ignore= Comma separated list of ignore paths."
echo " Directories must be specified from the starting location of --path."
echo " Example:"
echo " ignore 'foor/bar' folder inside '/var/www' path:"
echo " --path=/var/www --ignore=foo/bar"
echo
echo " --config= Load configuration file."
echo " File must contain the following directives:"
echo " ${MY_CONF_PRE}EXTENSION=\"\" # comma separated"
echo " ${MY_CONF_PRE}IGNORE=\"\" # comma separated"
echo " ${MY_CONF_PRE}TEXT=0|1 # 0 or 1"
if [ "${ENABLE_CUST_OPS}" = "1" ];then
echo " ${MY_CONF_PRE}CUSTOM=\"\" # custom command"
fi
echo " Note that cmd arguments take precedence over"
echo " config file settings."
echo
echo " --confpre= Set custom configuration directive prefix."
echo " Current default ist: '${MY_CONF_PRE}'."
echo " This is useful, when you want to define different defaults"
echo " per check via configuration file."
echo
if [ "${ENABLE_CUST_OPS}" = "1" ];then
echo " --custom= Parse custom command line option to the check binary."
echo " Note that when you want to add config files or other files"
echo " you must use an absolute path."
echo
echo " Current custom command:"
echo " --custom=\"${DEFAULT_CUST_OPS//\"/\\\"}\"" # \" -> \\\"
echo
echo " Overwrite example:"
echo " --custom=\"--color --config /absoulte/path/conf.json\""
echo
fi
echo " --verbose Be verbose and print commands and files being checked."
echo
echo
echo " --debug Print system messages."
echo
echo
echo "Optional training arguments:"
echo " --dry Don't do anything, just display the commands."
echo
echo " --list Instead of searching inside the files, just display the filenames"
echo " that would be found by --path, --extension and --ignore"
echo
echo
echo "System arguments:"
echo " --info Show versions of required commands (useful for bugreports)."
echo " --help Show help screen."
echo " --version Show version information."
echo
echo
echo "${MY_NAME} is part of the awesome-ci collection."
echo "https://github.com/cytopia/awesome-ci"
}
print_version() {
echo "tool: ${MY_NAME}"
echo "version: v${MY_VERSION} (${MY_DATE})"
echo "author: ${MY_AUTHOR}"
echo "email: ${MY_EMAIL}"
echo
echo "${MY_NAME} is part of the awesome-ci collection."
echo "https://github.com/cytopia/awesome-ci"
}
print_info() {
#
# Required binaries
#
if ! echo "${MY_INFO}" | grep -cqE "^bash"; then
MY_CMD_VERSION="bash --version | grep -E '(([0-9]+)(\.))+'"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^\(awk"; then
MY_CMD_VERSION="(awk -Wversion 2>/dev/null || awk --version) | grep -E '(([0-9]+)(\.))+'"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^grep"; then
MY_CMD_VERSION="grep -V | grep -E '([0-9]+\.+)+'"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^\(find"; then
MY_CMD_VERSION="(find --version >/dev/null 2>&1) && (find --version 2>&1 | head -1) || (echo 'find (BSD find)')"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^\(sed"; then
MY_CMD_VERSION="(sed --version >/dev/null 2>&1) && (sed --version 2>&1 | head -1) || (echo 'sed (BSD sed)')"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^\(tr"; then
MY_CMD_VERSION="(tr --version >/dev/null 2>&1) && (tr --version 2>&1 | head -1) || (echo 'tr (BSD tr)')"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
if ! echo "${MY_INFO}" | grep -cqE "^\(xargs"; then
MY_CMD_VERSION="(xargs --version >/dev/null 2>&1) && (xargs --version 2>&1 | head -1) || (echo 'xargs (BSD xargs)')"
echo "\$ ${MY_CMD_VERSION}"
eval "${MY_CMD_VERSION}"
echo
fi
#
# Custom binary
#
if [ "${MY_INFO}" != "" ]; then
echo "\$ ${MY_INFO}"
eval "${MY_INFO}"
echo
fi
#
# --fix binary
#
if [ "${ENABLE_FIX}" = "1" ] && [ "${REQUIRED_FIX_BINS}" != "" ]; then
_can_fix="1"
for _bin in ${REQUIRED_FIX_BINS}; do
if ! command -v "${_bin}" >/dev/null 2>&1; then
echo "${_bin}: Not found."
_can_fix="0}"
fi
done
if [ "${_can_fix}" = "1" ]; then
echo "\$ ${MY_FIX_INFO}"
eval "${MY_FIX_INFO}"
echo
else
echo "Unable to use --fix"
echo
return 0
fi
fi
}
check_requirements() {
_debug="${1}"
_ret1=0
_ret2=0
# System binaries
for _bin in ${REQUIRED_BINS}; do
if ! command -v "${_bin}" >/dev/null 2>&1; then
echo "[ERR] Required sys binary '${_bin}' not found."
_ret1=1
else
if [ "${_debug}" = "1" ]; then
echo "[OK] Required system binary '${_bin}' found."
fi
fi
done
# Specific binaries for this check
for _bin in ${REQUIRED_CUST_BINS}; do
if ! command -v "${_bin}" >/dev/null 2>&1; then
echo "[ERR] Required custom binary '${_bin}' not found."
_ret2=1
else
if [ "${_debug}" = "1" ]; then
echo "[OK] Required custom binary '${_bin}' found."
fi
fi
done
# Binaries required to fix
if [ "${ENABLE_FIX}" = "1" ] && [ "${MY_FIX}" = "1" ]; then
for _bin in ${REQUIRED_FIX_BINS}; do
if ! command -v "${_bin}" >/dev/null 2>&1; then
echo "[ERR] Required --fix binary '${_bin}' not found."
_ret2=1
else
if [ "${_debug}" = "1" ]; then
echo "[OK] Required --fix binary '${_bin}' found."
fi
fi
done
fi
return $((_ret1 + _ret2))
}
check_config_file() {
_config="${1}"
# Check config file
if [ ! -f "${_config}" ]; then
echo "[CONFIG] Config file not found: ${_config}"
return 1
fi
# Check directives
if ! grep -Eq "^${MY_CONF_PRE}IGNORE=" "${_config}"; then
echo "[CONFIG] ${MY_CONF_PRE}IGNORE variable not found in config."
return 1
fi
if ! grep -Eq "^${MY_CONF_PRE}IGNORE=[\"']{1}.*[\"']{1}$" "${_config}"; then
echo "[CONFIG] Invalid syntax for ${MY_CONF_PRE}IGNORE variable."
return 1
fi
if ! grep -Eq "^${MY_CONF_PRE}EXTENSION=" "${_config}"; then
echo "[CONFIG] ${MY_CONF_PRE}EXTENSION variable not found in config."
return 1
fi
if ! grep -Eq "^${MY_CONF_PRE}EXTENSION=[\"']{1}.*[\"']{1}$" "${_config}"; then
echo "[CONFIG] Invalid syntax for ${MY_CONF_PRE}EXTENSION variable."
return 1
fi
if ! grep -Eq "^${MY_CONF_PRE}TEXT=" "${_config}"; then
echo "[CONFIG] ${MY_CONF_PRE}TEXT variable not found in config."
return 1
fi
if ! grep -Eq "^${MY_CONF_PRE}TEXT=[\"']*[01]{1}[\"']*$" "${_config}"; then
echo "[CONFIG] Invalid syntax for ${MY_CONF_PRE}TEXT variable."
return 1
fi
# Check optional directives
if [ "${ENABLE_CUST_OPS}" = "1" ]; then
if ! grep -Eq "^${MY_CONF_PRE}CUSTOM=[\"']{1}.*[\"']{1}$" "${_config}"; then
echo "[CONFIG] Invalid syntax for ${MY_CONF_PRE}CUSTOM variable."
return 1
fi
fi
}
#
# System independent method
# to get number of CPU cores.
# (defaults to 1 if impossible)
#
num_cpu() {
if ! _num="$( getconf _NPROCESSORS_ONLN 2>/dev/null )"; then
echo "1"
else
echo "${_num}"
fi
}
#
# sed wrapper that automatically escapes
# regex literals
#
mysed() {
_input="$1"
_search="$2"
_replace="$3"
echo "${_input}" | sed "s/$(echo "${_search}" | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo "${_replace}" | sed -e 's/[\/&]/\\&/g')/g"
}
################################################################################
#
# M A I N E N T R Y P O I N T
#
################################################################################
############################################################
# Command Line arguments
############################################################
#
# Read command line arguments
#
while [ $# -gt 0 ]; do
case "${1}" in
# ----------------------------------------
--path=*)
MY_PATH="${1//--path=/}"
;;
# ----------------------------------------
--shebang=*)
MY_SHE="${1//--shebang=/}"
;;
# ----------------------------------------
--extension=*)
MY_EXT="${1//--extension=/}"
;;
# ----------------------------------------
--ignore=*)
MY_IGN="${1//--ignore=/}"
;;
# ----------------------------------------
--config=*)
MY_CFG="${1//--config=/}"
;;
# ----------------------------------------
--confpre=*)
MY_CONF_PRE="${1//--confpre=/}"
;;
# ----------------------------------------
--custom=*)
if [ "${ENABLE_CUST_OPS}" = "1" ]; then
MY_CUS="${1//--custom=/}"
else
echo "Invalid argument: ${1}"
echo "Type '${MY_NAME} --help' for available options."
exit 1
fi
;;
# ----------------------------------------
--fix)
if [ "${ENABLE_FIX}" = "1" ]; then
MY_FIX=1
else
echo "Invalid argument: ${1}"
echo "Type '${MY_NAME} --help' for available options."
exit 1
fi
;;
# ----------------------------------------
--text)
MY_TXT=1
;;
# ----------------------------------------
--size)
MY_SIZ=1
;;
# ----------------------------------------
--dry)
MY_DRY=1
;;
# ----------------------------------------
--list)
MY_LST=1
;;
# ----------------------------------------
--verbose)
MY_VER=1
;;
# ----------------------------------------
--debug)
MY_DEB=1
;;
# ----------------------------------------
--info)
if ! check_requirements "${MY_DEB}"; then
exit 1
fi
if ! print_info; then
exit 1
fi
exit 0
;;
# ----------------------------------------
--help)
print_usage
exit 0
;;
# ----------------------------------------
--version)
print_version
exit 0
;;
# ----------------------------------------
*)
echo "Invalid argument: ${1}"
echo "Type '${MY_NAME} --help' for available options."
exit 1
;;
esac
shift
done
############################################################
# Sanity Checks
############################################################
#
# Check general requirements
#
if ! check_requirements "${MY_DEB}"; then
exit 1
fi
#
# Check path
#
if [ "${MY_PATH}" = "" ]; then
echo "--path not specified"
echo "Type '${MY_NAME} --help' for available options."
exit 1
fi
if [ ! -e "${MY_PATH}" ]; then
echo "Specified path does not exist: '${MY_PATH}'."
echo "Type '${MY_NAME} --help' for available options."
exit 1
fi
#
# Check and load config if desired
#
if [ "${MY_CFG}" != "" ]; then
if ! check_config_file "${MY_CFG}"; then
exit 1
fi
. "${MY_CFG}"
fi
############################################################
# Evaluate Settings
############################################################
CPU_CORES="$(num_cpu)"
# Var substitutions for config file directives
# to match current program.
EXTENSION="${MY_CONF_PRE}EXTENSION"
IGNORE="${MY_CONF_PRE}IGNORE"
TEXT="${MY_CONF_PRE}TEXT"
SIZE="${MY_CONF_PRE}SIZE"
CUSTOM="${MY_CONF_PRE}CUSTOM"
#
# Decide on Shebang
#
if [ "${MY_SHE}" != "" ]; then
MY_SHE="xargs -0 -P ${CPU_CORES} -n1 awk '/^#!.*(\/${MY_SHE}|[[:space:]]+${MY_SHE})/{print FILENAME}'"
else
MY_SHE=""
fi
#
# Decide on File extensions
#
if [ "${MY_CFG}" != "" ] && [ "${MY_EXT}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Cmd arg --extension=${MY_EXT} will take precedence over config file value".
fi
MY_EXT="${MY_EXT}"
elif [ "${MY_CFG}" != "" ] && [ "${MY_EXT}" = "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using config file values: ext: ${!EXTENSION}"
fi
MY_EXT="${!EXTENSION}"
elif [ "${MY_CFG}" = "" ] && [ "${MY_EXT}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using cmd argument: ext: '${MY_EXT}'"
fi
MY_EXT="${MY_EXT}"
else
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using all file extensions"
fi
MY_EXT=
fi
#
# Decide on ignore paths
#
if [ "${MY_CFG}" != "" ] && [ "${MY_IGN}" != "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Cmd arg --ignore=${MY_IGN} will take precedence over config file value".
fi
MY_IGN="${MY_IGN}"
elif [ "${MY_CFG}" != "" ] && [ "${MY_IGN}" = "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using config file values: ignore: ${!IGNORE}"
fi
MY_IGN="${!IGNORE}"
elif [ "${MY_CFG}" = "" ] && [ "${MY_IGN}" != "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using cmd argument: ignore: ${MY_IGN}"
fi
MY_IGN="${MY_IGN}"
else
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Not ignoring anything"
fi
MY_IGN=
fi
#
# Decide on text files (non-binary) or all files
#
if [ "${MY_CFG}" != "" ] && [ "${MY_TXT}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Cmd arg --text: '${MY_TXT}' will take precedence over config file value".
fi
MY_TXT="${MY_TXT}"
elif [ "${MY_CFG}" != "" ] && [ "${MY_TXT}" = "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using config file values: --text: ${!TEXT}"
fi
MY_TXT="${!TEXT}"
elif [ "${MY_CFG}" = "" ] && [ "${MY_TXT}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using cmd argument: --text: ${MY_TXT}"
fi
MY_TXT="${MY_TXT}"
else
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Not narrowing down by text files"
fi
MY_TXT="0"
fi
#
# Decide on file size
#
if [ "${MY_CFG}" != "" ] && [ "${MY_SIZ}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Cmd arg --size: '${MY_SIZ}' will take precedence over config file value".
fi
MY_SIZ="${MY_SIZ}"
elif [ "${MY_CFG}" != "" ] && [ "${MY_SIZ}" = "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using config file values: --size: ${!SIZE}"
fi
MY_SIZ="${!SIZE}"
elif [ "${MY_CFG}" = "" ] && [ "${MY_SIZ}" != "0" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using cmd argument: --size: ${MY_SIZ}"
fi
MY_SIZ="${MY_SIZ}"
else
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Not narrowing down by size > 0 bytes"
fi
MY_SIZ="0"
fi
if [ "${MY_SIZ}" = "1" ]; then
MY_SIZ="! -size 0"
else
MY_SIZ=""
fi
#
# Decide on Custom string
#
if [ "${ENABLE_CUST_OPS}" = "1" ]; then
if [ "${MY_CFG}" != "" ] && [ "${MY_CUS}" != "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Cmd arg --custom=\"${MY_CUS}\" will take precedence over config file value".
fi
MY_CUS="${MY_CUS}"
elif [ "${MY_CFG}" != "" ] && [ "${MY_CUS}" = "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using config file values: custom: \"${!CUSTOM}\""
fi
MY_CUS="${!CUSTOM}"
elif [ "${MY_CFG}" = "" ] && [ "${MY_CUS}" != "" ]; then
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] Using cmd argument: custom: \"${MY_CUS}\""
fi
MY_CUS="${MY_CUS}"
else
if [ "${MY_DEB}" = "1" ]; then
echo "[ARG] No customization applied"
fi
MY_CUS=
fi
fi
############################################################
# Build command
############################################################
#
# 'find' pattern for file extensions
#
if [ "${MY_EXT}" != "" ]; then
NAME_PATTERN="\( -iname \*.${MY_EXT//,/ -o -iname \\*.} \)"
else
NAME_PATTERN=""
fi
#
# 'find' pattern for ignores/excludes
#
if [ "${MY_IGN}" != "" ]; then
EXCL_PATTERN="-not \( -path \"${MY_PATH}/${MY_IGN//,/*\" -o -path \"${MY_PATH}\/}*\" \)"
else
EXCL_PATTERN=""
fi
#
# Fix?
#
if [ "${MY_FIX}" = "1" ]; then
MY_CHECK="${MY_FIX_CMD}"
else
#
# Normal check
# (Apply custom command?)
#
if [ "${MY_CUS}" = "" ]; then
MY_CHECK="$( mysed "${MY_CHECK}" "__CUSTOM_OPT_PLACEHOLDER__" "${DEFAULT_CUST_OPS}" )"
else
MY_CHECK="$( mysed "${MY_CHECK}" "__CUSTOM_OPT_PLACEHOLDER__" "${MY_CUS}" )"
fi
fi
#
# Show files or grep in found files?
#
# Be verbose?
if [ "${MY_VER}" = "1" ]; then
XARGS_VERBOSE="-t"
else
XARGS_VERBOSE=""
fi
#
# Build command
#
### Text files (list-only)
if [ "${MY_TXT}" = "1" ] && [ "${MY_LST}" = "1" ]; then
if [ "${MY_SHE}" != "" ]; then
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | xargs -0 -P ${CPU_CORES} -n1 grep -Il '' | tr '\n' '\0' | ${MY_SHE}"
else
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | xargs -0 -P ${CPU_CORES} -n1 grep -Il ''"
fi
### Text files (run!!!)
elif [ "${MY_TXT}" = "1" ] && [ "${MY_LST}" = "0" ]; then
if [ "${MY_SHE}" != "" ]; then
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | xargs -0 -P ${CPU_CORES} -n1 grep -Il '' | tr '\n' '\0' | ${MY_SHE} | tr '\n' '\0' | xargs -0 -P ${CPU_CORES} -n1 ${XARGS_VERBOSE} sh -c 'if [ -f \"\${1}\" ]; then ${MY_CHECK}; fi' --"
else
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | xargs -0 -P ${CPU_CORES} -n1 grep -Il '' | tr '\n' '\0' | xargs -0 -P ${CPU_CORES} -n1 ${XARGS_VERBOSE} sh -c 'if [ -f \"\${1}\" ]; then ${MY_CHECK}; fi' --"
fi
### All files (list-only)
elif [ "${MY_TXT}" = "0" ] && [ "${MY_LST}" = "1" ]; then
if [ "${MY_SHE}" != "" ]; then
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | ${MY_SHE}"
else
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print"
fi
### All files (run!!!)
else
if [ "${MY_SHE}" != "" ]; then
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | ${MY_SHE} | tr '\n' '\0' | xargs -0 -P ${CPU_CORES} -n1 ${XARGS_VERBOSE} sh -c 'if [ -f \"\${1}\" ]; then ${MY_CHECK}; fi' --"
else
MY_CMD="find ${MY_PATH} -type f ${NAME_PATTERN} ${EXCL_PATTERN} ${MY_SIZ} -print0 | xargs -0 -P ${CPU_CORES} -n1 ${XARGS_VERBOSE} sh -c 'if [ -f \"\${1}\" ]; then ${MY_CHECK}; fi' --"
fi
fi
############################################################
# Execute
############################################################
# Dry mode?
if [ "${MY_DRY}" = "1" ]; then
printf "%s\n" "${MY_CMD}"
exit 0
fi
printf "\$ %s\n" "${MY_CMD}"
output="$(eval "${MY_CMD}")"
# If showing files only, exit normally
if [ "${MY_LST}" = "1" ]; then
printf "%s\n" "${output}"
exit 0
fi
if [ "${output}" != "" ]; then
printf "%s\n" "${output}"
printf "${CLR_ERR}[ERR] %s${CLR_CLS}\n" "${MY_FINISH_ERR}"
exit 1
else
printf "${CLR_SUC}[OK] %s${CLR_CLS}\n" "${MY_FINISH_OK}"
exit 0
fi