-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.sh
More file actions
executable file
·117 lines (99 loc) · 3.27 KB
/
assert.sh
File metadata and controls
executable file
·117 lines (99 loc) · 3.27 KB
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
#!/usr/bin/env bash
# A collection of useful assertions. Each one checks a condition and if the
# condition is not satisfied, exits the program. This is useful for defensive
# programming.
# shellcheck source=./log.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/log.sh"
# shellcheck source=./array.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/array.sh"
# shellcheck source=./string.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/string.sh"
# shellcheck source=./os.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/os.sh"
# Check that the given binary is available on the PATH. If it's not, exit with
# an error.
function assert_is_installed {
local -r name="$1"
if ! os_command_is_installed "$name"; then
log_error "The command '$name' is required by this script but is not installed or in the system's PATH."
exit 1
fi
}
# Check that the value of the given arg is not empty. If it is, exit with an
# error.
function assert_not_empty {
local -r arg_name="$1"
local -r arg_value="$2"
local -r reason="$3"
if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty. $reason"
exit 1
fi
}
# Check that the value of the given arg is empty. If it isn't, exit with an
# error.
function assert_empty {
local -r arg_name="$1"
local -r arg_value="$2"
local -r reason="$3"
if [[ ! -z "$arg_value" ]]; then
log_error "The value for '$arg_name' must be empty. $reason"
exit 1
fi
}
# Check that the given response from AWS is not empty or null (the null often
# comes from trying to parse AWS responses with jq). If it is, exit with an
# error.
function assert_not_empty_or_null {
local -r response="$1"
local -r description="$2"
if string_is_empty_or_null "$response"; then
log_error "Got empty response for $description"
exit 1
fi
}
# Check that the given value is one of the values from the given list. If not, exit with an error.
function assert_value_in_list {
local -r arg_name="$1"
local -r arg_value="$2"
shift 2
local -ar list=("$@")
if ! array_contains "$arg_value" "${list[@]}"; then
log_error "'$arg_value' is not a valid value for $arg_name. Must be one of: [${list[@]}]."
exit 1
fi
}
# Check that this script is running as root or sudo and exit with an error if it's not
function assert_uid_is_root_or_sudo {
if ! os_user_is_root_or_sudo; then
log_error "This script should be run using sudo or as the root user"
exit 1
fi
}
# Check that the path provided exsists
function assert_path_exists {
local -r arg_name="$1"
local -r arg_value="$2"
if [ ! -d "$arg_value" ]; then
log_error "The $arg_name provided does not exists."
exit 1
fi
}
# Check that the git repository url is valid
function assert_git_validity {
local -r arg_name="$1"
local -r arg_value="$2"
if [ "$(git ls-remote "$arg_value" > /dev/null 2>&1 && echo $?)" != 0 ]; then
log_error "The $arg_name Git repository URL '$arg_value' is invalid."
exit 1
fi
}
# Check that the date is properly formatted
function assert_date_formatting {
local -r arg_value="$1"
if [ "$(date +"%Y%m%d" -d "$arg_value" 2>/dev/null)" != "$arg_value" ]; then
EXAMPLE_DATE=$(date -d "1 day ago" +'%Y%m%d')
log_error "Incorrect date formatting: Ex. $EXAMPLE_DATE"
exit 1
fi
}