forked from influxdata/docs-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetswagger.sh
executable file
·273 lines (246 loc) · 8.09 KB
/
getswagger.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash -e
# Use this script to retrieve the following InfluxData API specifications:
# - the latest, fully resolved openapi (OAS, OpenAPI Specification) contract files from the influxdata/openapi repo
#
# Specify a product to retrieve (cloud-serverless, cloud-dedicated, clustered, cloud, v2, v1-compatibility, all).
# Optionally specify:
# - an OSS version as the second argument or using the -o flag.
# The version specifies where to write the updated openapi.
# The default version is the latest OSS version directory in the api-docs directory.
# - a base URL using the -b flag.
# The baseURL specifies where to retrieve the openapi files from.
# The default baseUrl (used for InfluxDB Cloud) is the master branch of influxdata/openapi.
# The default baseUrl for OSS is the docs-release/influxdb-oss branch of influxdata/openapi.
# For local development, pass your openapi directory using the file:/// protocol.
# To use the existing ref.yml and prevent fetching any openapi files, use the -B flag.
# Syntax:
# sh ./getswagger.sh <product>
# sh ./getswagger.sh <product> -b <baseUrl>
# sh ./getswagger.sh -c <product> -o <version> -b <baseUrl>
# sh ./getswagger.sh -c <product> -o <version> -B
#
# Examples:
# sh ./getswagger.sh cloud-serverless
# sh ./getswagger.sh clustered -B
# sh ./getswagger.sh cloud
# sh ./getswagger.sh -c v2 -o v2.0 -b file:///Users/johnsmith/github/openapi
versionDirs=($(ls -d */))
latestOSS=${versionDirs[${#versionDirs[@]}-1]}
# Use openapi master branch as the default base URL.
baseUrl="https://raw.githubusercontent.com/influxdata/openapi/master"
# Use openapi docs-release/influxdb-oss branch for the OSS base URL.
baseUrlOSS="https://raw.githubusercontent.com/influxdata/openapi/docs-release/influxdb-oss"
ossVersion=${latestOSS%/}
verbose=""
product=""
function showHelp {
echo "Usage: ./getswagger.sh <product>"
echo " With optional arguments:"
echo " ./getswagger.sh <product> -b <baseUrl> -V"
echo " ./getswagger.sh cloud"
echo " ./getswagger.sh cloud-dedicated"
echo " ./getswagger.sh cloud-serverless"
echo " ./getswagger.sh oss -o <ossVersion> -V"
echo " ./getswagger.sh all -o <ossVersion>"
echo "Commands:"
echo "-b <URL> The base URL to fetch from."
echo " ex. ./getswagger.sh -b file:///Users/yourname/github/openapi"
echo " The default is the influxdata/openapi repo master branch."
echo "-B Use the existing ref.yml and prevent fetching any openapi files."
echo "-h Show this help."
echo "-o <semantic version> The OSS Version to fetch."
echo " ex. ./getswagger.sh oss -o v2.0"
echo " The default is the latest OSS version directory in the api-docs directory."
echo "-V Verbose. Print the processed arguments and verbose Curl output."
}
subcommand=$1
case "$subcommand" in
cloud-dedicated-v2|cloud-dedicated-management|cloud-serverless-v2|clustered-v2|cloud-v2|v2|v1-compat|all)
product=$1
shift
while getopts ":o:b:BhV" opt; do
case ${opt} in
h)
showHelp
exit 0
;;
V)
verbose="-v"
;;
B)
baseUrl=""
baseUrlOSS=""
;;
b)
baseUrl=$OPTARG
baseUrlOSS=$OPTARG
;;
o)
ossVersion=$OPTARG
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
showHelp
exit 22
;;
:)
echo "Invalid option: $OPTARG requires an argument" 1>&2
showHelp
exit 22
;;
esac
done
shift $((OPTIND -1))
;;
esac
function showArgs {
echo "product: $product";
echo "baseUrl: $baseUrl";
echo "ossVersion: $ossVersion";
}
function postProcess() {
# Use npx to install and run the specified version of openapi-cli.
# npm_config_yes=true npx overrides the prompt
# and (vs. npx --yes) is compatible with npm@6 and npm@7.
specPath="$1"
configPath="$2"
api="$3"
openapiCLI=" @redocly/cli"
currentPath=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# TODO: Move some of this into the plugin:
# Use Redoc's openapi-cli to regenerate the spec with custom decorations.
# If you want to lint the source contract (before bundling),
# pass `--lint` to the `bundle` command.
# If you set environment variables (for example, INFLUXDB_PRODUCT=)
# preceding the command name, you can then access the variables
# in the NodeJS process.env global object.
# INFLUXDB_API_VERSION=$apiVersion \
npx --version
INFLUXDB_PRODUCT=$(dirname "$configPath") \
INFLUXDB_API_NAME=$(echo "$api" | sed 's/@.*//g;') \
API_DOCS_ROOT_PATH=$currentPath \
npm_config_yes=true \
npx $openapiCLI bundle $specPath \
-o $specPath \
--config=$configPath
}
function updateCloudV2 {
outFile="cloud/v2/ref.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrl}/contracts/ref/cloud.yml -o $outFile
fi
postProcess $outFile 'cloud/.config.yml' v2@2
}
function updateCloudDedicatedManagement {
outFile="cloud-dedicated/management/openapi.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS https://raw.githubusercontent.com/influxdata/granite/ab7ee2aceacfae7f415d15ffbcf8c9d0f6f3e015/openapi.yaml -o $outFile
fi
postProcess $outFile 'cloud-dedicated/.config.yml' management@0
}
function updateCloudDedicatedV2 {
outFile="cloud-dedicated/v2/ref.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrl}/contracts/ref/cloud.yml -o $outFile
fi
postProcess $outFile 'cloud-dedicated/.config.yml' v2@2
}
function updateClusteredV2 {
outFile="clustered/v2/ref.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrl}/contracts/ref/cloud.yml -o $outFile
fi
postProcess $outFile 'clustered/.config.yml' v2@2
}
function updateCloudServerlessV2 {
outFile="cloud-serverless/v2/ref.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrl}/contracts/ref/cloud.yml -o $outFile
fi
postProcess $outFile 'cloud-serverless/.config.yml' v2@2
}
function updateOSSV2 {
outFile="v2/ref.yml"
if [[ -z "$baseUrlOSS" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrlOSS}/contracts/ref/oss.yml -o $outFile
fi
postProcess $outFile 'v2/.config.yml' '@2'
}
function updateV1Compat {
outFile="cloud/v1-compatibility/swaggerV1Compat.yml"
if [[ -z "$baseUrl" ]];
then
echo "Using existing $outFile"
else
curl $UPDATE_OPTIONS ${baseUrl}/contracts/swaggerV1Compat.yml -o $outFile
fi
postProcess $outFile 'cloud/.config.yml' 'v1-compatibility'
outFile="v2/v1-compatibility/swaggerV1Compat.yml"
cp cloud/v1-compatibility/swaggerV1Compat.yml $outFile
postProcess $outFile 'v2/.config.yml' 'v1-compatibility'
outFile="cloud-dedicated/v1-compatibility/swaggerV1Compat.yml"
postProcess $outFile 'cloud-dedicated/.config.yml' 'v1-compatibility'
outFile="cloud-serverless/v1-compatibility/swaggerV1Compat.yml"
postProcess $outFile 'cloud-serverless/.config.yml' 'v1-compatibility'
outFile="clustered/v1-compatibility/swaggerV1Compat.yml"
postProcess $outFile 'clustered/.config.yml' 'v1-compatibility'
}
UPDATE_OPTIONS="--fail"
if [ ! -z ${verbose} ];
then
UPDATE_OPTIONS="-v $UPDATE_OPTIONS"
showArgs
echo ""
fi
if [ "$product" = "cloud-v2" ];
then
updateCloudV2
elif [ "$product" = "cloud-dedicated-v2" ];
then
updateCloudDedicatedV2
elif [ "$product" = "cloud-dedicated-management" ];
then
updateCloudDedicatedManagement
elif [ "$product" = "cloud-serverless-v2" ];
then
updateCloudServerlessV2
elif [ "$product" = "clustered-v2" ];
then
updateClusteredV2
elif [ "$product" = "v2" ];
then
updateOSSV2
elif [ "$product" = "v1-compat" ];
then
updateV1Compat
elif [ "$product" = "all" ];
then
updateCloudV2
updateCloudDedicatedV2
updateCloudDedicatedManagement
updateCloudServerlessV2
updateClusteredV2
updateOSSV2
updateV1Compat
else
echo "Provide a product argument: cloud-v2, cloud-serverless-v2, cloud-dedicated-v2, clustered-v2, v2, v1-compat, or all."
showHelp
fi