-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash-interactive-functions
373 lines (354 loc) · 9.95 KB
/
.bash-interactive-functions
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
c() { tput sc; clear; tput rc; } # save cursor/restore cursor man terminfo
cat() { if test -d "$1" && test $# = 1; then ls "$1"; else command cat "$@"; fi; }
cdm() { cd $(FULL_PATH=1 get-modified-files | longest-common-prefix -d); }
curl() { command curl -Ss "$@"; }
clock() { watch -n 1 banner -C '$(date +%H:%M:%S)'; }
gcd() {
local _dir
_dir=$(FULL_PATH=1 get-modified-files $1 | get-most-used-dir)
test -d "$_dir" || _dir=$(dirname "$_dir")
if test -d "$_dir"; then
cd "$_dir"
pwd
else
echo "$_dir is not a directory" >&2
fi
}
gcloud() {
local quiet=-q
if test "$1" = -v && test $# != 1; then
shift
quiet=
fi
command gcloud $quiet "$@"
}
gcurl() {
curl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json" \
"$@"
}
gdb() {
case $(uname -s) in
(Darwin) lldb "$@";;
(*) command gdb -tui "$@";;
esac;
}
GET() {
# ACCESS_TOKEN will need be refreshed periodically. Just unset it.
test -z "$ACCESS_TOKEN" \
&& ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl -H "Authorization: Bearer $ACCESS_TOKEN" "$@"
}
gpu() {
local set_upstream
if test "$1" = -u; then set_upstream=-u; shift; fi
remote=${1:-$(g remote | sed -n -e '/^w[^e]/{p; q; }' | grep .)} || {
echo "No remote to use as default. Specify remote as 1st arg" >&2; return 1; }
remote_branch=${2:-$(git rev-parse --abbrev-ref HEAD)}
{ git push $set_upstream "$remote" HEAD:"$remote_branch" 2>&1 >&3 \
| sed -e '/^remote:/d' >&2; } 3>&1
}
grep() {
if test -t 1; then
command grep --color=always "$@" | ${PAGER-more}
return ${PIPESTATUS[0]}
else
command grep --color=auto "$@"
fi
}
wrpselect() {
local -a args
unset REPLY
unset SELECTION
REPLY=$#
case $# in
1)
SELECTION=$1
;;
0)
;;
*)
for x; do echo "$x"; done | expand -t 8 | nl -ba -s ') ' -w 2 | column >&2
read -p "#? " REPLY
if test "$REPLY" -ge 0 2> /dev/null; then
eval SELECTION=\${${REPLY}}
elif test -z "$REPLY"; then
return;
else
for x; do
if echo "$x" | grep -q "$REPLY"; then
args+=("$x")
fi
done
wrpselect "${args[@]}"
fi
;;
esac
}
irssi() { dtach -A /tmp/irssi.dtach irssi; }
use() {
# Put the top level git dir of the cwd in the usage file list
local d root
root="${USE_HOME:-$HOME}"
d=$(cd "${1-.}"; g root) || return
d="${d#$root/}"
test -n "$d" || { echo "Don't try to use $root" >&2; return; }
local file="$root/.config/$USER/usage-list"
mkdir -p "$(dirname $file)"
touch "$file"
if ! grep -q "^$d$" "$file"; then
content=$( { cat "$file"; echo "$d"; } | sort ) \
&& echo "$content" > $file
fi
}
unuse() {
local d=.
root="${USE_HOME:-$HOME}"
local file="$root/.config/$USER/usage-list"
if test "$1" = old; then
for path in $(cat "$file"); do (
cd "$root/$path"
if ! find .git -mtime -7 | grep -q .; then
unuse $path
fi
) done
return
elif test -n "$1"; then
d=$(cd "$root/${1}"; g root) || return
fi
d="${d#$root/}"
content=$( grep -v "^$d$" "$file" ) \
&& echo "$content" > $file
}
get_dir_list() {
local root="${USE_HOME:-$HOME}"
if test "$1" -gt 0 2> /dev/null; then
sed -n -e "${1}p"
else
sed -n -e "\@${1:-.}@p"
fi < $root/.config/$USER/usage-list
}
j() {
local _dir
test -n "$DOCKER" && { echo "Running in DOCKER" >&2; return 1; }
case "$1" in
.)
select-git-branch;;
*)
wrpselect $(get_dir_list "$1")
_dir=$SELECTION
test -n "$_dir" || return
if ! test -d "$_dir"; then
echo "$_dir does not exist!" >&2
else
command cd "$_dir"
fi
;;
esac
g .
}
jq() { command jq --tab "$@"; }
_j_complete() {
local curr_word=${COMP_WORDS[$COMP_CWORD]}
list=$(get_dir_list "$curr_word")
COMPREPLY=($(echo "$list" | tr -s ' ' \\n | grep -- "/$curr_word"))
}
complete -o default -F _j_complete j
k() { kubectl "$@"; }
kx() { kubectx "$@"; }
lb() { tr \\n ${1:-\ } | perl -pe 's/\s*$//' | tmux loadb - ; }
less() { if test -t 1; then command less "$@"; else cat "$@"; fi; }
ls() { command ls -GF "$@"; }
if ! which md5sum > /dev/null 2>&1; then
md5sum() {
openssl md5 "$@" | sed -e 's/MD5(//' -e 's/)=\( [a-z0-9]*$\)/\1/' | awk '{print $2, $1}'
}
fi
mon() { inotifywait -qmr "${@-.}" --exclude '^\./\.git'; }
get_current_gcp_project() {
# This is faster than calling gcloud config get-value project,
# but very fragile
# echo "searching $file to find current config" >&2
# if awk '/^project =/ {print $3}' "$file" 2> /dev/null | grep .; then
# return 0
# fi
if ! gcloud config get-value project 2> /dev/null | grep . ; then
{
printf "\nNo currently configured project. Please run"
printf " 'gcloud config set project \$PROJECT'\n"
printf "You can get list of possible options from "
printf "'gcloud projects list'\n"
} >&2
return 1
fi
}
p() {
local line
local file
local base
base=${HOME}/.config/gcloud/configurations/config
file=${base}_${CLOUDSDK_ACTIVE_CONFIG_NAME-default}
local cfg_file=$HOME/.config/$USER/gcloud-project-list
test -f "$cfg_file" || gcloud projects list > "$cfg_file"
if test -n "$1"; then
REPLY="$1"
else
local CONFIGGED_PROJECT=$(get_current_gcp_project)
nl -s ') ' < "$cfg_file" \
| sed -e "/$CONFIGGED_PROJECT/s/^ / */" -e '/PROJECT_ID/d' \
| less -E
read -p "#? " REPLY
fi
if test "$REPLY" -ge 0 2> /dev/null; then
line=$(awk 'NR>1 && NR==c{print $0}' c="$REPLY" "$cfg_file");
else
line=$(awk 'NR>1 && tolower($0) ~ tolower(c) {print $0}' c="$REPLY" "$cfg_file");
fi
test -z "$line" && return 1
read PROJECT PROJECT_NAME _ <<< "$line"
test -n "$PROJECT" || return 1
CLOUDSDK_ACTIVE_CONFIG_NAME="$PROJECT"
KUBECONFIG="$HOME"/.config/k8s/"$PROJECT"
export PROJECT PROJECT_NAME KUBECONFIG CLOUDSDK_ACTIVE_CONFIG_NAME
file=${base}_${CLOUDSDK_ACTIVE_CONFIG_NAME}
if ! test -f "$file"; then
gcloud config configurations create "$CLOUDSDK_ACTIVE_CONFIG_NAME" || return 1
fi
gcloud config set project "$PROJECT" || return 1
echo "PROJECT=$PROJECT ($PROJECT_NAME)"
}
_p_complete() {
local curr_word=${COMP_WORDS[$COMP_CWORD]}
list=$(cat $HOME/.config/$USER/gcloud-project-list)
COMPREPLY=($(echo "$list" | tr ' ' \\n | grep "$curr_word"))
}
complete -o default -F _p_complete p
ps() { COLUMNS=${CO:-15000} command ps "$@"; }
push() { kill -s SIGUSR1 $(ls $HOME/.bash-history-dir | sed 's/shell-pid-//g'); }
POST() {
# ACCESS_TOKEN will need be refreshed periodically. Just unset it.
test -z "$ACCESS_TOKEN" \
&& ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" "$@"
}
PUT() {
# ACCESS_TOKEN will need be refreshed periodically. Just unset it.
test -z "$ACCESS_TOKEN" \
&& ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl -X PUT -H "Authorization: Bearer $ACCESS_TOKEN" "$@"
}
python() { if test $# = 0; then ipython; else command python "$@"; fi; }
rh() { history -r $HOME/.bash-history; }
ru () { tac | awk '!a[$0]++' | tac; } ## reverse-unique
if ! which run > /dev/null 2>&1 && test -n "$TMUX"; then
run() { # Create two new panes, run a command with stderr in one, stdout in the other
# For some reason, bash is spawning too many panes, so run this through dash
echo 'CAUTION: this is the shell wrapper, not the version from wrp-utils' >&2
pid=$$ dash -s "$@" << \EOF
st_split() { # "stamp-tee split"
perl -pe '$|=1; print localtime . ": "' \
| tee -a "$1" \
| tmux split-window -l 10 -dI
}
stdout="$HOME/.bash-run-output/$pid-stdout"
stderr="$HOME/.bash-run-output/$pid-stderr"
hdr="************************************"
{
printf "\n%s\n%s\n$" "$hdr" "$(date)"
printf " '%s'" "$@"
printf "\n"
} \
| tee -a "$stdout" "$stderr" > /dev/null
{
"$@" 2>&3 | st_split "$stdout"
} 3>&1 \
| st_split "$stderr"
EOF
}
fi
rsync() { command rsync -e 'ssh -o LogLevel=error' "$@"; }
s() { search-bash-history "$@"; }
ssh() { command ssh -o LogLevel=error "$@"; }
ssh_split() {
# Ssh in a new pane
if test -z "$TMUX"; then
echo 'Not in a tmux session. Aborting' >&2
return 1
fi
tmux split-window -p 80 -b sh -c 'ssh $@
nl="$(tput el)
"
printf "${nl}******** terminated: ssh %s ********${nl}" "$*" >&2
printf "Hit return to close pane${nl}" >&2
printf "******** terminated: ssh %s ********${nl}${nl}" "$*" >&2
read' sh "$@";
while test "${1#-}" != "$1"; do shift; done
}
scp() { [[ ! $* =~ : ]] &&
{ echo 'One of the files should be remote' >&2; return 1; }
command scp -p -o LogLevel=error "$@"
}
sort() { LC_ALL=C command sort "$@"; }
if ! which tac; then
if which gtac; then
tac() { gtac "$@"; }
else
tac() { for x; do awk '{a[NR] = $0 }
END {for(c=NR; c>0; c--) print a[c]}' "$x"; done; }
fi
fi > /dev/null 2>&1
ta() { session "$@"; }
tags() { git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' | grep ${1-members} | head; }
tn() { tmux switch-client -n; }
tp() { tmux switch-client -p; }
trim() { perl -nE "chomp; say substr \$_, 0, ${COLUMNS-80} - 1" "$@"; }
ts() {
# stdbuf -oL "$@" | perl -pe '$|=1; print localtime . ": "' | ${PAGER-more}
$@ | perl -pe '$|=1; print localtime . ": "' | ${PAGER-more}
}
typeof() { for e; do which "$e" && file $( which $e ); done }
up() {
local _dir=$(g root)
test -d "$_dir" && cd "$_dir"
while _dir=$( git rev-parse --show-superproject-working-tree | grep .); do
test -d "$_dir" && cd "$_dir"
done
}
update() {
local cmd=update
local pfx=.
# -ef tests if the two paths refer to the same file. This
# seems to work on both macos and linux, but is non-standard.
until test -x $pfx/$cmd || test $pfx -ef ../$pfx; do
pfx=../$pfx
done
if test -x $pfx/$cmd; then
echo $pfx/$cmd "$@"
$pfx/$cmd "$@"
else
echo Unable to find update command looking up in filesystem >&2
fi
}
upfind() {
local f="${1-README}"
if ! find . -maxdepth 1 -name "${f}*" | sed -e "s@^.@$(pwd)@" -e 's@^//@/@' \
| command grep .; then
if ! test . -ef ..; then (
cd ..;
upfind "$f"
)
return
else
printf "%s\n" "$f not found" >&2
return 1
fi
fi
}
xclip() {
if test -t 0 && ! printf '%s' "$*" | grep -q -- ' -o'; then
set -- -o "$@"
fi
command xclip -selection clipboard "$@";
}
y() { select-git-branch "$@"; }