-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopulate-mock-api.sh
executable file
·76 lines (60 loc) · 1.66 KB
/
populate-mock-api.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
#! /bin/sh
API_URL=https://lexica.github.io/lexica/api/v1
MOCK_PATH=./mock-api/lexica/api/v1
populate_base_languages_file() {
LANGUAGE_FILE=$MOCK_PATH/languages.json
if [ -f $LANGUAGE_FILE ]; then
echo $LANGUAGE_FILE exists!
return
fi
curl -s "$API_URL/languages.json" > $LANGUAGE_FILE
}
get_available_languages() {
# We can reasonably rely on people having nodejs, but jq would be better here
cat $MOCK_PATH/languages.json |
node -e "const fs = require('fs'); JSON.parse(fs.readFileSync(0).toString()).forEach(w => console.log(w))"
}
print_available_languages() {
for language in $AVAILABLE_LANGUAGES
do
echo $language
done
}
mkdir -p $MOCK_PATH
populate_base_languages_file
AVAILABLE_LANGUAGES=`get_available_languages`
# echo $AVAILABLE_LANGUAGES
populate_language() {
echo "populating $1..."
mkdir -p $MOCK_PATH/language/$1
curl -s "$API_URL/language/$1/metadata.json">$MOCK_PATH/language/$1/metadata.json
curl -s "$API_URL/language/$1/dictionary.json">$MOCK_PATH/language/$1/dictionary.json
echo "done"
}
if [ -z "$1" ]
then
echo "Language(s) needed. (or enter all to populate all languages)"
echo "./populate-mock-api.sh ['all' or language] [language]..."
echo "Valid languages:"
print_available_languages
exit 1
fi
mkdir -p $MOCK_PATH
if [ "all" = "$1" ]
then
print_available_languages |
while IFS= read -r language; do
populate_language $language
done
exit 0
fi
invalid_language() {
echo "WARNING: $1 is not a valid language. Exiting."
exit 1
}
for var in $@
do
echo validating $var...
print_available_languages | grep -Eq ^$var$ && echo valid || invalid_language $var
populate_language $var
done