-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgenerate_client
executable file
·88 lines (69 loc) · 3.38 KB
/
generate_client
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
#!/bin/bash
function _exit {
echo "$1" >&2
exit 1
}
# Exit if cluster name was not specified as a first argument.
[[ "$1" ]] || _exit "Please specify cluster as an argument."
CGEN_NAME="swagger-codegen-cli"
CGEN_VER="2.4.32" # For 3.x use CGEN_VER="3.0.42"
CGEN_JAR_NAME="${CGEN_NAME}-${CGEN_VER}.jar"
CGEN_JAR_URL="https://search.maven.org/remotecontent?filepath=\
io/swagger/swagger-codegen-cli/${CGEN_VER}/${CGEN_JAR_NAME}"
# io/swagger/codegen/v3/swagger-codegen-cli/${CGEN_VER}/${CGEN_JAR_NAME}"
# Uncomment above if you'd like to use swagger-codegen-cli version 3.x
CONFIG_FILE=".swagger-codegen/config.json"
EXTRA_PROPS="basePath=https://YOUR_INSTANCE.wavefront.com"
EXTRA_PROPS+=",[email protected]"
SWGR_JSON="swagger.json"
SWGR_URL="https://$1.wavefront.com/api/v2/${SWGR_JSON}"
VERSION_URL="https://$1.wavefront.com/auth/forgetPassword"
PYTHON=$(command -v python3 || command -v python)
# Make sure required python version and curl tool are present or exit.
$PYTHON -V | grep -q "Python 3" || _exit "Python ^^^ is obsolete. Aborting."
[[ "$(command -v curl)" ]] || _exit "The curl command was not found. Aborting."
# Check if config file exists in '.swagger-codegen' directory.
[[ -f "${CONFIG_FILE}" ]] || _exit "File '${CONFIG_FILE}' not found."
# Get the current version from the config file.
CONFIG_VER=$($PYTHON -c "import json, sys;\
sys.stdout.write(json.load(sys.stdin)['packageVersion'][2:]+'\n')"\
< "${CONFIG_FILE}"
)
echo "Fetching the current version from '$1' cluster..."
SERVER_VER=$(curl -s "${VERSION_URL}" | grep 'version: ' |\
grep -o '[0-9]\+\.[0-9]\+')
echo "Current Version on server is: ${SERVER_VER}"
echo "Current Version in config is: ${CONFIG_VER}"
if [[ -z "${CONFIG_VER}" || -z "${SERVER_VER}" ]]; then
_exit "Unable to determine the version for '$1' cluster."
elif [[ "${CONFIG_VER}" == "${SERVER_VER}" ]]; then
echo "No Version Change Detected. Bye."
else
echo "Version change detected from ${CONFIG_VER} to ${SERVER_VER}..."
TEMP_DIR_NAME="$(mktemp -d)"
SWAGGER_FILE="${TEMP_DIR_NAME}/${SWGR_JSON}"
CGEN_JAR_BINARY="${TEMP_DIR_NAME}/${CGEN_JAR_NAME}"
echo "Step 1: Fetching the latest swagger-codegen..."
curl -Ls "${CGEN_JAR_URL}" -o "${CGEN_JAR_BINARY}"
echo "Step 2: Fetching the config from '${SWGR_URL}'..."
curl -s "${SWGR_URL}" | $PYTHON -m json.tool --sort-keys > "${SWAGGER_FILE}"
# Exit if swagger file is missing at this point.
[[ -f "${SWAGGER_FILE}" ]] || _exit "Failed to fetch ${SWGR_JSON}."
echo "Step 3: Generating the client..."
java -jar "${CGEN_JAR_BINARY}" generate -l python \
-c "${CONFIG_FILE}" -i "${SWAGGER_FILE}" \
--additional-properties "${EXTRA_PROPS}"
echo "Step 4: Checking if anything has changed..."
if [[ -n "$(git status --porcelain)" ]]; then # non-zero diff detected.
echo "Step 5: Updating the version in the config..."
sed -ie "s/${CONFIG_VER//./\\.}/${SERVER_VER//./\\.}/" "${CONFIG_FILE}"
echo "Step 6: Generating the updated client..."
java -jar "${CGEN_JAR_BINARY}" generate -l python \
-c "${CONFIG_FILE}" -i "${SWAGGER_FILE}" \
--additional-properties "${EXTRA_PROPS}"
echo "Step 7: Committing the updated files..."
git add -A && git commit -am "Autogenerated Update v2.${SERVER_VER}."
echo "Please run git push in order to push commit to GitHub... Bye."
fi
echo "Cleaning up..." && rm -rv "${TEMP_DIR_NAME}"
fi