-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall-fhir-packages.sh
executable file
·186 lines (144 loc) · 5.4 KB
/
install-fhir-packages.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
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
#!/bin/bash
exit_with_message () {
echo $1 >&2
exit 1
}
check_availability_metadata () {
echo -e "\nChecking if $fhirServer/metadata can be reached"
endpointSucccess=$(curl -sL -w '%{http_code}' "$fhirServer/metadata" -o /dev/null)
if [ $endpointSucccess != "200" ]; then
echo "Could not reach FHIR server $fhirServer"
echo "Continue anyway (Y/n)?"
read forcePackageInstall
forcePackageInstall=${forcePackageInstall:-y}
forcePackageInstall=$(echo "$forcePackageInstall" | awk '{print tolower($0)}')
if [ "$forcePackageInstall" = "n" ] || [ "$forcePackageInstall" != "y" ]; then
exit_with_message "Exiting..."
fi
else
echo -e "\t Successfully reached $fhirServer"
fi
}
# ------
# Setup
# ------
for dependency in "jq" "fhir"
do
if ! [ -x "$(command -v $dependency)" ]; then
exit_with_message "$dependency is not installed."
fi
done
fhirCommand="fhir"
$fhirCommand login
echo -n "Use local file (local filename or leave empty for cloud package):"
read fileName
echo -n "FHIR package name: "
read packageName
if [ -z "$packageName" ]; then
exit_with_message "It's mandatory to specify the name of a FHIR package. Exiting..."
fi
if [ -z "$fileName" ]; then
echo -n "FHIR package version (latest): "
read packageVersion
rawOutputVersions=$($fhirCommand versions $packageName --raw)
if [ -z "$packageVersion" ]; then
packageVersion=$(echo $rawOutputVersions | jq -r '."dist-tags"."latest"')
fi
fhirVersion=$(echo $rawOutputVersions | jq -r ".versions.\"$packageVersion\".fhirVersion" | awk '{print tolower($0)}')
fhir spec $fhirVersion --project
echo -e "\nInstalling FHIR package '$packageName' using version $packageVersion"
if [ ! -f "package.json" ]; then
fhir init
fi
$fhirCommand install $packageName $packageVersion | awk '{ print "\t" $0 }'
else
echo -n "FHIR package version: "
read packageVersion
if [ -z "$packageVersion" ]; then
exit_with_message "It's mandatory to specify the version of the FHIR package. Exiting..."
fi
echo -n "FHIR version: "
read fhirVersion
if [ -z "$fhirVersion" ]; then
exit_with_message "It's mandatory to the FHIR version. Exiting..."
fi
fhir spec $fhirVersion --project
if [ ! -f "package.json" ]; then
fhir init
fi
echo -e "\nInstalling FHIR package '$fileName'"
$fhirCommand install $fileName --file | awk '{ print "\t" $0 }'
fi
echo -n "Upload package to FHIR server: "
read fhirServer
if [ -z "$fhirServer" ]; then
exit_with_message "It's mandatory to specify the endpoint of a FHIR server. Exiting..."
fi
$fhirCommand server add $fhirServer $fhirServer | awk '{ print "\t" $0 }'
check_availability_metadata
echo -e "\nChecking if FHIR '$packageName' using version $packageVersion was successfully installed"
$fhirCommand cache | grep -q $packageName#$packageVersion
if [ $? -eq 1 ]; then
$fhirCommand cache | grep -q $packageName@$packageVersion # Re-try with @ instead of # as the separator
fi
if [ $? -eq 1 ]; then
exit_with_message "Failed to install package $packageName using version $packageVersion"
else
echo -e "\t Successfully found package '$packageName' with version '$packageVersion' in cache"
fi
# ----------------------------
# Upload conformance resource
# ----------------------------
canonicals=$($fhirCommand canonicals $packageName $packageVersion)
for canonical in $canonicals; do
echo -e "\nTrying to resolve $canonical..."
$fhirCommand resolve $canonical | awk '{ print "\t" $0 }'
json=$($fhirCommand show --output json)
resourceType=$(echo $json | jq -r '.resourceType')
echo -e "\tUploading $canonical ($resourceType) to $fhirServer"
# Should we do a PUT or POST?
id=$(echo $json | jq -r -e '.id')
if [ $? = 1 ]; then
$fhirCommand post $fhirServer | awk '{ print "\t" $0 }'
else
$fhirCommand put $fhirServer | awk '{ print "\t" $0 }'
fi
done
echo "Successfully uploaded package '$packageName' using version $packageVersion to $fhirServer"
# ----------------
# Upload examples
# ----------------
echo -e "\nUpload example resources from FHIR package '$packageName' using version $packageVersion (Y/n)?"
read installExamples
echo -n "Upload examples to FHIR server: "
read fhirServer
if [ -z "$fhirServer" ]; then
exit_with_message "It's mandatory to specify the endpoint of a FHIR server. Exiting..."
fi
$fhirCommand server add $fhirServer $fhirServer | awk '{ print "\t" $0 }'
check_availability_metadata
installExamples=${installExamples:-y}
installExamples=$(echo "$installExamples" | awk '{print tolower($0)}')
if [ "$installExamples" = "n" ] || [ "$installExamples" != "y" ]; then
exit_with_message "Exiting..."
fi
currentWorkingDir=$(pwd)
fhirCacheLocation=$($fhirCommand cache --location)
cd $fhirCacheLocation
cd $packageName#$packageVersion/package/examples
$fhirCommand push .
while true; do
currentResource=$(fhir peek || true)
if [[ "$currentResource" == *"The stack is empty."* ]]; then
echo "Uploaded all example FHIR resources to $fhirServer"
break
fi
echo "Uploading $currentResource ..."
json=$($fhirCommand show --output json)
id=$(echo $json | jq -r -e '.id')
if [ $? = 1 ]; then
$fhirCommand post $fhirServer | awk '{ print "\t" $0 }'
else
$fhirCommand put $fhirServer | awk '{ print "\t" $0 }'
fi
done