-
Notifications
You must be signed in to change notification settings - Fork 40
/
yaml-build.sh
executable file
·138 lines (119 loc) · 4.22 KB
/
yaml-build.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
#!/bin/bash
LATEST_RELEASE=`git tag --sort=creatordate | tail -n 1`
LATEST_COMMIT=`git rev-parse HEAD`
function from_yaml {
YSH_LIB=1;source ./ysh
eval "$(YSH_parse commands.yml)"
NAMESPACES=`set | grep "^y_" | awk -F"_" '{ print $2; }' | sort -u`
POSTMAN='{"info": {"name": "AppDynamics API","schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"}, "auth": {"type": "basic","basic": [{"key": "password","value": "{{controller_password}}","type": "string"},{"key": "username","value": "{{controller_user}}@{{controller_account}}","type": "string"}]}, "event": [{"listen": "test","script": {"type": "text/javascript","exec": ["pm.globals.set(\"X-CSRF-TOKEN\", postman.getResponseCookie(\"X-CSRF-TOKEN\").value);"]}}],"item": ['
for NS in ${NAMESPACES}; do
COMMANDS=`set | grep "^y_${NS}_" | awk -F"_" '{ print $3; }'| grep -v "=" | sort -u`
# echo -e "Building ${NS}"
NS_DESCRIPTION="y_${NS}_description"
read -r -d '' OUTPUT << ASDF
doc ${NS} << EOF
${!NS_DESCRIPTION}
EOF\n
ASDF
POSTMAN+="{\"name\": \"${NS}\",\"item\": ["
echo -en "${OUTPUT}" >> temp.sh
echo -en "${POSTMAN}" >> postman-collection.json
for CMD in ${COMMANDS}; do
TITLE="y_${NS}_${CMD}_title"
DESCRIPTION="y_${NS}_${CMD}_description"
EXAMPLE="y_${NS}_${CMD}_example"
METHOD="y_${NS}_${CMD}_method"
ENDPOINT="y_${NS}_${CMD}_endpoint"
PAYLOAD="y_${NS}_${CMD}_payload"
FORM="y_${NS}_${CMD}_form"
EXPAND="y_${NS}_${CMD}_expand"
ENDPOINT=${!ENDPOINT}
PAYLOAD=${!PAYLOAD}
FORM=${!FORM}
EXPAND=${!EXPAND}
# echo -e "\t- ${CMD} (${ENDPOINT})"
POSTMAN_ENDPOINT=${ENDPOINT}
POSTMAN_PAYLOAD=${PAYLOAD}
ARGUMENT_PATTERN="\{\{([a-zA-Z]):([a-zA-Z0-9_-]+)(\??)\}\}"
while [[ $POSTMAN_ENDPOINT =~ $ARGUMENT_PATTERN ]] ; do
REPLACEMENT="{{${BASH_REMATCH[2]}}}"
POSTMAN_ENDPOINT=${POSTMAN_ENDPOINT//${BASH_REMATCH[0]}/$REPLACEMENT}
done;
while [[ $POSTMAN_PAYLOAD =~ $ARGUMENT_PATTERN ]] ; do
REPLACEMENT="{{${BASH_REMATCH[2]}}}"
POSTMAN_PAYLOAD=${POSTMAN_PAYLOAD//${BASH_REMATCH[0]}/$REPLACEMENT}
done;
P_PATH=${POSTMAN_ENDPOINT%%\?*}
P_PATH=${P_PATH//\//\",\"}
P_QUERY=""
if [[ ${POSTMAN_ENDPOINT} == *"?"* ]] ; then
P_QUERY=${POSTMAN_ENDPOINT#*\?}
local REPLACEMENT="\"},{\"key\":\""
P_QUERY=${P_QUERY//&/${REPLACEMENT}}
P_QUERY="{\"key\": \"${P_QUERY//=/\",\"value\": \"}\"}"
fi
POSTMAN+="{
\"name\": \"${!TITLE}\",
\"request\": {
\"method\": \"${!METHOD}\",
\"header\": [
{
\"key\": \"Content-Type\",
\"value\": \"application/json;charset=UTF-8\",
\"type\": \"text\"
},
{
\"key\": \"X-CSRF-TOKEN\",
\"value\": \"{{X-CSRF-TOKEN}}\",
\"type\": \"text\"
}
],
\"body\": {
\"mode\": \"raw\",
\"raw\": \"${POSTMAN_PAYLOAD//\"/\\\"}\"
},
\"url\": {
\"raw\": \"{{controller_host}}${ENDPOINT}\",
\"host\": [
\"{{controller_host}}\"
],
\"path\": [${P_PATH#\",}\"],
\"query\": [${P_QUERY}]
},
\"description\": \"${!DESCRIPTION}\"
}
},"
if [ -n "${PAYLOAD}" ] ; then
PAYLOAD=" -d '${PAYLOAD}'"
else
PAYLOAD=""
fi;
if [ -n "${FORM}" ] ; then
FORM=" -F '${FORM}'"
else
FORM=""
fi;
if [ -n "${!METHOD}" ] && [ "${!METHOD}" != "GET" ]; then
METHOD=" -X ${!METHOD}"
else
METHOD=""
fi;
if [ "${EXPAND}" == "true" ] ; then
read -r -d '' OUTPUT << ASDF
${NS}_${CMD}() { apiCallExpand${METHOD}${PAYLOAD}${FORM} '${ENDPOINT}' "\$@" ; }
rde ${NS}_${CMD} "${!TITLE}" "${!DESCRIPTION}" "${!EXAMPLE}"\n
ASDF
else
read -r -d '' OUTPUT << ASDF
${NS}_${CMD}() { apiCall${METHOD}${PAYLOAD}${FORM} '${ENDPOINT}' "\$@" ; }
rde ${NS}_${CMD} "${!TITLE}" "${!DESCRIPTION}" "${!EXAMPLE}"\n
ASDF
fi
echo -en "${OUTPUT}" >> temp.sh
done;
POSTMAN=${POSTMAN%,}
POSTMAN+="]},"
done;
POSTMAN=${POSTMAN%,}
echo -en "${POSTMAN}]\n}" > postman-collection.json
}