-
Notifications
You must be signed in to change notification settings - Fork 16
/
gredit.sh
executable file
·334 lines (281 loc) · 7.52 KB
/
gredit.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
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
#!/bin/bash
# [ 0x19e Networks ]
# http://0x19e.net
# Author: Robert W. Baumgartner <[email protected]>
#
# Allows editing a range of files by locating similar lines.
# Two grep patterns are used to locate lines: the first allows narrowing the context to a block of
# text that contains the actual target line. This is done using a regular expression and an integer
# defining how many lines surrounding each match to include when the next search is performed.
# The final search simply greps for a given target string, which must be an exact match.
# Every match is then processed to construct edit commands.
#
# TODO: Allow searching single files.
EDIT_COMMAND="editor"
DEFAULT_CTX_LINES=3
GREP_LOCATION="*"
CONTEXT_REGEX=""
TARGET_STRING=""
CONTEXT_LINES=${DEFAULT_CTX_LINES}
#CONTEXT_REGEX="EOF"
#CONTEXT_REGEX="^[\s[A-Za-z0-9\-\_]+ocsp[A-Za-z0-9\-\_]+\s]$"
#TARGET_STRING="sed\s\-e"
#TARGET_STRING="authorityInfoAccess"
exit_script()
{
# Default exit code is 1
local exit_code=1
local re
re='^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
if echo "$1" | grep -qE "$re"; then
exit_code=$1
shift
fi
re='[[:alnum:]]'
if echo "$@" | grep -iqE "$re"; then
if [ "$exit_code" -eq 0 ]; then
echo "INFO: $*"
else
echo "ERROR: $*" 1>&2
fi
fi
# Print 'aborting' string if exit code is not 0
[ "$exit_code" -ne 0 ] && echo "Aborting script..."
exit "$exit_code"
}
usage()
{
# Prints out usage and exit.
sed -e "s/^ //" -e "s|SCRIPT_NAME|$(basename "$0")|" << "EOF"
USAGE
Edit a configuration settings across multiple files in a directory using grep.
SYNTAX
SCRIPT_NAME [OPTIONS] ARGUMENTS
ARGUMENTS
path The location to search within.
OPTIONS
-c, --context <regex> A RegEx to locate lines close to the desired target.
-t, --target <expr> An expression identifying the target lines to process.
-s, --search <lines> The number of lines around context matches to search within.
-r, --recursive Recurse sub-directories (if target is a folder).
-l, --list List matching files but do not edit.
--dry-run Do not invoke editor; print out commands instead.
--vim Use Vim instead of the default editor.
-v, --verbose Make the script more verbose.
-h, --help Prints this usage.
EXAMPLES
- To search and edit AIA OCSP settings within an OpenSSL configuration:
`~/gredit.sh -c '^[\s[A-Za-z0-9\-\_]+ocsp[A-Za-z0-9\-\_]+\s]$' -t 'authorityInfoAccess' -s 20 ./ssl-config/`
EOF
exit_script "$@"
}
test_arg()
{
# Used to validate user input
local arg="$1"
local argv="$2"
if [ -z "$argv" ]; then
if echo "$arg" | grep -qE '^-'; then
usage "Null argument supplied for option $arg"
fi
fi
if echo "$argv" | grep -qE '^-'; then
usage "Argument for option $arg cannot start with '-'"
fi
}
test_path_arg()
{
# test directory argument
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
if [ ! -e "$argv" ]; then
usage "Specified path does not exist: $argv"
fi
}
test_number_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
re='^[0-9]+$'
if ! [[ "$argv" =~ $re ]] ; then
usage "Option for argument $arg must be numeric."
fi
}
VERBOSITY=0
#VERBOSE=""
#check_verbose()
#{
# if [ $VERBOSITY -gt 1 ]; then
# VERBOSE="-v"
# fi
#}
#argc=$#
[ $# -gt 0 ] || usage
i=1
TARGET_PATH=""
GREP_RECURSIVE="false"
LIST_MODE="false"
DRY_RUN="false"
USE_VIM="false"
# process arguments
while [ $# -gt 0 ]; do
case "$1" in
-c|--context)
test_arg "$1" "$2"
shift
CONTEXT_REGEX="$1"
shift
;;
-t|--target)
test_arg "$1" "$2"
shift
TARGET_STRING="$1"
shift
;;
-s|--search)
test_number_arg "$1" "$2"
shift
CONTEXT_LINES="$1"
shift
;;
-v|--verbose)
((VERBOSITY++))
#check_verbose
i=$((i+1))
shift
;;
-vv)
((VERBOSITY++))
((VERBOSITY++))
#check_verbose
i=$((i+2))
shift
;;
-vvv)
((VERBOSITY++))
((VERBOSITY++))
((VERBOSITY++))
#check_verbose
i=$((i+3))
shift
;;
--dry-run)
DRY_RUN="true"
shift
;;
--vim)
USE_VIM="true"
shift
;;
-l|--list)
LIST_MODE="true"
shift
;;
-r|--recursive)
GREP_RECURSIVE="true"
shift
;;
-h|--help)
usage
;;
*)
if [ -n "${TARGET_PATH}" ]; then
usage "Cannot specify multiple search locations."
fi
test_path_arg "$1"
TARGET_PATH="$(readlink -m "${1}")"
shift
;;
esac
done
if [ "${USE_VIM}" == "true" ]; then
EDIT_COMMAND="vim"
fi
if [ "${DRY_RUN}" == "true" ]; then
PRE_CMD="echo"
fi
GREP_EXT_OPTS=""
GREP_CTX_OPTS="-P"
if [ "${GREP_RECURSIVE}" == "true" ]; then
GREP_EXT_OPTS="${GREP_EXT_OPTS} -r"
fi
if [ -z "${TARGET_STRING}" ]; then
usage "Must specify a target string to search for."
fi
if [ -z "${CONTEXT_REGEX}" ]; then
if [ "${CONTEXT_LINES}" -eq ${DEFAULT_CTX_LINES} ]; then
CONTEXT_LINES=1
fi
GREP_CTX_OPTS=""
CONTEXT_REGEX="${TARGET_STRING}"
fi
if [ -z "${TARGET_PATH}" ]; then
TARGET_PATH="$(readlink -m .)"
fi
#if [ -d "${TARGET_PATH}" ]; then
#pushd "${TARGET_PATH}" > /dev/null 2>&1
#else
#GREP_LOCATION="${TARGET_PATH}"
#fi
if [ ! -d "${TARGET_PATH}" ]; then
GREP_LOCATION="${TARGET_PATH}"
else
GREP_LOCATION="${TARGET_PATH}/${GREP_LOCATION}"
fi
GREP_COMMAND="grep -P ${GREP_EXT_OPTS} -n ${GREP_CTX_OPTS} '${CONTEXT_REGEX}' ${GREP_LOCATION} -A${CONTEXT_LINES}"
if [ -n "${TARGET_STRING}" ] && [ ! -d "${TARGET_STRING}" ]; then
GREP_COMMAND="${GREP_COMMAND} | grep '${TARGET_STRING}'"
fi
if [ $VERBOSITY -gt 0 ]; then
echo "Target string : ${TARGET_STRING}"
echo "Target path : ${TARGET_PATH}"
echo "Grep location : ${GREP_LOCATION}"
echo "Grep command : ${GREP_COMMAND}"
fi
GREP_RESULTS=$(bash -c "${GREP_COMMAND} | grep -vP '^Binary\sfile'")
if [ -n "${TARGET_STRING}" ] && [ ! -d "${TARGET_STRING}" ]; then
GREP_RESULTS=$(echo "${GREP_RESULTS}" | grep "${TARGET}")
fi
# To ignore comments use 'grep -v -P "(\-|\:)[0-9]+(\-|\:)([\s]+)?\#"', for example:
# if ! TMP_RESULTS=$(echo "${GREP_RESULTS}" | grep -P "${TARGET_STRING}" | grep -v -P "(\-|\:)[0-9]+(\-|\:)([\s]+)?\#" | sed -r "s/\-([0-9]+)\-/\:\1\:/"); then
if ! TMP_RESULTS=$(echo "${GREP_RESULTS}" | grep -P "${TARGET_STRING}" | sed -r "s/\-([0-9]+)\-/\:\1\:/"); then
echo >&2 "ERROR: Failed to perform search."
exit 1
fi
if [ -z "${TMP_RESULTS}" ]; then
echo >&2 "No result(s) found."
exit 1
fi
if [ "${LIST_MODE}" == "true" ]; then
if [ -d "${TARGET_PATH}" ]; then
MATCHES=$(echo "${TMP_RESULTS}" | awk -F: '{ print $1 }' | uniq -c | sed -e 's/^[ \t]*//')
echo "${MATCHES}" | while read -r line; do
if [ ! -z "${line}" ]; then
echo "${line}" | awk -F' ' "{ printf \"Found %d match(es): %s\n\", \$1, \$2 }";
fi
done
# echo "${TMP_RESULTS}" | awk -F: '{ printf "Found match(es): %s\n", $1 }'
popd > /dev/null 2>&1
else
echo "Found match(es): ${TARGET_PATH}"
fi
exit 0
fi
if [ ! -d "${TARGET_PATH}" ]; then
AWK_CMD="{ printf \"+%s %s\n\", \$1, \"${TARGET_PATH}\" }"
else
AWK_CMD="{ printf \"+%s %s\n\", \$2, \$1 }"
fi
IFS=$'\n'; for l in $(echo "${TMP_RESULTS}" | awk -F: "${AWK_CMD}"); do ${PRE_CMD} bash -c "${EDIT_COMMAND} ${l}"; done
#if [ -d "${TARGET_PATH}" ]; then
#popd > /dev/null 2>&1
#fi
exit 0