forked from bderenzo/tinystatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanned_maintenance.sh
executable file
·71 lines (59 loc) · 1.66 KB
/
planned_maintenance.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
#!/usr/bin/env bash
PLANNED_MAINTENANCE_FILE="planned_maintenance.csv"
LAST_UPDATED=$(date +"%Y-%m-%dT%H:%M:%S%z")
command_exists() {
if ! command -v "${1}" >/dev/null 2>&1; then
echo >&2 "Error: ${1} missing. Please install it"
exit 1
fi
}
trim_spaces() {
echo "$1" | sed 's/^[ \t]*//;s/[ \t]*$//'
}
json_output=$(cat <<EOF
{
"last_updated": "$LAST_UPDATED",
"planned_maintenance": []
}
EOF
)
convert_csv_to_json() {
local csv_file=$1
local json_array_name=$2
local json_objects=""
while IFS=',' read -r service date_time_from date_time_to impact; do
# Trim spaces
service=$(trim_spaces "$service")
date_time_from=$(trim_spaces "$date_time_from")
date_time_to=$(trim_spaces "$date_time_to")
impact=$(trim_spaces "$impact")
# Skip the line if any field is empty
if [ -z "$service" ] || [ -z "$date_time_from" ] || [ -z "$date_time_to" ] || [ -z "$impact" ]; then
continue
fi
# Create JSON object for each row
json_object=$(cat <<EOF
{
"service": "$service",
"date_time_from": "$date_time_from",
"date_time_to": "$date_time_to",
"impact": "$impact"
}
EOF
)
if [ -z "$json_objects" ]; then
json_objects="$json_object"
else
json_objects="$json_objects, $json_object"
fi
done < <(tail -n +1 "$csv_file")
# Add JSON objects to the main JSON structure
if [ -n "$json_objects" ]; then
json_output=$(echo "$json_output" | jq ".${json_array_name} = [$json_objects]")
fi
}
command_exists 'jq'
# Process the CSV files
convert_csv_to_json "$PLANNED_MAINTENANCE_FILE" "planned_maintenance"
# Output the final JSON structure
echo "$json_output"