1- #! /bin/bash
1+ #! /usr/ bin/env bash
22# A collection of useful assertions. Each one checks a condition and if the condition is not satisfied, exits the
33# program. This is useful for defensive programming.
44
5-
6- set -e
7-
5+ # shellcheck source=./modules/bash-commons/src/log.sh
86source " $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) /log.sh"
7+ # shellcheck source=./modules/bash-commons/src/array.sh
98source " $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) /array.sh"
9+ # shellcheck source=./modules/bash-commons/src/string.sh
1010source " $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) /string.sh"
11+ # shellcheck source=./modules/bash-commons/src/os.sh
1112source " $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) /os.sh"
1213
1314# Check that the given binary is available on the PATH. If it's not, exit with an error.
1415function assert_is_installed {
15- local readonly name=" $1 "
16+ local -r name=" $1 "
1617
1718 if ! os_command_is_installed " $name " ; then
1819 log_error " The command '$name ' is required by this script but is not installed or in the system's PATH."
@@ -22,9 +23,9 @@ function assert_is_installed {
2223
2324# Check that the value of the given arg is not empty. If it is, exit with an error.
2425function assert_not_empty {
25- local readonly arg_name=" $1 "
26- local readonly arg_value=" $2 "
27- local readonly reason=" $3 "
26+ local -r arg_name=" $1 "
27+ local -r arg_value=" $2 "
28+ local -r reason=" $3 "
2829
2930 if [[ -z " $arg_value " ]]; then
3031 log_error " The value for '$arg_name ' cannot be empty. $reason "
@@ -34,9 +35,9 @@ function assert_not_empty {
3435
3536# Check that the value of the given arg is empty. If it isn't, exit with an error.
3637function assert_empty {
37- local readonly arg_name=" $1 "
38- local readonly arg_value=" $2 "
39- local readonly reason=" $3 "
38+ local -r arg_name=" $1 "
39+ local -r arg_value=" $2 "
40+ local -r reason=" $3 "
4041
4142 if [[ ! -z " $arg_value " ]]; then
4243 log_error " The value for '$arg_name ' must be empty. $reason "
@@ -47,8 +48,8 @@ function assert_empty {
4748# Check that the given response from AWS is not empty or null (the null often comes from trying to parse AWS responses
4849# with jq). If it is, exit with an error.
4950function assert_not_empty_or_null {
50- local readonly response=" $1 "
51- local readonly description=" $2 "
51+ local -r response=" $1 "
52+ local -r description=" $2 "
5253
5354 if string_is_empty_or_null " $response " ; then
5455 log_error " Got empty response for $description "
@@ -58,10 +59,10 @@ function assert_not_empty_or_null {
5859
5960# Check that the given value is one of the values from the given list. If not, exit with an error.
6061function assert_value_in_list {
61- local readonly arg_name=" $1 "
62- local readonly arg_value=" $2 "
62+ local -r arg_name=" $1 "
63+ local -r arg_value=" $2 "
6364 shift 2
64- local readonly list=(" $@ " )
65+ local -ar list=(" $@ " )
6566
6667 if ! array_contains " $arg_value " " ${list[@]} " ; then
6768 log_error " '$arg_value ' is not a valid value for $arg_name . Must be one of: [${list[@]} ]."
0 commit comments