forked from emqx/emqx-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite-swagger.sh
executable file
·51 lines (40 loc) · 1.89 KB
/
rewrite-swagger.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
#!/usr/bin/env bash
## This script requres jq 1.6
set -euo pipefail
PROFILE="${1:-}"
SWAGGER_DOWNLOAD_DEFAULT_URI="http://localhost:18083/api-docs/swagger.json"
case "$PROFILE" in
ce)
TARGET_FILE='swagger/swagger.json'
;;
ee)
TARGET_FILE='swagger/swagger-ee.json'
;;
*)
echo "Usage $0 ce|ee [SWAGGER_INPUT]"
echo "The optional parameter SWAGGER_INPUT can be the generated json file"
echo "Otherwise it downloads the JSON file from $SWAGGER_DOWNLOAD_DEFAULT_URI"
exit 1
;;
esac
SWAGGER_INPUT="${2:-"$SWAGGER_DOWNLOAD_DEFAULT_URI"}"
## download swagger from EMQX api-docs
if [ -f "$SWAGGER_INPUT" ]; then
echo "Using swagger JSON file $SWAGGER_INPUT"
SWAGGER_INPUT_FILE="$SWAGGER_INPUT"
else
SWAGGER_INPUT_FILE='/tmp/swagger-filter-stage0.json'
echo "Downloading swagger JSON from $SWAGGER_INPUT"
curl "$SWAGGER_INPUT" > "$SWAGGER_INPUT_FILE"
fi
## change the deprecated endpoints to "__DEPRECATED__"
(jq 'walk( if type == "object" then ( if .deprecated == true then "__DEPRECATED__" else . end ) else . end)' > /tmp/swagger-filter-stage1.json)<"$SWAGGER_INPUT_FILE"
## if a path has all methods "__DEPRECATED__", change the path to "__DEPRECATED__"
(jq 'def all_deprecated: to_entries | length > 0 and all(.value == "__DEPRECATED__"); walk ( if type == "object" then ( if all_deprecated then "__DEPRECATED__" else . end ) else . end)' > /tmp/swagger-filter-stage2.json)</tmp/swagger-filter-stage1.json
## drop all "__DEPRECATED__" paths
## or single deprecated methods
(jq 'walk ( if type == "object" then with_entries(select(.value != "__DEPRECATED__")) else . end)' > /tmp/swagger-filter-stage3.json)</tmp/swagger-filter-stage2.json
## tags.json has been manullay tweaked for ordering
TAGS="$(cat 'swagger/tags.json')"
## prepend the tags to swagger body
(jq "$TAGS + ." > "$TARGET_FILE") </tmp/swagger-filter-stage3.json