-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendReports.sh
executable file
·46 lines (37 loc) · 1.31 KB
/
sendReports.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
#!/bin/bash
: ${MYSQL_HOST=openmrs-mysql-db}
: ${MYSQL_ROOT_PASSWORD=secret_password}
: ${SERVER_ADDRESS=localhost}
export MYSQL_HOST
export MYSQL_ROOT_PASSWORD
export SERVER_ADDRESS
USERNAME=admin;
POST_TEMPLATE=$(cat /postTemplate.json)
POST_TEMPLATE_NO_PERIODS=$(cat /postTemplateNoPeriods.json)
processFile() {
REPORT_CONTENT=$(envsubst "`printf '${%s} ' $(bash -c "compgen -A variable")`" < $1)
REPORT_CONTENT="${REPORT_CONTENT//\"/\\\"}"
REPORT_CONTENT=$(echo "$REPORT_CONTENT" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
REPORT_CONTENT=$(echo "$REPORT_CONTENT" | sed 's/\t/ /g')
REPORT_NAME=$(echo $2 | sed 's/\([A-Z][^A-Z]\)/ \1/g')
POST_CONTENT=$(printf "$3" "$REPORT_NAME" "$REPORT_CONTENT")
echo "$POST_CONTENT" > tmp_report_content.json
echo $'\n'$REPORT_NAME
curl -X POST -H "Content-Type: application/json" -u "$USERNAME:$DHIS_ADMIN_PASSWORD" \
"http://localhost:8080/api/reports" -d @tmp_report_content.json
rm tmp_report_content.json
}
for file in /reports/*; do
filename=$(basename "$file")
fname="${filename%.*}"
if [[ -f $file ]]; then
processFile $file $fname "$POST_TEMPLATE"
fi
done
for file in /reports/noPeriods/*; do
filename=$(basename "$file")
fname="${filename%.*}"
if [[ -f $file ]]; then
processFile $file $fname "$POST_TEMPLATE_NO_PERIODS"
fi
done