Skip to content

Commit e92c7e2

Browse files
committed
feat(converter): helper script
1 parent 248fdf7 commit e92c7e2

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

converter/scripts/call_convert.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
5+
ENVELOPE_DEFAULT="$SCRIPT_DIR/../tests/fixtures/EDXL/edxl_envelope_health_to_health.json"
6+
CONVERTER_URL_DEFAULT="http://localhost:8083"
7+
8+
usage() {
9+
cat >&2 <<EOF
10+
Usage: $(basename "$0") <sourceVersion> <targetVersion> <type> <useCase> [options]
11+
12+
Positional arguments:
13+
sourceVersion e.g. v1, v2, v3, vactive (passed as-is)
14+
targetVersion e.g. v1, v2, v3, vactive (passed as-is)
15+
type HealthVersionConversion | CISUTranscoding | CISUVersionConversion
16+
useCase path to a JSON file OR http(s) URL of the message payload
17+
18+
Options:
19+
--envelope <path|url> EDXL envelope file or URL
20+
(default: tests/fixtures/EDXL/edxl_envelope_health_to_health.json)
21+
--converter_url <url> Converter base URL (default: http://localhost:8083)
22+
-h, --help Show this message
23+
24+
Example:
25+
$(basename "$0") v3 v2 HealthVersionConversion tests/fixtures/RS-RI/RS-RI_V3.0_exhaustive_fill.json
26+
EOF
27+
}
28+
29+
ENVELOPE="$ENVELOPE_DEFAULT"
30+
CONVERTER_URL="$CONVERTER_URL_DEFAULT"
31+
POSITIONAL=()
32+
33+
while [[ $# -gt 0 ]]; do
34+
case "$1" in
35+
--envelope)
36+
[[ $# -ge 2 ]] || { echo "Error: --envelope requires a value" >&2; usage; exit 1; }
37+
ENVELOPE="$2"; shift 2;;
38+
--converter_url)
39+
[[ $# -ge 2 ]] || { echo "Error: --converter_url requires a value" >&2; usage; exit 1; }
40+
CONVERTER_URL="$2"; shift 2;;
41+
-h|--help)
42+
usage; exit 0;;
43+
--*)
44+
echo "Error: unknown flag: $1" >&2; usage; exit 1;;
45+
*)
46+
POSITIONAL+=("$1"); shift;;
47+
esac
48+
done
49+
50+
if [[ ${#POSITIONAL[@]} -ne 4 ]]; then
51+
echo "Error: expected 4 positional arguments, got ${#POSITIONAL[@]}" >&2
52+
usage
53+
exit 1
54+
fi
55+
56+
SOURCE_VERSION="${POSITIONAL[0]}"
57+
TARGET_VERSION="${POSITIONAL[1]}"
58+
TYPE="${POSITIONAL[2]}"
59+
USECASE="${POSITIONAL[3]}"
60+
61+
for dep in jq curl; do
62+
if ! command -v "$dep" >/dev/null 2>&1; then
63+
echo "Error: missing required dependency: $dep" >&2
64+
exit 1
65+
fi
66+
done
67+
68+
read_source() {
69+
local src="$1"
70+
local label="$2"
71+
if [[ "$src" =~ ^https?:// ]]; then
72+
if ! curl -fsSL "$src"; then
73+
echo "Error: failed to download $label from $src" >&2
74+
return 1
75+
fi
76+
else
77+
if [[ ! -f "$src" ]]; then
78+
echo "Error: $label file not found: $src" >&2
79+
return 1
80+
fi
81+
cat "$src"
82+
fi
83+
}
84+
85+
ENVELOPE_JSON=$(read_source "$ENVELOPE" "envelope") || exit 1
86+
USECASE_JSON=$(read_source "$USECASE" "useCase") || exit 1
87+
88+
PAYLOAD=$(jq -n \
89+
--argjson envelope "$ENVELOPE_JSON" \
90+
--argjson usecase "$USECASE_JSON" \
91+
--arg sourceVersion "$SOURCE_VERSION" \
92+
--arg targetVersion "$TARGET_VERSION" \
93+
--arg type "$TYPE" \
94+
'{
95+
sourceVersion: $sourceVersion,
96+
targetVersion: $targetVersion,
97+
type: $type,
98+
edxl: ($envelope.edxl | .content[0].jsonContent.embeddedJsonContent.message |= (. + $usecase))
99+
}')
100+
101+
RESPONSE=$(curl -sS -X POST "$CONVERTER_URL/convert" \
102+
-H "Content-Type: application/json" \
103+
-d "$PAYLOAD")
104+
105+
if printf '%s' "$RESPONSE" | jq . >/dev/null 2>&1; then
106+
printf '%s' "$RESPONSE" | jq .
107+
else
108+
printf '%s\n' "$RESPONSE"
109+
fi

0 commit comments

Comments
 (0)