Skip to content

Commit 001104d

Browse files
committed
NO-JIRA: feat(ci/check-json.sh): make the script macOS compatible
1 parent bba58b3 commit 001104d

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

ci/check-json.sh

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
#
33
# This script serves to check YAML files in this repository that contain particular
44
# key fields where JSON string is expected. Such JSON strings are extracted and
@@ -10,7 +10,19 @@
1010
# In case of the PR on GitHub, this check is tied to GitHub actions automatically,
1111
# see `.github/workflows` directory.
1212

13-
shopt -s globstar
13+
if ! shopt -s globstar; then
14+
echo "macOS ships bash-3.2 that does not know shopt -s globstar; install newer bash from homebrew"
15+
exit 1
16+
fi
17+
18+
# yq: `brew install yq` or `apt-get install yq`
19+
# json_verify: `brew install yajl` or `apt-get install yajl-tools`
20+
for dep in yq json_verify; do
21+
if ! which -- ${dep} >/dev/null; then
22+
echo "the dependency ${dep} is not installed; install it now"
23+
exit 1
24+
fi
25+
done
1426

1527
function check_json() {
1628
local f="${1}"
@@ -29,8 +41,9 @@ function check_json() {
2941
echo " ${json}"
3042
echo -n " > "; echo "${json}" | json_verify || ret_code="${?}"
3143
done <<< "${jsons}"
44+
tmp_dir=$(mktemp --directory -t=check-jsons-in-file-)
3245
else
33-
echo " Ignoring as this file doesn't contain necessary key field '${string}' for check"
46+
echo " Ignoring as this file doesn't contain necessary key field '${string}' for check"
3447
fi
3548

3649
return "${ret_code}"
@@ -48,20 +61,27 @@ function split_yaml_file() {
4861
return 0
4962
}
5063

51-
ret_code=0
64+
function main() {
65+
local ret_code=0
5266

67+
# Some yaml files can contain more definitions.
68+
# This is a problem for `yq` tool so we need to split these into separate files.
69+
local tmp_dir
70+
tmp_dir=$(mktemp --directory -t check-json-XXXXXXXXXX-)
71+
for f in **/*.yaml; do
72+
echo "Splitting the '${f}' file."
73+
split_yaml_file "${f}" "${tmp_dir}" || ret_code="${?}"
74+
done
5375

54-
# Some yaml files can contain more definitions.
55-
# This is a problem for `yq` tool so we need to split these into separate files.
56-
tmp_dir=$(mktemp --directory --suffix=-check-json)
57-
for f in **/*.yaml; do
58-
echo "Splitting the '${f}' file."
59-
split_yaml_file "${f}" "${tmp_dir}" || ret_code="${?}"
60-
done
76+
for f in "${tmp_dir}"/*; do
77+
check_json "${f}" "opendatahub.io/notebook-software" || ret_code="${?}"
78+
check_json "${f}" "opendatahub.io/notebook-python-dependencies" || ret_code="${?}"
79+
done
6180

62-
for f in "${tmp_dir}"/*; do
63-
check_json "${f}" "opendatahub.io/notebook-software" || ret_code="${?}"
64-
check_json "${f}" "opendatahub.io/notebook-python-dependencies" || ret_code="${?}"
65-
done
81+
exit "${ret_code}"
82+
}
6683

67-
exit "${ret_code}"
84+
# allows sourcing the script into interactive session without executing it
85+
if [[ "${0}" == "${BASH_SOURCE[0]}" ]]; then
86+
main
87+
fi

0 commit comments

Comments
 (0)