-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.sh
209 lines (202 loc) · 5.32 KB
/
parse.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
#!/usr/bin/env bash
#
# author: @bekcpear
# license: GPLv2
#
#Func: read file and parse configurations
# $1 <PATH>
# $2 <IDENTIFIER> (array: parameters)
# $3 <IDENTIFIER> (array: ignored list)
# $4 <IDENTIFIER> (array: pass values to it)
#return: $?
function parseconfigs() {
local p="${1}"
local -i ret=0
shopt -s extglob
eval "local opts=(\${${2}[@]})"
eval "p=\$(_absolutepath '${p}')"
for opt in ${opts[@]}; do
if [[ ! -d ${p%/*} ]]; then
printlog "Directory '${1%/*}' does not exist." warn
ret+=1
fi
local val
eval "val=\$(grep -Ei '^[[:space:]]*${opt}[[:space:]]*=' '${p}' 2>${VERBOSEOUT2}) || true"
eval "val=\$(echo '${val}' | { IFS='='; read -r _ v; echo -n \"\${v}\"; }) || ret+=\$?"
val=${val##[[:space:]]}
if [[ "${val}" =~ ^\"([^\"]*)\"|^\'([^\']*)\'|^\`([^\`]*)\` ]]; then
val="${BASH_REMATCH[1]:-${val}}"
val="${BASH_REMATCH[2]:-${val}}"
val="${BASH_REMATCH[3]:-${val}}"
else
val="${val%%+([[:space:]])#*}"
fi
if [[ "${opt}" == IGNORE ]]; then
#parse ignore patterns
if [[ -n ${val} ]]; then
local -i nn=0
val="${val//\\/\\\\}"
val="${val//\\,/Z16CoMmA-f09448b9-96c6-4f09-8b6f-bb7d5c251943}"
val="${val//,/$'\n'}"
val="${val//Z16CoMmA-f09448b9-96c6-4f09-8b6f-bb7d5c251943/\\,}"
while read -r n; do
eval "${3}[${nn}]=\"${n}\""
(( ++nn ))
done <<< "${val}"
fi
else
eval "${4}['${opt}']='${val}'"
fi
done
shopt -u extglob
return ${ret}
}
#Func: parse shell parameters
# $@
function parseparam() {
set +e
unset GETOPT_COMPATIBLE
getopt -T
if [[ ${?} != 4 ]]; then
fatalerr "The command 'getopt' of Linux version is necessory to parse parameters."
fi
local args
args=$(getopt -o 'P:c:fi:k::ps:v' -l 'port:,config:,force,identity-file:,keep-alive::,pretend,ssh-destination:,verbose' -n 'z16' -- "$@")
if [[ ${?} != 0 ]]; then
showhelp
exit 1
fi
set -e
# parse arguments
eval "set -- ${args}"
while true; do
case "${1}" in
-P|--port)
shift
if [[ ${1} =~ ^[[:digit:]]+$ ]]; then
Z16_SSH[PORT]=${1}P
else
printlog "Unrecognized ssh port: '${1}', use default '${Z16_SSH[PORT]}' instead." warn
fi
shift
;;
-c|--config)
shift
CONFPATH="${1}"
shift
;;
-f|--force)
FORCEOVERRIDE=1
shift
;;
-i|--identity-file)
shift
Z16_SSH[IDENTITYOPTS]+="-i '${1}' "
shift
;;
-k|--keep-alive)
shift
Z16_SSH[KEEP]=1
if [[ ${1} =~ ^[[:digit:]]+(s|S|m|M|h|H|d|D|w|W)?$ ]]; then
Z16_SSH[MUX_TIMEOUT]=${1}
elif [[ -n ${1} ]]; then
printlog "Unrecognized timeout argument: '${1}', use default '${Z16_SSH[MUX_TIMEOUT]}' instead." warn
fi
shift
;;
-p|--pretend)
PRETEND=1
shift
;;
-s|--ssh-destination)
shift
Z16_SSH_RAW="${1}"
shift
;;
-v|--verbose)
VERBOSEOUT1='&1'
VERBOSEOUT2='&2'
shift
;;
--)
shift
break
;;
*)
fatalerr "unknow error"
;;
esac
done
# check action
local cmd
local avacmd=( ${AVAILABLECMD//|/ } ${AVAILABLECMD_NOARGS//|/ } )
for cmd in ${avacmd[@]}; do
if [[ "${cmd}" == "${1}" ]]; then
CMD=${1}
shift
break
fi
done
if [[ ${CMD} =~ ${AVAILABLECMD} && ${#} < 1 ]]; then
fatalerr "Action '${CMD}' needs more arguments!" True
fi
if [[ ${CMD} =~ ${AVAILABLECMD_NOARGS} && ${#} > 0 ]]; then
eval "printlog 'args: \"${@}\" ignored' warn"
return
fi
# parse instance name
for arg; do
case "${arg}" in
-*)
printlog "Unrecognized instance name '${arg}' has been ignored." warn
;;
*[[:space:]]*)
fatalerr "Instance name should not contain spaces!"
;;
*/*)
fatalerr "Instance name should not contain slashes!"
;;
*)
if [[ ${#INSTANCES[@]} == 0 ]]; then
INSTANCES=( "${arg}" )
else
[[ "${INSTANCES[@]}" =~ (^|[[:space:]])${arg}([[:space:]]|$) ]] || \
INSTANCES=( "${INSTANCES[@]}" "${arg}" )
fi
;;
esac
done
# check ssh configurations and unify
if [[ -n ${Z16_SSH_RAW} ]]; then
local sshconfraw _idfpath
eval "sshconfraw=\"\$(ssh ${Z16_SSH[IDENTITYOPTS]} -G ${Z16_SSH_RAW})\"" || \
fatalerr "Get SSH configurations error!" $?
while read -r line; do
case "${line}" in
user*)
Z16_SSH[USER]="${line#* }"
;;
hostname*)
Z16_SSH[HOSTNAME]="${line#* }"
;;
port*)
if [[ ! ${Z16_SSH[PORT]} =~ P$ ]]; then
Z16_SSH[PORT]="${line#* }"
fi
;;
identityfile*)
if [[ -z ${Z16_SSH[IDENTITYOPTS]} ]]; then
_idfpath=$(_absolutepath ${line#* })
if [[ -e ${_idfpath} ]]; then
Z16_SSH[IDENTITYOPTS]+="-i '${_idfpath}' "
fi
fi
;;
esac
done <<< "$(echo "${sshconfraw}" | grep -E '^user\s|^hostname\s|^port\s|^identityfile\s')"
if [[ ${Z16_SSH[PORT]} =~ P$ ]]; then
Z16_SSH[PORT]="${Z16_SSH[PORT]%P}"
fi
fi
}
# vim: et:ts=2:sts:sw=2