-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.sh
executable file
·101 lines (86 loc) · 2.72 KB
/
get.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
#!/bin/bash
# Function to retrieve the type of a variable
get_variable_type() {
local var_name="$1"
local var_type
var_type=$(grep "^__VAR__TYPE__${var_name}=" "$CONFIG_FILE" | cut -d'=' -f2)
#echo "Debug: Retrieved variable type for ${var_name}: ${var_type}" >&2
echo "$var_type"
}
# Function to retrieve a regular variable
get_regular_var() {
local var_name="$1"
local var_value
if grep -q "^${var_name}=" "$REGULAR_VARIABLES"; then
var_value=$(grep "^${var_name}=" "$REGULAR_VARIABLES" | cut -d'=' -f2-)
echo "$var_value"
return 0
else
echo ""
return 1
fi
}
get_array_var() {
local var_name="$1"
local var_value
if grep -q "^${var_name}=" "$ARRAY_VARIABLES"; then
var_value=$(grep "^${var_name}=" "$ARRAY_VARIABLES" | cut -d'=' -f2-)
var_value="${var_value:1:-1}" # Remove parentheses
# Properly quote each element without adding extra quotes
echo "$var_value" | sed -E 's/("[^"]+")/\1/g'
return 0
else
echo ""
return 1
fi
}
get_associative_array_var() {
local var_name="$1"
if grep -q "^${var_name}\[" "$ASSOCIATIVE_ARRAY_VARIABLES"; then
grep "^${var_name}\[" "$ASSOCIATIVE_ARRAY_VARIABLES" | while IFS='=' read -r key value; do
key=$(echo "$key" | sed -e "s/^${var_name}\[\"//" -e 's/\"\]$//')
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//')
echo "$key:$value"
done
return 0
else
echo ""
return 1
fi
}
# Main get function
get() {
local var_name="$1"
# Step 1: Check if the variable type is stored in the config file
local var_type
var_type=$(get_variable_type "$var_name")
# Remove quotes from the variable type
var_type="${var_type%\"}"
var_type="${var_type#\"}"
if [[ -z "$var_type" ]]; then
#echo "Debug: Variable type not found for ${var_name}" >&2
echo "" # Variable type not found
return 3
fi
#echo "Debug: Variable type for ${var_name} is ${var_type}" >&2
# Step 2: Retrieve the variable based on its type
case "$var_type" in
"regular")
get_regular_var "$var_name"
;;
"array")
get_array_var "$var_name"
#echo "Debug: Array type detected, but not yet implemented" >&2
;;
"associative_array")
get_associative_array_var "$var_name"
#echo "Debug: Associative array type detected, but not yet implemented" >&2
;;
*)
#echo "Debug: Unknown variable type for ${var_name}: ${var_type}" >&2
echo "" # Unknown variable type
return 1
;;
esac
return 0
}