Skip to content

Commit 7c20cdd

Browse files
committed
Refactor ytox function to improve XML generation and error handling
1 parent 9a5cde7 commit 7c20cdd

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/lib/ytoxt.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
#!/bin/bash
22

33
ytox() {
4-
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml>$(yq -ox -I0 "$1" | sed 's/"/\\"/g')</xml>" >"${1%.*}.xml" 2>/dev/null
5-
stat -c %s "${1%.*}.xml" || echo -1
4+
local source_file
5+
local normalized_file
6+
local normalized_tmp=""
7+
local xml_body
8+
9+
source_file="$1"
10+
normalized_file="$source_file"
11+
12+
if jq -e 'type == "array"' "$source_file" >/dev/null 2>&1; then
13+
normalized_tmp=$(mktemp "${TMPDIR:-/tmp}/ytoxt.XXXXXX") || return 1
14+
jq -c '{"package": .}' "$source_file" >"$normalized_tmp" || {
15+
rm -f "$normalized_tmp"
16+
return 1
17+
}
18+
normalized_file="$normalized_tmp"
19+
fi
20+
21+
xml_body=$(yq -ox -I0 "$normalized_file" 2>/dev/null) || {
22+
[ -n "$normalized_tmp" ] && rm -f "$normalized_tmp"
23+
return 1
24+
}
25+
26+
[ -n "$normalized_tmp" ] && rm -f "$normalized_tmp"
27+
printf '<?xml version="1.0" encoding="UTF-8"?><xml>%s</xml>' "$(printf '%s' "$xml_body" | sed 's/"/\\"/g')" >"${source_file%.*}.xml" 2>/dev/null
28+
stat -c %s "${source_file%.*}.xml" || echo -1
629
}
730

831
# ytox + trim: if the json or xml is over 50MB, remove oldest versions

0 commit comments

Comments
 (0)