forked from wttech/CQ-Unix-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqapi
executable file
·367 lines (336 loc) · 12.2 KB
/
cqapi
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/sh
# CQ Unix Toolkit
# Copyright (C) 2013 Cognifide Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
VERSION=1.1.2
_usage()
{
cat <<EOT
Usage: $(basename "${0}") [OPTION...]
CQ UNIX Toolkit API command.
Version ${VERSION}
Examples:
cqapi -u admin # Provides username to connect to CQ
cqapi -i http://localhost:5510 # Provided instance URL to connect to CQ
# on local machine on port 5510
cqapi -p secret # Provides password: secret to connect to
# CQ
cqapi -c # Get curl binary path
cqapi -P -u admin # Enable parse mode for common arguments
cqapi -C 2 # Get curl error for exit code: 2
cqapi -H 401 # Get HTTP textual error for HTTP 401 error
# code
cqapi -E # Extract exception message from stdin
cqapi -e # Detect if on stdin there is exception
cqapi -v # Returns toolkit version
cqapi -s # Returns command and parameter required for
# sed extended mode (extended regexp) to
# be compatible with BSD/Mac OS X.
cqapi -w # Returns command and parameter required for
# extended printing purposes (interpreting
# escape sequences) to be compatible
# with BSD/Mac OS X.
Options:
-u Use specified usernamed for connection
-p Use provided password for authentication
-i Use specified instance URL to connect
-c Print on stdout curl binary path
-s Print sed path with -r option
-P Enable parse mode for API common arguments. This must
be first parameter.
-C Get curl friendly error for specified exit code.
-H Get HTTP friendly error response instead of code
-E Return on stdout only exception message skipping
stack trace at the same time
-e Detect if on stdin thereis exception stacktrace
-v Provides toolkit version
-w Print echo command line or path with escaped sequences
turned on
EOT
exit 1
}
_sedpath()
{
echo "${SEDX}"
exit
}
_echopath()
{
echo "printf"
exit
}
_curlpath()
{
WHEREIS=$(whereis whereis 2>/dev/null | cut -f2 -d ' ')
EXITCODE=${?}
if [ ${EXITCODE} -ne 0 -o -z "${WHEREIS}" ]
then
CURLBIN=$(which curl)
EXITCODE=${?}
if [ ${EXITCODE} -ne 0 -o -z "${CURLBIN}" ]
then
echo "Cannot locate required command: curl (using which/whereis\
method)." >&2
exit 1
fi
echo "${CURLBIN}"
exit 0
fi
CURLBIN=$(${WHEREIS} -b curl 2>/dev/null | cut -f2 -d ' ')
if [ -z "${CURLBIN}" ]
then
CURLBIN=$(${WHEREIS} curl | cut -f2 -d ' ')
fi
EXITCODE=${?}
if [ -z "${CURLBIN}" -o ${EXITCODE} -ne 0 ]
then
echo "Cannot locate required command: curl." >&2
exit 1
fi
echo "${CURLBIN}"
exit
}
_curlcode()
{
if [ -z "${1}" -o "${1}" -eq 0 ]
then
exit 0
fi
codes=$(cat <<-'EOF'
1 Unsupported protocol. This build of curl has no support for this protocol.
2 Failed to initialize.
3 URL malformed. The syntax was not correct.
4 A feature or option that was needed to perform the desired request was not enabled or was explicitly disabled at build-time. To make curl able to do this, you probably need another build of libcurl!
5 Couldn't resolve proxy. The given proxy host could not be resolved.
6 Couldn't resolve host. The given remote host was not resolved.
7 Failed to connect to host.
8 FTP weird server reply. The server sent data curl couldn't parse.
9 FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn't exist on the server.
11 FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.
13 FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.
14 FTP weird 227 format. Curl couldn't parse the 227-line the server sent.
15 FTP can't get host. Couldn't resolve the host IP we got in the 227-line.
17 FTP couldn't set binary. Couldn't change transfer method to binary.
18 Partial file. Only a part of the file was transferred.
19 FTP couldn't download/access the given file, the RETR (or similar) command failed.
21 FTP quote error. A quote command returned error from the server.
22 HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f, --fail is used.
23 Write error. Curl couldn't write data to a local filesystem or similar.
25 FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.
26 Read error. Various reading problems.
27 Out of memory. A memory allocation request failed.
28 Operation timeout. The specified time-out period was reached according to the condi‐ tions.
30 FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead!
31 FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.
33 HTTP range error. The range "command" didn't work.
34 HTTP post error. Internal post-request generation error.
35 SSL connect error. The SSL handshaking failed.
36 FTP bad download resume. Couldn't continue an earlier aborted download.
37 FILE couldn't read file. Failed to open the file. Permissions?
38 LDAP cannot bind. LDAP bind operation failed.
39 LDAP search failed.
41 Function not found. A required LDAP function was not found.
42 Aborted by callback. An application told curl to abort the operation.
43 Internal error. A function was called with a bad parameter.
45 Interface error. A specified outgoing interface could not be used.
47 Too many redirects. When following redirects, curl hit the maximum amount.
48 Unknown option specified to libcurl. This indicates that you passed a weird option to curl that was passed on to libcurl and rejected. Read up in the manual!
49 Malformed telnet option.
51 The peer's SSL certificate or SSH MD5 fingerprint was not OK.
52 The server didn't reply anything, which here is considered an error.
53 SSL crypto engine not found.
54 Cannot set SSL crypto engine as default.
55 Failed sending network data.
56 Failure in receiving network data.
58 Problem with the local certificate.
59 Couldn't use specified SSL cipher.
60 Peer certificate cannot be authenticated with known CA certificates.
61 Unrecognized transfer encoding.
62 Invalid LDAP URL.
63 Maximum file size exceeded.
64 Requested FTP SSL level failed.
65 Sending the data requires a rewind that failed.
66 Failed to initialise SSL Engine.
67 The user name, password, or similar was not accepted and curl failed to log in.
68 File not found on TFTP server.
69 Permission problem on TFTP server.
70 Out of disk space on TFTP server.
71 Illegal TFTP operation.
72 Unknown TFTP transfer ID.
73 File already exists (TFTP).
74 No such user (TFTP).
75 Character conversion failed.
76 Character conversion functions required.
77 Problem with reading the SSL CA cert (path? access rights?).
78 The resource referenced in the URL does not exist.
79 An unspecified error occurred during the SSH session.
80 Failed to shut down the SSL connection.
82 Could not load CRL file, missing or wrong format (added in 7.19.0).
83 Issuer check failed (added in 7.19.0).
84 The FTP PRET command failed
85 RTSP: mismatch of CSeq numbers
86 RTSP: mismatch of Session Identifiers
87 unable to parse FTP file list
88 FTP chunk callback reported error
127 Unknown error
'
EOF
)
msg=$(echo "${codes}" | ${SEDX} 's#^[ \t]+##' | grep "^${1} " \
| cut -f2- -d ' ')
if [ -z "${msg}" ]
then
msg="Unknown code"
fi
echo "CURL ERROR ${1}: ${msg}" >&2
exit ${EXITCODE}
}
_httpcode()
{
if [ -z "${1}" -o "${1}" = "200" ]
then
exit 0
fi
codes=$(cat <<-'EOF'
301 Resource has been moved permanently
302 Resource has been moved temporarily
400 HTTP Bad request
401 Unauthorized properly: please check supplied username and password
403 Forbidden resource: please check permissions
404 Not found or forbidden: please check permissions
410 Gone: resource is not available anymore
500 Internal CQ error
501 Internal CQ error
502 Internal CQ error
503 Internal CQ error
EOF
)
msg=$(echo "${codes}" | ${SEDX} 's#^[ \t]+##' | grep "^${1} " \
| cut -f2- -d ' ')
if [ -z "${msg}" ]
then
msg="Unknown response"
fi
echo "HTTP RESPONSE ${1}: ${msg}" >&2
exit 1
}
_java_exception()
{
head -n 1 | cut -f2 -d ':' | ${SEDX} 's#^[ ]+##'
exit
}
_java_exception_detection()
{
grep -Ec "(^[\.[:alnum:]]+Exception:.*\$|^[[:space:]]+at[[:space:]][\.\
[:alnum:]]+(\(.+\))?)"
exit
}
_parse()
{
printf "%s\t%s\t%s\t%s\t%s" "${1}" "${2}" "${3}" "${4}" "${5}"
exit
}
_process_parse_mode()
{
instance="http://localhost:5410"
username=admin
password=admin
while [ ${#} -ne 0 ]
do
getopts_failed=1
while getopts ":${PARSE_OPTIONS}" option
do
getopts_failed=0
case "${option}" in
u)
username=${OPTARG};
explicit_option=1;;
p)
password=${OPTARG};
explicit_option=1;;
i)
instance=$(echo "${OPTARG}" | ${SEDX} 's#[/]+$##');
explicit_option=1;;
esac
done
if [ ${getopts_failed} -eq 1 ]
then
shift
fi
done
_parse "${username}" "${password}" "${instance}" "${explicit_option}" \
"${PARSE_OPTIONS}"
exit 0
}
_invalid_option()
{
if [ "${1}" -ne 1 ]
then
echo "Invalid option: -${2}" >&2
_usage
fi
}
_missing_argument()
{
if [ "${1}" -eq 0 ]
then
echo "Option -${2} requires an argument." >&2
_usage
fi
}
_version()
{
echo ${VERSION}
exit
}
OPTIONS=":Pu:p:i:csC:H:Evwe-:"
PARSE_OPTIONS="u:p:i:"
sed --version >/dev/null 2>/dev/null
BSDSED=${?}
[ ${BSDSED} -eq 0 ] && SEDX="sed -r " || SEDX="sed -E "
explicit_option=0
parse_mode=0
mode=?
argument=?
while getopts "${OPTIONS}" option
do
mode=${option}
case ${option} in
P)
cq_api_output=$(_process_parse_mode "${@}");
printf "%s\n" "${cq_api_output}"
exit ${?};;
u) username=${OPTARG}; explicit_option=1;;
p) password=${OPTARG}; explicit_option=1;;
i) instance=$(echo "${OPTARG}" | ${SEDX} 's#[/]+$##');
explicit_option=1;;
C) argument=${OPTARG};;
H) argument=${OPTARG};;
\?) _invalid_option ${parse_mode} "${OPTARG}";;
:) _missing_argument ${parse_mode} "${OPTARG}";;
esac
done
shift $((OPTIND-1))
# cqapi normal modes
case "${mode}" in
c) _curlpath;;
s) _sedpath;;
C) _curlcode "${argument}";;
H) _httpcode "${argument}";;
E) _java_exception;;
e) _java_exception_detection;;
v) _version;;
w) _echopath;;
esac
_usage