-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcleanupUntrackedTickets.sh
executable file
·355 lines (324 loc) · 12.9 KB
/
cleanupUntrackedTickets.sh
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
#!/usr/bin/env bash
##
## Trivial Tickets Ticketsystem
## Copyright (C) 2019 The Contributors
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
##
## Ticketsystem Trivial Tickets
##
## Matriculation numbers: 3040018, 6694964, 3478222
## Lecture: Programmieren II, INF16B
## Lecturer: Herr Prof. Dr. Helmut Neemann
## Institute: Duale Hochschule Baden-Württemberg Mosbach
##
## ---------------
## Script to delete untracked tickets and mails
## to revert the server into its initial state
##
## Get more information about this script here:
## https://github.com/mortenterhart/trivial-tickets/wiki/Cleaning-untracked-Tickets-and-Mails
##
# Script constants
program_name="trivial-tickets"
script_name="${0##*/}"
root_dir="${0%/*}"
# Exit status
exit_status=0
# Paths to the ticket and mail directories
ticket_dir="${root_dir}/files/tickets"
mail_dir="${root_dir}/files/mails"
# Script options
clean_options=()
force_removal="false"
dry_run="false"
interactive="false"
exclude_pattern=""
# printInfo prints an info message consisting
# of all arguments concatenated to stdout.
#
# Parameters:
# $*: the info message
function printInfo() {
printf "[%s] [INFO] %s\n" "${program_name}" "$*"
}
# printError prints an error message consisting
# of all arguments concatenated to stderr.
#
# Parameters:
# $*: the error message
function printError() {
printf "%s: %s\n" "${script_name}" "$*" >&2
printf "Try '%s --help' for more information.\n" "${script_name}" >&2
}
# helpText prints an informative help text
# with options and usage instructions.
function helpText() {
printf "Usage: %s [option(s)]\n" "${script_name}"
printf "Delete untracked tickets and mails from cache\n"
printf "in 'files/tickets' and 'files/mails' to revert\n"
printf "the server to its original state.\n\n"
printf "%s offers the following options:\n" "${script_name}"
printf " -n, --dry-run Show which files would be deleted,\n"
printf " but don't delete actually any.\n"
printf " -e, --exclude=<PATTERN>\n"
printf " Exclude files matching the pattern\n"
printf " from deletion.\n"
printf " -f, --force Force deletion without prompting for.\n"
printf " -h, --help Display this help text and exit.\n"
printf " -i, --interactive Delete files using an interactive menu.\n"
printf " -m, --mail-dir=<DIR>\n"
printf " Specify another directory for mails.\n"
printf " Defaults to \"files/mails\".\n"
printf " -q, --quiet Don't print the file paths as they\n"
printf " are removed.\n"
printf " -t, --ticket-dir=<DIR>\n"
printf " Specify another directory for tickets.\n"
printf " Defaults to \"files/tickets\".\n"
printf "Mandatory arguments to long options are also mandatory\n"
printf "for short options. Long options may be abbreviated as\n"
printf "long as they do not get ambiguous.\n"
}
# testDirectoryExists checks if the directory given as
# parameter $1 exists. If the directory does not exist
# the script exits with an error, otherwise nothing is
# done.
#
# Parameters:
# $1: the directory path to check for
function testDirectoryExists() {
local directory_name="$1"
if ! [ -d "${directory_name}" ]; then
printError "directory does not exist: '${directory_name}'"
exit 2
fi
}
# processOptions parses all command-line options and
# sets the appropriate options for this script. The
# script exits with an error in case an option is
# not defined, requires an argument or is ambiguous.
function processOptions() {
local clean_mode_set="false"
local option
OPTIND=1
while getopts ":efhimnqt:-:" option "$@"; do
case "${option}" in
e)
# -e, --exclude=<pattern>
clean_options+=("-e" "${OPTARG}")
exclude_pattern="${OPTARG}"
;;
f)
# -f, --force
force_removal="true"
;;
h)
# -h, --help
helpText
exit 0
;;
i)
# -i, --interactive
clean_options+=("-i")
interactive="true"
clean_mode_set="true"
;;
m)
# -m, --mail-dir
testDirectoryExists "${OPTARG}"
mail_dir="${OPTARG}"
;;
n)
# -n, --dry-run
clean_options+=("-n")
dry_run="true"
clean_mode_set="true"
;;
q)
# -q, --quiet
clean_options+=("-q")
;;
t)
# -t, --ticket-dir
testDirectoryExists "${OPTARG}"
ticket_dir="${OPTARG}"
;;
-)
# Parsing of long options
local option="${OPTARG%%=*}"
local option_argument="${OPTARG#*=}"
local long_options="dry-run exclude force help interactive mail-dir quiet ticket-dir"
local long_options_arguments="exclude mail-dir ticket-dir"
local -a option_matches=($(compgen -W "${long_options}" -- "${option}"))
if [ "${#option_matches[@]}" -eq 0 ]; then
printError "unrecognized option: --${option}"
exit 1
elif [ "${#option_matches[@]}" -eq 1 ]; then
if compgen -W "${long_options_arguments}" -- "${option_matches[0]}" >/dev/null 2>&1; then
if [[ "${OPTARG}" != *=* ]]; then
printError "option requires an argument: '--${option_matches[0]}'" >&2
exit 2
elif [ -z "${option_argument}" ]; then
printError "option requires a non-empty argument: '--${option_matches[0]}'"
exit 2
fi
elif ! compgen -W "${long_options_arguments}" -- "${option_matches[0]}" >/dev/null 2>&1 && [[ "${OPTARG}" == *=* ]]; then
printError "option does not allow an argument: --${option_matches[0]}"
exit 2
fi
case "${option_matches[0]}" in
dry-run)
clean_options+=("--dry-run")
dry_run="true"
clean_mode_set="true"
;;
exclude)
clean_options+=("--exclude" "${option_argument}")
exclude_pattern="${option_argument}"
;;
force)
force_removal="true"
;;
help)
helpText;
exit 0
;;
interactive)
clean_options+=("--interactive")
interactive="true"
clean_mode_set="true"
;;
mail-dir)
testDirectoryExists "${option_argument}"
mail_dir="${option_argument}"
;;
quiet)
clean_options+=("--quiet")
;;
ticket-dir)
testDirectoryExists "${option_argument}"
ticket_dir="${option_argument}"
;;
esac
else
local -i arg_pos="$((OPTIND - 1))"
printf "%s: ambiguous option: '%s'; could be one of:\n" "${script_name}" "${!arg_pos}" >&2
printf " --%s\n" "${option_matches[@]}" >&2
printf "Try '%s --help' for more information.\n" "${script_name}" >&2
exit 3
fi
;;
\?)
# invalid option or '?'
if [ "${OPTARG}" == "?" ]; then
helpText
exit 0
fi
printError "invalid option -- ${OPTARG}"
exit 1
;;
:)
# option requires an argument
printError "option requires an argument -- ${OPTARG}"
exit 2
;;
esac
done
# If no clean mode was specified assume '-f' because
# the 'git clean' step requires one of the mode options
# to be set
if ! "${clean_mode_set}"; then
clean_options+=("-f")
fi
# Shift the processed options so that only
# non-option parameters are left
shift "$((OPTIND - 1))"
}
# promptMenu prints the available commands and their
# keys for the prompt menu.
function promptMenu() {
printInfo "Commands: [y] Continue deletion [n] Show which files would be removed"
printInfo " [*] Abort deletion [q] Quit"
}
# Parse and process the command-line options
processOptions "$@"
printInfo "You attempted to remove all untracked tickets and mails from these directories:"
printInfo " ${ticket_dir}"
printInfo " ${mail_dir}"
# Collect the untracked ticket and mail files from
# 'files/tickets' and 'files/mails' and store them in an
# array. Exclude the files matching the exclude pattern
# from search if one was given.
untracked_files=()
mapfile -t untracked_files < <(git -C "${root_dir}" ls-files --ignored --exclude-standard --others \
$(if [ -n "${exclude_pattern}" ]; then echo "--exclude" "${exclude_pattern}"; fi) \
"${ticket_dir}/*.json" "${mail_dir}/*.json" 2>/dev/null)
# Only start deletion process if untracked files were found
if [ "${#untracked_files[@]}" -gt 0 ]; then
printInfo "In total, ${#untracked_files[@]} untracked file(s) found to be removed."
continue_deletion="y"
# Show the prompt as long as no terminating command
# was entered
prompt_finished="false"
while ! "${prompt_finished}"; do
# Show the command prompt if none of --force, --dry-run or
# --interactive were specified. In those cases skip the prompt
# and execute the clean command directly.
if ! "${force_removal}" && ! "${dry_run}" && ! "${interactive}"; then
promptMenu
read -rep "$(printInfo "Command: ")" continue_deletion
fi
case "${continue_deletion}" in
n)
# [n] (dry run): Show which files would be removed and
# return to the prompt
printInfo "These untracked files would be deleted by cleanup:"
printf "\n"
printf "%s\n" "${untracked_files[@]}"
printf "\n"
;;
q)
# [q] (quit): Quit the program without deleting anything
printInfo "Quitting, no cleanup done."
prompt_finished="true"
;;
y)
# [y] (continue): Continue execution and cleanup all
# untracked tickets and mails. If --dry-run or --interactive
# were passed display the files or the interactive menu.
if "${dry_run}" || "${interactive}"; then
printInfo "These untracked files would be deleted by cleanup:"
else
printInfo "Removing all untracked tickets and mails:"
fi
printf "\n"
# Execute 'git clean' to manage the cleanup process
git -C "${root_dir}" clean -X "${clean_options[@]}" -- "${ticket_dir}/*.json" "${mail_dir}/*.json"
exit_status=$?
prompt_finished="true"
;;
*)
# [*] (abort): Any other user input aborts the deletion
printInfo "Aborted deletion of tickets and mails."
printInfo "Type 'y' explicitly to confirm."
prompt_finished="true"
;;
esac
done
else
printInfo "No untracked files found and thus no removal done."
fi
# Exit the script with the exit status
exit "${exit_status}"