-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.rc.common
executable file
·377 lines (302 loc) · 8.91 KB
/
.rc.common
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
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env sh
# vim:tw=80:ts=4:sw=4:ai:et:ff=unix:fenc=utf-8:et:fixeol:eol:fdm=marker:fdl=0:fen:
#
# Utilities for all shell scripts.
#
# More stuff to do here. {{{
#
# - [ ] figure out a better way to name functions (shell doesn't have namespaces)
#
# }}}
if [ -z "$_THIS_FILE" ]; then
_THIS_FILE="$0"; readonly _THIS_FILE
fi
# Source personal stuff.
if [ -f "$HOME/bin/commonp.sh" ]; then
. "$HOME/bin/commonp.sh"
fi
# Defaults to 'set -e'.
mm_set() {
local mode="$1"
if [ "$mode" = "all" ]; then
set -euxo pipefail
elif [ "$mode" = "u" ]; then
# Exit on undefined variable.
set -u
elif [ "$mode" = "x" ]; then
# Print all commands before execution.
set -x
else
# Exit on error.
set -e
fi
}
mm_now() {
local mode="$1"
if [ "$mode" = "--path" ]; then
echo "$(date '+%F_%T' | tr ':' '-')"
elif [ "$mode" = "--simple" ]; then
echo "$(date '+%d%b%y/%Hh%M')"
else
if [ -z "$mode" ]; then
mode='+%F %T'
fi
echo "$(date "$mode")"
fi
}
alias mcra_now=mm_now
# In shell, for return values, 0 is success (eq to true in a conditional) and 1 is failure (eq
# to false in a conditional).
is_debug() {
# If the variable is empty, we are not in debug mode.
if [ -z "$MCRA_DEBUG" ]; then
# Not debug mode.
return 1
else
# Debug mode.
return 0
fi
}
debug() {
if ! is_debug; then
return 0
fi
echo "[$(date '+%F %T')] [debug] $@"
}
alias mm_debug=debug
alias mm_is_debug=is_debug
if is_debug; then
echo '----------------------------------------------------------------------------'
echo 'DEBUG MODE ENABLED!'
echo '----------------------------------------------------------------------------'
if [ "$MCRA_DEBUG" = "test" ]; then
debug '$MCRA_DEBUG == test: tests will run'
fi
fi
error() {
debug $1
return 1
}
fatal() {
echo
echo "$@"
exit 1
}
has_prefix() {
if [ $# -ne 2 ]; then
error 'Usage: has_prefix "original string" "prefix to check".'
fi
if [ "${1#$2}" != "$1" ]; then
# The `${1#$2}` substitution replaces $2 at the beginning of $1, making it different from
# itself, hence entering this branch and confirming that $2 is a prefix on $1.
return 0 # true
else
return 1 # false
fi
}
count=1
assert() {
if ! is_debug; then
return 0
fi
local test_name=$1
local expected=$2
local actual=$3
echo -n "[$(date '+%F %T')] [test] $count "
if [ "$expected" != "$actual" ]; then
echo -n "failure: $test_name\n\n"
echo "expected:\n\n$expected\n"
echo "actual:\n\n$actual"
else
echo -n "success: $test_name"
fi
echo
count=$((count + 1))
}
get_this_file_dir() {
echo "$(command -v readlink >/dev/null 2>&1 && dirname $(readlink -f "$1") || dirname "$1")"
}
alias mm_this_file_dir=get_this_file_dir
mm_is_command() {
command -v "$1" >/dev/null 2>&1
}
mm_file_path() {
local filepath="$1"
if mm_is_command readlink; then
echo "$(readlink -f "$filepath")"
elif mm_is_command realpath; then
echo "$(realpath "$filepath")"
else
echo "Need 'readlink' or 'realpath' and they were not found."
fi
}
mm_dir_path() {
echo "$(dirname $(mm_file_path "$1"))"
}
mm_grep() {
local grep_cmd='grep' # Default.
echo 'rg egrep' | tr ' ' '\n' | while read cmd; do
if mm_is_command $cmd; then
grep_cmd="$cmd"
break
fi
done
$grep_cmd "$@"
}
mm_remove_leading_spaces() {
local num_spaces="${1:-8}"
local usage="\nUsage: $0 [num]\n\nRemoves [num] leading spaces (default=8)."
case "$num_spaces" in
help|-h|--help)
echo $usage
return 1
;;
esac
local check_number="$(echo "$num_spaces" | sed -E -e 's/[1-9][0-9]*//')"
if [ ! -z "$check_number" ]; then
echo $usage
echo "\nInvalid [num]: $1"
return 1
fi
sed -E -e "s/^\s{0,$num_spaces}//"
}
mm_trim() {
# Remove leading spaces.
local output1="$(echo "$1" | mm_remove_leading_spaces "$2")"
# Remove first newline character.
local output2="${output1#"\n"}"
# Remove last newline character and output.
echo "${output2%"\n"}"
}
mcra_doc() {
local usage="
USAGE:
mcra_doc \$@ '<docs>'
mcra_doc \$@ \"<docs>\"
NOTES:
1) The first argument should be literally that. It is what detects
whether the user wants more information about the function (by
detecting '-h', '--help' and 'help').
2) If you want, you can write the <docs> part using newlines and
leading spaces, as the function will remove them for you. For
example:
mcra_doc \$@ '
Here goes your function documentation. The first newline (above)
along with up to 8 leading spaces
<--- here
will be removed for you, as will the last newline right after
this line.
<--- here.
'
3) You might want to use variable/process substitution, in which
case you would use the second variation (with double quotes). You
can still write it as described in 2 above, just replace the single
quotes for double. In this case, remember to escape double quotes or
other stuff that should be displayed literally.
4) If you want a full example, check the following function in this
file: mm_example_documented_function. It provides the common use
case for this function.
"
# Either this will be called with one or two arguments.
#
# If called with only one, the user is trying to get info about this
# particular function, so show the help.
#
# If called with two, the user is documenting their function. In this case,
# if later the function is called with the first argument being one of the
# help ones, print the help.
case "$1" in
help|-h|--help)
if [ -z "$2" ]; then
mm_trim $usage
return 1
fi
mm_trim "$2"
return 1
;;
esac
return 0
}
mm_example_documented_function() {
mcra_doc $@ "
NAME: $0
DESCRIPTION:
Shows some example \"mcra_doc\" usage and output.
Run this with different arguments to see different behaviors.
" || return 0
echo 'Print this if first argument above is not "help|-h|--help"'
}
main() {
local editor="${EDITOR:-vi}"
mcra_get_last_edit() {
date '+%F_%T' -r "$1" | tr ':' '-'
}
local this_file_last_edit_time="$(mcra_get_last_edit "$_THIS_FILE")"
if [ "$MCRA_COMMON" = "$this_file_last_edit_time" ]; then
# No need to rerun this file, as it wasn't editted since last run.
return 0
fi
export MCRA_COMMON="$this_file_last_edit_time"
export MCRA_LOGFILE="$HOME/.mcra.logfile.txt"
mcra_log_base() {
if [ -z "$1" ] || [ -z "$2" ]
then
mm_trim "
Usage: $0 [mode] [message to log] [additional info (optional)]
Mode:
- log: append to logfile as log and return 0
- err: append to logfile as err and return 1
Additional info:
Any extra arguments after [mode] and [message] will be
appended to the logfile as provided.
"
return 1
fi
local logfile="$MCRA_LOGFILE"
if [ ! -f $logfile ]; then
touch $logfile
fi
local mode=log
local msg="$1"
if [ "$1" = "err" ]; then
mode=err
msg="$2"
elif [ "$1" = "cat" ]; then
cat $MCRA_LOGFILE
return 0
elif [ "$1" = "edit" ]; then
$editor $MCRA_LOGFILE
return 0
fi
echo "[$(mm_now)] [$mode] $msg" >> $logfile
shift; shift; # Ignore the first two arguments and use all the others.
count=1
while [ $# -ne 0 ]; do
echo -n "\n [metadata $count] $1" >> $logfile
count=$((count+1))
shift
done
if [ $count -gt 1 ]; then
echo '\n' >> $logfile
fi
}
alias mcra_log="mcra_log_base log"
alias mcra_err="mcra_log_base err"
#
# Next function above this line.
#
}
main "$@"
# /////////////////////////////////////////////////////////////////////////
# Tests.
# /////////////////////////////////////////////////////////////////////////
#
# Functions are tested here, but ignored when debug is disabled.
# Run tests.
if [ "$MCRA_DEBUG" = "test" ]
then
has_prefix "hello world" "hello" && assert "prefix match" $? 0
has_prefix "hello world" "world" || assert "prefix doesn't match" $? 1
has_prefix "hello world" "not there" || assert "prefix doesn't match with completely different strings" $? 1
assert "get_this_file_dir" $(get_this_file_dir $0) "$HOME/projects/dotfiles"
fi