-
Notifications
You must be signed in to change notification settings - Fork 20
/
runhaz.sh
executable file
·157 lines (143 loc) · 4.2 KB
/
runhaz.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
#!/bin/bash
base_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
server_dir="$base_dir/server"
# check python is installed
if command -v python3 > /dev/null 2>&1; then
python_cmd=$(command -v python3)
base_py_cmd="$python_cmd $server_dir/manage.py "
elif command -v python > /dev/null 2>&1; then
python_cmd=$(command -v python)
base_py_cmd="$python_cmd $server_dir/manage.py "
else
echo "Python3 not found"
exit 1
fi
# check python version is at least 3.8
ver=$($python_cmd -V 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/')
if [ "$ver" -eq "31" ]; then
:
elif [ "$ver" -lt "38" ]; then
echo "Python version 3.8 or greater is expected, received $ver"
exit 1
fi
print_usage() {
# Display help
echo "Command line utility to help develop Haztrak"
echo
echo "Syntax: $(basename "$0") <option>"
echo "options:"
echo "-d, --db Bring up the local development database and expose it on port 5432"
echo "-l, --load load initial database data from fixture files"
echo "-p, --pre-commit installs hooks, if necessary, and runs pre-commit run --all-files"
echo "-o, --openapi Generate the Open API Schema to /docs/API/"
echo "-e, --erd Graph the django models to an entity relationship diagram (ERD), requires graphviz"
echo "-c, --coverage Generate a coverage report for the python server"
echo "-h, --help Print this help message"
echo
}
# prints colored text
print_style() {
if [ "$2" == "info" ] ; then
COLOR="96m";
elif [ "$2" == "success" ] ; then
COLOR="92m";
elif [ "$2" == "warning" ] ; then
COLOR="93m";
elif [ "$2" == "danger" ] ; then
COLOR="91m";
else #default color
COLOR="0m";
fi
START_COLOR="\e[$COLOR";
END_COLOR="\e[0m";
printf "$START_COLOR%b$END_COLOR" "$1";
}
start_db(){
echo "starting database..."
# check if docker is installed
if command -v docker> /dev/null 2>&1; then
docker_exec=$(command -v docker)
else
print_style "Docker not found" "danger"
exit 1
fi
eval "$docker_exec compose -f $base_dir/docker-compose.yaml --env-file $base_dir/configs/.env.dev start postgres"
exit 0
}
load_django_fixtures() {
# load initial data, good if you need to drop the dev database
fixtures_dir="$server_dir/fixtures"
exec_cmd="$base_py_cmd loaddata $fixtures_dir/dev_data.yaml"
eval "$exec_cmd"
exit
}
generate_api_schema() {
print_style "Generating Open API schema...\n" "success";
exec_cmd="$base_py_cmd spectacular --settings haztrak.settings --file $base_dir/docs/api/postman/schemas/openapi-schema.yaml"
eval "$exec_cmd"
exit
}
graph_models() {
print_style "Generating Entity Relationship Diagram...\n" "success";
exec_cmd="$base_py_cmd graph_models rcrasite handler core org wasteline manifest site profile\
-g \
--settings haztrak.settings \
--rankdir=RL \
--arrow-shape=normal \
--exclude-models=*BaseModel \
--disable-abstract-fields \
-o $base_dir/docs/guide/src/assets/images/erd.png"
eval "$exec_cmd"
exit
}
run_pre_commit() {
# This will install the pre-commit script, and run whatever pre-commit
# hooks we have configured. Great to run before committing as a pre-commit hook
# failure will abort the commit
if command -V pre-commit > /dev/null 2>&1 ; then
eval "pre-commit install"
eval "pre-commit run --all-files"
exit
else
echo "pre-commit not found, did you forget to activate your virtualenv?"
exit 1
fi
}
run_coverage() {
# Run the test suite with coverage
(cd "$base_dir"/server || exit; eval "coverage run")
(cd "$base_dir"/server || exit; eval "coverage report")
exit
}
# Parse CLI argument
while [[ $# -gt 0 ]]; do
case $1 in
-d|--db)
start_db
;;
-l|--load)
load_django_fixtures
;;
-p|--pre-commit)
run_pre_commit
;;
-o|--openapi)
generate_api_schema
;;
-e|--erd)
graph_models
;;
-c|--coverage)
run_coverage
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "Unknown option $1"
print_usage
exit 1
;;
esac
done