-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-by-extension
executable file
·110 lines (93 loc) · 2.25 KB
/
find-by-extension
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
#!/usr/bin/env bash
#
#
# Determines the existence and echoes the names of files with the given file
# extension(s).
#
#
# Usage:
# find-by-extension <extension> [<extension> ...]
# find-by-extension -h
#
# Arguments:
# <extension> Extension to find by.
#
# Options:
# -h Display help and exit.
#
#
# Exit codes:
# 0 - successfully found files
# 1 - incorrect usage
# 2 - failed to find files
# exit codes
SUCCESS=0
INCORRECT_USAGE=1
FAILURE=2
#######################################
# Display help message.
# Arguments:
# None
# Outputs:
# Writes help message to stdout
#######################################
showhelp() {
cat << EOF
Usage:
find-by-extension <extension> [<extension> ...]
find-by-extension -h
Determines the existence and echoes the names of files with the given file
extension(s).
Arguments:
<extension> Extension to find by.
Options:
-h Display help and exit.
EOF
}
#######################################
# Asserts the given command runs successfully.
# Arguments:
# $1 - message to print before running command
# $2 - command to run
# $3 - error message to print on failure
# $4 - exit code to use on failure
# $5 - success message to print on success
# Outputs:
# Writes error message to stdout
#######################################
assert_success() {
echo $1
eval $2
if [[ $? -ne 0 ]]; then
echo "$3"
echo -e "\nGoodbye..."
exit $4
fi
echo -e $5
}
while getopts "h" flag; do
case "${flag}" in
h) showhelp; exit ${SUCCESS} ;;
?) showhelp; exit ${INCORRECT_USAGE} ;;
esac
done
to_ignore=("node_modules" "venv" "build" "dist" ".github" ".vscode")
# Add any directories that you want to ignore to the above array.
# The script will ignore all files in these directories.
max=${#to_ignore[@]}
index=1
ignore="-name '${to_ignore[0]}' -prune"
while [[ ${index} -lt ${max} ]]; do
ignore="${ignore} -o -name '${to_ignore[${index}]}' -prune"
index=$((index + 1))
done
max=$#
index=2
extensions="--regexp='$1\$'"
shift 1
while [[ ${index} -le ${max} ]]; do
extensions="${extensions} --regexp='$1\$'"
index=$((index + 1))
shift 1
done
eval "find . ${ignore} -o -print | grep ${extensions}"