-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.sh
More file actions
258 lines (213 loc) · 6.02 KB
/
library.sh
File metadata and controls
258 lines (213 loc) · 6.02 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# Shellib TOML Library - TOML parsing utilities
# Check if a section exists in TOML file
has_section() {
local section="$1"
local file="$2"
if [ ! -f "$file" ]; then
return 1
fi
# Trim trailing whitespace and match section
grep -q "^\[$section\][ ]*$" "$file" 2>/dev/null
}
# Get a value from a TOML section
get_value() {
local section="$1"
local key="$2"
local file="$3"
if [ ! -f "$file" ]; then
return 1
fi
# Use awk to parse the TOML section, handling trailing whitespace
awk -v section="$section" -v key="$key" '
$0 ~ "^\\[" section "\\][ ]*$" { in_section=1; next }
/^\[/ { in_section=0 }
in_section && $0 ~ "^" key " = " {
gsub(/^[^=]*= *"|"$/, "");
print;
exit
}
' "$file"
}
# Get all key-value pairs from a TOML section
get_section() {
local section="$1"
local file="$2"
if [ ! -f "$file" ]; then
return 1
fi
# Use awk to extract all key-value pairs from section
awk -v section="$section" '
$0 ~ "^\\[" section "\\][ ]*$" { in_section=1; next }
/^\[/ { in_section=0 }
in_section && /^[a-zA-Z_][a-zA-Z0-9_]* = / {
key = $1
gsub(/^[^=]*= *"|"$/, "")
print key "|" $0
}
' "$file"
}
# List all sections in a TOML file
list_sections() {
local file="$1"
if [ ! -f "$file" ]; then
return 1
fi
# Match both quoted and unquoted sections, handle trailing whitespace
grep "^\[.*\][ ]*$" "$file" | sed 's/^\[\(.*\)\][ ]*$/\1/'
}
# Test functions
test_has_section() {
local temp_file=$(mktemp)
cat > "$temp_file" << 'EOF'
[default]
method = "fallback"
["github.com"]
method = "https"
["gitlab.example.com/org/repo"]
method = "ssh"
username = "testuser"
EOF
# Test existing sections
if has_section "default" "$temp_file"; then
echo "✓ has_section finds default section"
else
echo "✗ has_section failed to find default section"
rm "$temp_file"
return 1
fi
if has_section "\"github.com\"" "$temp_file"; then
echo "✓ has_section finds quoted section"
else
echo "✗ has_section failed to find quoted section"
rm "$temp_file"
return 1
fi
# Test non-existing section
if ! has_section "nonexistent" "$temp_file"; then
echo "✓ has_section correctly rejects non-existing section"
else
echo "✗ has_section incorrectly found non-existing section"
rm "$temp_file"
return 1
fi
rm "$temp_file"
return 0
}
test_get_value() {
local temp_file=$(mktemp)
cat > "$temp_file" << 'EOF'
[default]
method = "fallback"
["github.com"]
method = "https"
["gitlab.example.com/org/repo"]
method = "ssh"
username = "testuser"
EOF
# Test getting values
local method
method=$(get_value "default" "method" "$temp_file")
if [ "${method:-}" = "fallback" ]; then
echo "✓ get_value gets default method"
else
echo "✗ get_value failed for default method, got: '$method'"
rm "$temp_file"
return 1
fi
local github_method
github_method=$(get_value "\"github.com\"" "method" "$temp_file")
if [ "${github_method:-}" = "https" ]; then
echo "✓ get_value gets quoted section method"
else
echo "✗ get_value failed for quoted section, got: '$github_method'"
rm "$temp_file"
return 1
fi
local username
username=$(get_value "\"gitlab.example.com/org/repo\"" "username" "$temp_file")
if [ "${username:-}" = "testuser" ]; then
echo "✓ get_value gets username"
else
echo "✗ get_value failed for username, got: '$username'"
rm "$temp_file"
return 1
fi
# Test non-existing key
local nonexistent
nonexistent=$(get_value "default" "nonexistent" "$temp_file")
if [ -z "${nonexistent:-}" ]; then
echo "✓ get_value returns empty for non-existing key"
else
echo "✗ get_value should return empty for non-existing key, got: '$nonexistent'"
rm "$temp_file"
return 1
fi
rm "$temp_file"
return 0
}
test_get_section() {
local temp_file=$(mktemp)
cat > "$temp_file" << 'EOF'
[default]
method = "fallback"
["gitlab.example.com/org/repo"]
method = "ssh"
username = "testuser"
EOF
# Test getting section
local section_data
section_data=$(get_section "\"gitlab.example.com/org/repo\"" "$temp_file")
if echo "${section_data:-}" | grep -q "method|ssh" && echo "${section_data:-}" | grep -q "username|testuser"; then
echo "✓ get_section gets all key-value pairs"
else
echo "✗ get_section failed, got: '$section_data'"
rm "$temp_file"
return 1
fi
rm "$temp_file"
return 0
}
test_list_sections() {
local temp_file=$(mktemp)
cat > "$temp_file" << 'EOF'
[default]
method = "fallback"
["github.com"]
method = "https"
["gitlab.example.com/org/repo"]
method = "ssh"
EOF
# Test listing sections
local sections
sections=$(list_sections "$temp_file")
if echo "${sections:-}" | grep -q "default" && echo "${sections:-}" | grep -q "\"github.com\"" && echo "${sections:-}" | grep -q "\"gitlab.example.com/org/repo\""; then
echo "✓ list_sections lists all sections"
else
echo "✗ list_sections failed, got: '$sections'"
rm "$temp_file"
return 1
fi
rm "$temp_file"
return 0
}
# Run all tests
run_tests() {
echo "Running tests for toml library..."
local failed=0
test_has_section || failed=1
test_get_value || failed=1
test_get_section || failed=1
test_list_sections || failed=1
if [ $failed -eq 0 ]; then
echo "All tests passed!"
return 0
else
echo "Some tests failed!"
return 1
fi
}
if [ "${1:-}" = "--test" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]; then
run_tests
exit $?
fi