-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrun.sh
431 lines (350 loc) · 13.5 KB
/
mrun.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
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
#!/bin/bash
PROJECT_MAP_FILE="projects.mrun"
PROJECT_ID=""
COMMAND=""
OS_TYPE=""
ACTION=""
usage() {
echo -e "\nUsage: $0 -p <project_id> -c \"<command>\""
echo -e " -p \tSpecify the unique project ID"
echo -e " -c \tProvide the command to execute inside the selected project folder (enclosed in quotes)"
echo -e "\nExample: $0 -p project_id -c \"npm install\""
echo -e "\nUse the -h option for more information on how to use this script."
exit 1
}
help_message() {
echo -e "\nHelp: This script allows you to run commands inside specific project directories."
echo -e "You can also list, add, remove, or update projects in your mapping file."
echo -e "\nCommands:"
echo -e " -p <project_id> \tSpecify the project ID (refer to the mapping file)"
echo -e " -c <command> \tCommand to execute inside the selected project's directory"
echo -e "\nAdditional Options:"
echo -e " -l \tList all available project IDs from the mapping file."
echo -e " -d \tList all available project IDs with their directories."
echo -e " -a \tAdd a new project to the mapping file."
echo -e " -r \tRemove a project from the mapping file."
echo -e " -u \tUpdate a project's directory in the mapping file."
echo -e " -h \tShow this help message."
exit 0
}
log_message() {
MESSAGE="$1"
LOG_TO_FILE=$2
LOG_TO_CLI=$3
NEWLINE=$4
LOGGING_FILE="mrun.log"
if [[ $NEWLINE == true ]]; then
echo ""
fi
FORMATTED_MESSAGE="[$(date)] - $MESSAGE"
if [[ "$LOG_TO_CLI" == true ]]; then
echo -e "$FORMATTED_MESSAGE"
fi
if [[ "$LOG_TO_FILE" == true ]]; then
echo -e "$FORMATTED_MESSAGE" >> "$LOGGING_FILE"
fi
}
check_empty_input() {
USER_INPUT_TO_CHECK="$1"
if [[ -z "$USER_INPUT_TO_CHECK" ]]; then
log_message "Error: Input cannot be empty!" true true false
exit 1
fi
}
detect_os() {
case "$(uname)" in
"Darwin")
OS_TYPE="macOS"
;;
"Linux")
OS_TYPE="Linux"
;;
"CYGWIN"|"MINGW"*|"MSYS")
OS_TYPE="Windows"
;;
*)
OS_TYPE="Unknown"
;;
esac
}
check_dependencies() {
DETECTED_OS_TYPE="$1"
if ! command -v jq &> /dev/null; then
log_message "Error: jq is required but not installed. Please install jq first." true true false
exit 1
fi
if [ "$DETECTED_OS_TYPE" == "Windows" ] && ! command -v realpath &> /dev/null; then
log_message "Error: realpath is required but not installed. Please install realpath first." true true false
exit 1
fi
}
validate_project_id() {
PROJECT_ID_TO_VALIDATE="$1"
if [[ ! "$PROJECT_ID_TO_VALIDATE" =~ ^[a-zA-Z0-9_-]+$ ]]; then
log_message "Error: Invalid project ID '$1'. Only alphanumeric characters, hyphens, and underscores are allowed." true true false
exit 1
fi
}
validate_directory() {
PROJECT_DIR_TO_VALIDATE="$1"
if [[ ! -d "$PROJECT_DIR_TO_VALIDATE" ]]; then
log_message "Error: Project directory '$1' not found!" true true false
exit 1
fi
}
validate_directory_unique() {
PROJECT_MAPPING_FILE="$1"
PROJECT_DIR_TO_VALIDATE="$2"
validate_directory "$PROJECT_DIR_TO_VALIDATE"
if jq -e "to_entries | .[] | select(.value == \"$PROJECT_DIR_TO_VALIDATE\")" "$PROJECT_MAPPING_FILE" > /dev/null; then
log_message "Error: The directory '$PROJECT_DIR_TO_VALIDATE' is already assigned to another project!" true true false
exit 1
fi
}
validate_project_id_unique() {
PROJECT_MAPPING_FILE="$1"
PROJECT_ID_TO_VALIDATE="$2"
validate_project_id "$PROJECT_ID_TO_VALIDATE"
if jq -e ".\"$PROJECT_ID_TO_VALIDATE\"" "$PROJECT_MAPPING_FILE" > /dev/null; then
log_message "Error: Project ID '$PROJECT_ID_TO_VALIDATE' already exists!" true true false
exit 1
fi
}
sanitize_command() {
COMMAND_TO_SANITIZE="$1"
if echo "$COMMAND_TO_SANITIZE" | grep -q '[;&|<>()\$]'; then
log_message "Error: Command contains potentially dangerous characters (e.g., ; & | < >)." true true false
exit 1
fi
if echo "$COMMAND_TO_SANITIZE" | grep -q '[^a-zA-Z0-9\-\_\/\ \=]'; then
log_message "Error: Command contains invalid characters." true true false
exit 1
fi
}
validate_mapping_file() {
PROJECT_MAPPING_FILE="$1"
if [[ ! -f "$PROJECT_MAPPING_FILE" ]]; then
log_message "Info: Looks like you don't have any '$PROJECT_MAPPING_FILE' configured. Please run './mrun.sh init' first." true true false
exit 1
fi
if ! jq -e . "$PROJECT_MAPPING_FILE" > /dev/null 2>&1; then
log_message "Error: Invalid JSON format in '$PROJECT_MAPPING_FILE'. Please re-configure '$PROJECT_MAPPING_FILE' first." true true false
exit 1
fi
}
normalize_directory_path() {
PATH_TO_CHECK="$1"
if [[ "$PATH_TO_CHECK" == /* ]]; then
echo "$PATH_TO_CHECK" | sed "s|^/||"
else
echo "$PATH_TO_CHECK"
fi
}
project_file_entries_action() {
PROJECT_MAPPING_FILE="$1"
FILE_ACTION="$2"
if [[ $FILE_ACTION == "create" ]]; then
# $3 -> NEW_PROJECT_ID
# $4 -> NEW_PROJECT_DIR
jq --arg id "$3" --arg dir "$4" \
'. + {($id): $dir}' "$PROJECT_MAPPING_FILE" > temp.mrun.json && mv temp.mrun.json "$PROJECT_MAPPING_FILE"
elif [[ $FILE_ACTION == "read" ]]; then
# $3 -> WITH_DIR_ACTION
if [[ $3 == "all" ]]; then
jq -r 'to_entries | .[] | "* \(.key) -> \(.value)"' "$PROJECT_MAPPING_FILE"
else
jq -r 'keys[] | "* \(.)"' "$PROJECT_MAPPING_FILE"
fi
elif [[ $FILE_ACTION == "update" ]]; then
# $3 -> UPDATE_FIELD_TYPE
if [[ $3 == "id" ]]; then
# $4 -> OLD_PROJECT_ID
# $5 -> NEW_PROJECT_ID
jq --arg old_id "$4" --arg new_id "$5" \
'if .[$old_id] then .[$new_id] = .[$old_id] | del(.[$old_id]) else . end' "$PROJECT_MAPPING_FILE" > temp.json && mv temp.json "$PROJECT_MAPPING_FILE"
elif [[ $3 == "dir" ]]; then
# $4 -> PROJECT_ID
# $5 -> NEW_PROJECT_DIR
jq --arg id "$4" --arg dir "$5" \
'.[$id] = $dir' "$PROJECT_MAPPING_FILE" > temp.mrun.json && mv temp.mrun.json "$PROJECT_MAPPING_FILE"
fi
elif [[ $FILE_ACTION == "delete" ]]; then
# $3 -> PROJECT_ID_TO_DELETE
jq "del(.\"$3\")" "$PROJECT_MAPPING_FILE" > temp.mrun.json && mv temp.mrun.json "$PROJECT_MAPPING_FILE"
fi
}
create_project() {
PROJECT_MAPPING_FILE="$1"
read -p "Enter the new project ID: " NEW_PROJECT_ID
check_empty_input "$NEW_PROJECT_ID"
validate_project_id_unique "$PROJECT_MAPPING_FILE" "$NEW_PROJECT_ID"
read -p "Enter the directory for the new project: " NEW_PROJECT_DIR
check_empty_input "$NEW_PROJECT_DIR"
NEW_PROJECT_DIR=$(normalize_directory_path "$NEW_PROJECT_DIR")
validate_directory_unique "$PROJECT_MAPPING_FILE" "$NEW_PROJECT_DIR"
project_file_entries_action "$PROJECT_MAPPING_FILE" "create" "$NEW_PROJECT_ID" "$NEW_PROJECT_DIR"
log_message "Info: Project '$NEW_PROJECT_ID' added successfully." true true false
}
read_projects() {
PROJECT_MAPPING_FILE="$1"
echo -e "Here are all the available projects in '$PROJECT_MAPPING_FILE':"
project_file_entries_action $PROJECT_MAPPING_FILE "read"
}
read_projects_with_dirs() {
PROJECT_MAPPING_FILE="$1"
echo -e "Here are all the available projects with their directories in '$PROJECT_MAPPING_FILE':"
project_file_entries_action "$PROJECT_MAPPING_FILE" "read" "all"
}
update_project() {
PROJECT_MAPPING_FILE="$1"
read -p "Enter the project ID to update: " UPDATE_PROJECT_ID
check_empty_input "$UPDATE_PROJECT_ID"
if ! jq -e ".\"$UPDATE_PROJECT_ID\"" "$PROJECT_MAPPING_FILE" > /dev/null; then
log_message "Error: Project ID '$UPDATE_PROJECT_ID' not found!" true true false
exit 1
fi
read -p "Update project ID (leave blank if unchanged): " NEW_PROJECT_ID
if [[ -n "$NEW_PROJECT_ID" ]]; then
validate_project_id_unique "$PROJECT_MAPPING_FILE" "$NEW_PROJECT_ID"
project_file_entries_action "$PROJECT_MAPPING_FILE" "update" "id" "$UPDATE_PROJECT_ID" "$NEW_PROJECT_ID"
log_message "Info: Project ID '$UPDATE_PROJECT_ID' updated to '$NEW_PROJECT_ID'." true false false
UPDATE_PROJECT_ID="$NEW_PROJECT_ID"
fi
read -p "Update directory (leave blank if unchanged): " UPDATE_PROJECT_DIR
if [[ -n "$UPDATE_PROJECT_DIR" ]]; then
UPDATE_PROJECT_DIR=$(normalize_directory_path "$UPDATE_PROJECT_DIR")
validate_directory_unique "$PROJECT_MAPPING_FILE" "$UPDATE_PROJECT_DIR"
project_file_entries_action "$PROJECT_MAPPING_FILE" "update" "dir" "$UPDATE_PROJECT_ID" "$UPDATE_PROJECT_DIR"
log_message "Info: Directory of project ID '$UPDATE_PROJECT_ID' updated to '$UPDATE_PROJECT_DIR'." true false false
fi
if [[ -z "$NEW_PROJECT_ID" && -z "$UPDATE_PROJECT_DIR" ]]; then
log_message "Info: No changes were made. Nothing to update." false true false
else
log_message "Info: Updated successfully." false true false
fi
}
delete_project() {
PROJECT_MAPPING_FILE="$1"
read -p "Enter the project ID to delete: " DELETE_PROJECT_ID
check_empty_input "$DELETE_PROJECT_ID"
if ! jq -e ".\"$DELETE_PROJECT_ID\"" "$PROJECT_MAPPING_FILE" > /dev/null; then
log_message "Error: Project ID '$DELETE_PROJECT_ID' not found!" true true false
exit 1
fi
project_file_entries_action $PROJECT_MAPPING_FILE "delete" "$DELETE_PROJECT_ID"
log_message "Info: Project ID '$DELETE_PROJECT_ID' deleted successfully." true true false
}
check_no_projects_configured() {
PROJECT_MAPPING_FILE="$1"
if [[ ! -s "$PROJECT_MAPPING_FILE" || "$(jq 'length' "$PROJECT_MAPPING_FILE")" -eq 0 ]]; then
log_message "Info: You need to add at least one project in '$PROJECT_MAPPING_FILE'. Let's do that now." false true false
if [[ ! -s "$PROJECT_MAPPING_FILE" || ! $(jq empty "$PROJECT_MAPPING_FILE" 2>/dev/null) ]]; then
echo "{}" > "$PROJECT_MAPPING_FILE"
fi
create_project "$PROJECT_MAPPING_FILE"
fi
}
run_command() {
PROJECT_MAPPING_FILE="$1"
TYPE_OS="$2"
COMMAND_TO_RUN="$3"
TARGETED_PROJECT_ID="$4"
ITS_DIR="$5"
if [[ -n "$TARGETED_PROJECT_ID" ]]; then
ITS_DIR=$(jq -r ".\"$TARGETED_PROJECT_ID\"" "$PROJECT_MAPPING_FILE")
if [[ "$ITS_DIR" == "null" || -z "$ITS_DIR" ]]; then
log_message "Error: Project ID '$TARGETED_PROJECT_ID' not found in the mapping!" true true false
exit 1
fi
fi
ITS_DIR_PATH=""
if [[ "$TYPE_OS" == "Windows" ]]; then
ITS_DIR_PATH=$(cygpath -w "$ITS_DIR")
else
ITS_DIR_PATH=$(realpath "$ITS_DIR")
fi
validate_directory "$ITS_DIR_PATH"
log_message "Info: Running command '$COMMAND_TO_RUN' in $TARGETED_PROJECT_ID -> $ITS_DIR" true true false
cd "$ITS_DIR" && eval "$COMMAND_TO_RUN"
EXIT_STATUS=$?
if [[ $EXIT_STATUS -ne 0 ]]; then
log_message "Error: Command '$COMMAND_TO_RUN' failed with status code $EXIT_STATUS." true true false
exit $EXIT_STATUS
fi
}
initialize_mapping_file() {
PROJECT_MAPPING_FILE="$1"
if [[ -f "$PROJECT_MAPPING_FILE" ]]; then
read -p "Looks like '$PROJECT_MAPPING_FILE' already exists. Do you want to reinitialize it and remove all project entries? [y/n]: " reinit_choice
check_empty_input "$reinit_choice"
if [[ "$reinit_choice" == "y" || "$reinit_choice" == "Y" ]]; then
echo -e "{}" > "$PROJECT_MAPPING_FILE"
log_message "Info: '$PROJECT_MAPPING_FILE' has been reinitialized." true true false
create_project "$PROJECT_MAPPING_FILE"
else
log_message "Info: Skipping reinitialization of '$PROJECT_MAPPING_FILE'." false true false
fi
else
log_message "Info: '$PROJECT_MAPPING_FILE' not found. Initializing it now..." false true false
echo -e "{}" > "$PROJECT_MAPPING_FILE"
log_message "Info: New '$PROJECT_MAPPING_FILE' file created." true true false
create_project "$PROJECT_MAPPING_FILE"
fi
}
detect_os
check_dependencies "$OS_TYPE"
if [[ "$1" == "init" ]]; then
initialize_mapping_file "$PROJECT_MAP_FILE"
exit 0
fi
validate_mapping_file "$PROJECT_MAP_FILE"
check_no_projects_configured "$PROJECT_MAP_FILE"
while getopts ":p:c:lhdaru" opt; do
case ${opt} in
p)
validate_project_id "$OPTARG"
PROJECT_ID=$OPTARG
;;
c)
sanitize_command "$OPTARG"
COMMAND=$OPTARG
;;
a)
ACTION="create"
;;
l)
read_projects "$PROJECT_MAP_FILE"
exit 0
;;
d)
read_projects_with_dirs "$PROJECT_MAP_FILE"
exit 0
;;
u)
ACTION="update"
;;
r)
ACTION="delete"
;;
h)
help_message
;;
\?)
usage
;;
esac
done
if [[ -z "$PROJECT_ID" || -z "$COMMAND" ]] && [[ -z "$ACTION" ]]; then
usage
fi
if [[ "$ACTION" == "create" ]]; then
create_project $PROJECT_MAP_FILE
exit 0
elif [[ "$ACTION" == "delete" ]]; then
delete_project $PROJECT_MAP_FILE
exit 0
elif [[ "$ACTION" == "update" ]]; then
update_project $PROJECT_MAP_FILE
exit 0
fi
run_command $PROJECT_MAP_FILE $OS_TYPE $COMMAND $PROJECT_ID $TARGET_DIR