forked from BugSwarm/bugswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
72 lines (57 loc) · 2.07 KB
/
common.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
#!/usr/bin/env bash
ANSI_RED="\033[31;1m"
ANSI_GREEN="\033[32;1m"
ANSI_RESET="\033[0m"
ANSI_CLEAR="\033[0K"
# Prints `message` in green in compatible terminals.
#
# Usage: print_green <message>
print_green () {
echo -e "\n${ANSI_GREEN}$1${ANSI_RESET}"
}
# Prints `message` in red in compatible terminals.
#
# Usage: print_red <message>
print_red () {
echo -e "\n${ANSI_RED}$1${ANSI_RESET}"
}
# Prints `error-message` in context and then exits with 1 if the previous command exited with a non-zero code.
#
# Usage: exit_if_failed <error-message>
exit_if_failed () {
if [ $? -ne 0 ]; then print_red "ERROR: $1 Exiting."; exit 1; fi
}
# Prints `done-message` in context. The text will appear green in compatible terminals.
# Use this function when a script completes successfully.
#
# Usage: print_done_message <done-message>
print_done_message () {
print_green "Done! $1"
}
# Prints a message indicating that the current stage in a script has completed successfully. The text will appear green
# in compatible terminals.
# Use this function when a stage in a script completes successfully.
#
# Usage: print_stage_done <stage-name>
print_stage_done () {
print_done_message "The $1 stage completed successfully."
}
# Checks if `project-directory` exists but does not determine whether it is actually a Git repository.
# Exits with 1 if `project-directory` does not exist.
#
# Usage: check_repo_exists <project-directory> <project-name>
check_repo_exists () {
if [ ! -d $1 ]; then
echo "$1 does not exist. Make sure you cloned $2 or specified a component directory."; exit 1;
fi
}
# Prints a message that gives context for the current step of the current stage in a script.
#
# After sourcing the script that contains this function, `current_step` is 1. Each call to this function increments
# `current_step`. Thus, the script that contains this function must be sourced again when a new stage begins.
#
# Usage: print_step <stage-name> <total-steps> <step-name>
current_step=1
print_step () {
print_green "$1 stage, step ${current_step} of $2: $3"; ((current_step++))
}