-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf_parser.sh
executable file
·100 lines (88 loc) · 2.24 KB
/
conf_parser.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
#!/bin/bash
# This table will contain our conf values on a tab_conf["key"]=value basis
declare -A tab_conf
# add an already validated line to tab_conf
function add_line_to_tab_conf()
{
if [ $# -ne 1 ]
then
printf "Usage: add_line_to_tab_conf line_to_add\n"
return 1
fi
line_to_add=$1
key=$(echo $line_to_add | cut -f1 -d=)
value=$(echo $line_to_add | cut -f2 -d=)
tab_conf["$key"]="$value"
}
# Print the string in red
function print_error()
{
if [ $# -ne 1 ]
then
printf "Usage: print_error string\n"
return 1
fi
printf "\033[01;31m%s\033[00m\n" "$1"
}
# Validate a line and pass it to add_line_to_tab_conf()
function parse_line()
{
if [ $# -ne 2 ]
then
printf "Usage: parse_line line_to_parse line_number\n"
return 1
fi
comment_regex='^[[:space:]]*#'
empty_regex="^$"
valid_regex="^[0-9a-zA-Z_]+=[-0-9a-zA-Z\s\_\/]+"
line=$1
line_number=$2
if ! [[ $line =~ $empty_regex ]]
then
if ! [[ $line =~ $comment_regex ]]
then
if [[ $line =~ $valid_regex ]]
then
add_line_to_tab_conf "$line"
else
string=$(printf "Invalid configuration syntax l%d: %s" "$line_number" "$line")
print_error "$string"
fi
fi
fi
return 0
}
# Validate that all mandatory variables are set
function validate_mandatory_variables()
{
#local abort_exec=0
for key in "${tab_mandatory[@]}"
do
if [ -z "${tab_conf["$key"]}" ]
then
string=$(printf "%s key is missing, please add a value to this configuration key like so: %s=<VALUE>" "$key" "$key")
print_error "$string"
abort_exec=true
fi
done
if [ "$abort_exec" == true ]
then
exit 1
fi
}
# Read the config file line by line and pass them to other functions
function parse_config_file()
{
if [ $# -ne 1 ]
then
printf "Usage: parse_line config_file_path\n"
return 1
fi
config_file_path=$1
line_number=1
while IFS="" read -r line || [ -n "$line" ]
do
parse_line "$line" $line_number
line_number=$((line_number + 1))
done < $config_file_path
}