-
Notifications
You must be signed in to change notification settings - Fork 1
/
_alias
583 lines (507 loc) · 15.5 KB
/
_alias
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/bin/bash
# "do" : execute one command on multiple folders
alias d='~/bin/d'
IS_WINDOWS=0
IS_MACOS=0
case "$(uname -s)" in
MINGW*) IS_WINDOWS=1 ;;
Darwin*) IS_MACOS=1 ;;
esac
if [[ 1 -eq $IS_WINDOWS ]]; then
alias dir='ls'
fi
which xmlstarlet > /dev/null
if [[ 0 -eq $? ]]; then
alias x='xmlstarlet'
fi
if [[ 1 -eq $IS_WINDOWS ]]; then
o (){
start "$@"
}
elif [[ 1 -eq $IS_MACOS ]]; then
o (){
open "$@"
}
else
# assume linux
which xdg-open > /dev/null
if [[ 0 -eq $? ]]; then
o (){
xdg-open "$@" 2> /dev/null
}
fi
fi
# Ack
# quick and dirty way to handle 'ack-grep' as 'ack'
which ack-grep > /dev/null
if [[ 0 -eq $? ]]; then
alias ack='ack-grep'
fi
which ack > /dev/null
if [[ 0 -eq $? ]]; then
alias a='ack'
alias ai='ack -i'
alias al='ack -l'
alias ali='ack -li'
fi
HAS_FZF=0
which fzf > /dev/null
if [[ 0 -eq $? ]]; then
HAS_FZF=1
fi
HAS_GH=0
which gh > /dev/null
if [[ 0 -eq $? ]]; then
HAS_GH=1
fi
# Maven
which mvn > /dev/null
if [[ 0 -eq $? ]]; then
# TODO : print error when cwd does not contains a pom.xml
alias m='mvn'
alias md='mvnDebug'
alias mi='mvn install'
alias mp='mvn package'
alias mpst='mvn package -DskipTests'
alias mci='mvn clean install'
alias mcist='mvn clean install -DskipTests -Dmaven.javadoc.skip=true -Danimal.sniffer.skip'
# default effective pom output
mvn_effective_pom_file='target/effective-pom.xml'
# maven effective pom
mvn_effective_pom () {
local out="${1:-$mvn_effective_pom_file}"
mvn help:effective-pom -Doutput="$out" >/dev/null || return 1
}
# maven dependency graph (without clutter)
mdep (){
local outFile='target/.mdep'
mvn dependency:tree -DoutputFile="$outFile" "$@">/dev/null
if [[ 0 -eq $? ]]; then
cat **/$outFile
/bin/rm -f **/$outFile
else
echo "error : can't retrieve dependency tree, 'clean install' required if you haven't built project yet"
fi
}
# maven project version
mpv () {
mvn help:evaluate -Dexpression=project.version -q -DforceStdout|tail -1
}
# maven effective pom (output to console for easy grep)
mep () {
local out="${1:-$mvn_effective_pom_file}"
mvn_effective_pom $out && cat $out && /bin/rm -f $out
}
fi
# Git
which git > /dev/null
if [[ 0 -eq $? ]]; then
# taken as-is form zsh/oh-my-zsh configuration to make it work with bash too
_current_branch () {
ref=$(git symbolic-ref HEAD 2>/dev/null) || head=$(git rev-parse --short HEAD 2>/dev/null)
echo ${ref#refs/heads/}
}
alias g='git'
alias gl='git log --decorate --oneline --graph'
alias gla='git log --decorate --oneline --graph --all'
alias gld='git log --decorate --oneline --graph --simplify-by-decoration'
alias gls='git ls-files --exclude-standard'
#alias gpr='git pull --rebase'
alias gp='git pull'
alias gpu='git push'
alias gf='git fetch --all --tags'
alias gst='git status -sb'
alias ga='git add'
alias gap='git add -vp'
alias gai='git add -i'
alias gds='git diff --staged'
alias gd='git diff'
alias gc='git commit'
gbm () {
# TODO wip attempt to have a "git branch merge base diff" to
current_branch=${1:-$(git_current_branch)}
main_branch=${2:-main}
git --no-pager diff --name-only ${current_branch} $(git merge-base ${current_branch} ${main_branch})
}
alias gco='git checkout'
alias gs='git switch'
# allow to know which commits have been merged upstream/to specific branch
alias gch='git cherry -v'
alias gcp='git cherry-pick'
alias gdesc='git describe --tags --always --dirty'
# git list unmerged files
glsu (){
git ls-files -u | awk '{print $4}' | sort -u
}
alias gwl='git worktree list'
if [[ 1 -eq $HAS_FZF ]]; then
# git worktree select
gws() {
echo "$(gwl | fzf | awk '{print $1}')"
}
# git worktree (move to)
gw() {
folder="$(gws)"
[ "${folder}" != '' ] || return 1
cd ${folder}
}
# git worktree create
gwc(){
suffix="${1:-}"
if [[ '' == "${suffix}" ]]; then
echo -e "Choose workspace suffix :"
read suffix
suffix="${suffix:-tmp}"
fi
worktree_path="$(gw_base_workspace)_${suffix}"
ref=${2:-main}
git worktree add --detach "${worktree_path}" ${ref} \
&& cd "${worktree_path}"
}
# git worktree rename
gwr(){
current_path=$(gws)
suffix="${1:-}"
if [[ '' == "${suffix}" ]]; then
echo -e "Choose workspace suffix :"
read suffix
suffix="${suffix:-tmp}"
fi
new_path="$(gw_base_workspace)_${suffix}"
git worktree move ${current_path} ${new_path} \
&& cd ${new_path}
}
# git worktree delete
gwd(){
folder="$(gws)"
[[ "${folder}" != '' ]] || return 1
main_worktree="$(git worktree list | sort | head -1 | awk '{print $1}')"
cwd="$(pwd)"
git worktree remove ${folder} \
&& [[ "${cwd}" == "${folder}" ]] && cd "${main_worktree}"
# jump to the 1st worktree available (usualy the default one without suffix)
}
gw_base_workspace(){
echo "$(gwl | awk '{print $1}' | sort | head -1)"
}
fi
if [[ 1 -eq $HAS_GH ]]; then
# open current pr in browser
gprv() {
gh pr view --web
}
gprw() {
gh pr checks --watch
# WIP
#tmp_file="/tmp/$(gh pr view --json 'title,url,id' --jq '.id').gprw"
#if [ ! -f "${tmp_file}" ]; then
# touch "${tmp_file}"
#fi
#rm -f "${tmp_file}"
# && notify-send k\
# ||
# gh pr view --json title,url,id
}
fi
if [[ 1 -eq $HAS_FZF ]] && [[ 1 -eq $HAS_GH ]]; then
# interactive prompt to select pull-request
gpr() {
gh pr list --limit 999 | fzf | awk '{print $1}'
}
# interactive prompt to checkout PR, will use current branch if available
# add remote if required for a fork to allow convenient push
gprco() {
pr="$(gpr)"
[ "${pr}" != '' ] || return 1
gh pr checkout ${pr}
upstream_ssh=$(gh api repos/:owner/:repo --jq '.ssh_url')
# add remote if not already available
remote_ssh=$(gh api repos/:owner/:repo/pulls/${pr} --jq '.head.repo.ssh_url')
remote_name=${remote_ssh#[email protected]:}
remote_name=${remote_name%%/*}
if [[ "${upstream_ssh}" == "${remote_ssh}" ]]; then
# PR branch is on upstream remote
remote_name=$(git remote -v|grep "${upstream_ssh}"| awk '{print $1}'|uniq)
else
# PR is not on upstream remote, add it if required
git remote -v | tr '\t' ' ' | grep "${remote_ssh}" > /dev/null
if [ $? != 0 ]; then
echo "adding remote: ${remote_name}"
git remote add ${remote_name} ${remote_ssh}
else
# remote is already registered, use local alias if defined (mostly for 'origin', which is usually my own)
remote_name=$(git remote -v | tr '\t' ' ' | grep "${remote_ssh}" | awk '{print $1}'|uniq)
fi
fi
remote_branch=$(gh api repos/:owner/:repo/pulls/${pr}|jq -Mr '.head.ref')
git fetch ${remote_name} ${remote_branch}
git push --set-upstream ${remote_name} ${remote_branch}
}
fi
fi
which vim > /dev/null
if [[ 0 -eq $? ]]; then
if [[ 1 -eq $IS_WINDOWS ]]; then
alias v='gvim'
else
alias v='vim'
alias gv='UBUNTU_MENUPROXY=gvim;gvim'
fi
fi
# Virtualbox
which vboxmanage > /dev/null
if [[ 0 -eq $? ]]; then
#TODO : add zsh autocompletion support
_waitCommandEnd() {
vboxmanage showvminfo "$1" >/dev/null
}
_powerOff(){
vboxmanage controlvm "$1" poweroff
_waitCommandEnd "$1"
}
_restoreCurrent(){
vboxmanage snapshot "$1" restorecurrent
_waitCommandEnd "$1"
}
_startVm(){
vboxmanage startvm "$1"
_waitCommandEnd "$1"
}
vbox(){
cmd="${1:-list}"
vmId="$2"
targetFile="$3"
case "$cmd" in
"usage" )
echo "usage :"
echo " vbox usage : display this help"
echo " vbox list : list VMs (and get their ID)"
echo " vbox playagain <id> : power off, restore snapshot and start vm"
echo " vbox start <id> : start vm"
echo " vbox stop <id> : stop vm (power off)"
echo " vbox export <id> [target] : export vm, defaults to box.ovf"
return 1
;;
"list" )
vboxmanage list vms | sed 's/ {/ |{/' | column -t -s \|
;;
"start" )
_startVm "$vmId"
;;
"playagain" )
_powerOff "$vmId"
_restoreCurrent "$vmId"
_startVm "$vmId"
;;
"stop" )
_powerOff "$vmId"
;;
"export" )
vboxmanage export "$vimId" --output "${targetFile:-box.ovf}"
;;
esac
return $?
}
fi
# easy filesytem nav
# thanks to http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
# TODO : bypass calls to "column" when it does not exists (like on windows)
export MARKPATH=$HOME/.marks
jump() {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
mark() {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
unmark() {
/bin/rm -i "$MARKPATH/$1"
}
marks() {
ls -l "$MARKPATH" | sed 's/\s\+/ /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' | column -t
}
j(){
# direct jump to arg or interactive prompt
[[ "${1:-}" = '' ]] \
&& jump $(marks | fzf | awk '{print $1}') \
|| jump "${1}"
}
if [[ 'zsh' = "${SHELL#/bin/}" ]];then
# zsh style completion
_completemarks() {
reply=($(ls $MARKPATH))
}
compctl -K _completemarks jump
compctl -K _completemarks unmark
fi
if [[ 'bash' = "${SHELL#/bin/}" ]];then
# bash style completion
_completemarks() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(find $MARKPATH -type l -printf "%f\n")
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}
complete -F _completemarks jump unmark
fi
# ssh "audit" : see who accesses what
sshaudit (){
for server in "$@"; do
# note : we use grep to prefix each line with file name
ssh $server -C 'sudo grep -H "^" $(sudo find /root/.ssh/ -name authorized_keys) $(sudo ls -0d /home/*/.ssh/authorized_keys)' | sort | while read line; do
tmp="$(mktemp)"
echo "$line" | sed 's/^.*://' > "$tmp" # remove file prefix
# get user from file location
[[ $line == /root/* ]] \
&& user=root \
|| user=$(echo "$line" | sed 's+/home/\(.*\)/\.ssh.*$+\1+')
echo $server $user $(ssh-keygen -lf "$tmp")
/bin/rm -f "$tmp"
done
done
}
which javap > /dev/null
if [[ 0 -eq $? ]]; then
# from .class file
jp(){
javap -c -verbose $1
}
_javap(){
tmp=$(mktemp '/tmp/XXX.javap')
javap -c -verbose $1 > ${tmp}
echo ${tmp}
}
vjp(){
tmp=$(_javap ${1})
vim ${tmp}
rm ${tmp}
}
# Open files within a .jar
jjf(){
jarFile="$(find . -name '*.jar'|fzf)"
file="$(unzip -Z1 "${jarFile}"|fzf)"
tmpFile="/tmp/jjf-$(basename ${file})"
unzip -p ${jarFile} ${file} > ${tmpFile}
if [[ '.class' == "${tmpFile: -6}" ]]; then
cat $(_javap ${tmpFile})
else
cat "${tmpFile}"
fi
rm ${tmpFile}
}
vjjp(){
vim =(jjp $1 $2)
}
vdjp(){
f1="$(_javap $1)"
f2="$(_javap $2)"
vimdiff "${f1}" "${f2}"
}
fi
if [[ -f "${VISUAL_VM_HOME}/bin/visualvm" ]];then
visualvm(){
rm -f ~/.visualvm/*/lock 2>&1 > /dev/null
echo ${JAVA_HOME}
${VISUAL_VM_HOME}/bin/visualvm --jdkhome ${JAVA_HOME}&
}
fi
which vagrant > /dev/null
if [[ 0 -eq $? ]]; then
vagu(){
vagrant up
}
vagd(){
vagrant destroy --force
}
vagdu(){
vagd
vagu
}
fi
which docker > /dev/null
if [[ 0 -eq $? ]]; then
# docker select (container)
ds(){
docker ps | sed '1d' | fzf | awk '{print $1}'
}
di(){
docker image ls | sed '1d' | fzf | awk '{print $1 ":" $2}'
}
alias d='docker'
alias dc='docker container'
alias dm='docker inspect -f "{{ .Mounts }}"'
# root shell
drsh(){
id=${1:-$(ds)}
[[ '' != "${id}" ]] && docker exec -it --user 0 ${id} bash
}
# open shell
dsh(){
id=${1:-$(ds)}
[[ '' != "${id}" ]] && docker exec -it ${id} bash
}
# get container IP
dip(){
id=${1:-$(ds)}
[[ '' != "${id}" ]] && docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${id}
}
fi
which docker-compose > /dev/null
if [[ 0 -eq $? ]]; then
alias dcc='docker-compose'
fi
which qrencode > /dev/null
if [[ 0 -eq $? ]]; then
qrcode() {
file="$(mktemp --suffix=.png)"
cat - | qrencode -t png -o "${file}"
display "${file}"
rm "${file}"
}
fi
which tmux > /dev/null
if [[ 0 -eq $? ]]; then
tm(){
# TODO: wip
tmux list-sessions
# create fixed sessions if they don't exist
# list sessions
# select with fzf then attach to it
}
fi
# per projects aliases
doc_build(){
${HOME}/dev/docs/build_docs \
--doc ${HOME}/dev/apm-agent-java/docs/index.asciidoc \
--out /tmp/doc \
--open
}
agent_dev(){
agent_folder=$(find ${HOME}/dev -maxdepth 1 -name 'apm-agent-java*'|sort|fzf)
export AGENT_FOLDER=${agent_folder}
export AGENT_JAR=$(find ${agent_folder} -name 'elastic-apm-agent-*.jar' | grep -v 'sources.jar' | grep -v 'javadoc' | grep -v 'java8' | grep -v 'premain')
export ATTACHER_JAR=$(find ${agent_folder} -name 'apm-agent-attach-cli-*.jar' | grep -v 'sources.jar' | grep -v 'javadoc' | grep -v 'slim' | grep -v 'bc-verifier')
# don't export otherwise it enables debug everywhere
DEBUG_OPTS='-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y'
echo AGENT_FOLDER=${AGENT_FOLDER}
echo AGENT_JAR=\${AGENT_FOLDER}${AGENT_JAR##${AGENT_FOLDER}}
echo ATTACHER_JAR=\${AGENT_FOLDER}${ATTACHER_JAR##${AGENT_FOLDER}}
echo "#DEBUG_OPTS=${DEBUG_OPTS}"
}
agent_dl(){
version=$(curl -s https://api.github.com/repos/elastic/apm-agent-java/tags\?per_page\=100 | jq -Mr '.[].name' | grep -v RC | grep -v stable |sed 's/^v//' | fzf)
curl -s -O https://repo1.maven.org/maven2/co/elastic/apm/elastic-apm-agent/${version}/elastic-apm-agent-${version}.jar
curl -s -O https://repo1.maven.org/maven2/co/elastic/apm/apm-agent-attach-cli/${version}/apm-agent-attach-cli-${version}.jar
export AGENT_JAR=elastic-apm-agent-${version}.jar
export ATTACHER_JAR=apm-agent-attach-cli-${version}.jar
export DEBUG_OPTS='-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y'
echo AGENT_JAR=${AGENT_JAR}
echo ATTACHER_JAR=${ATTACHER_JAR}
echo "#DEBUG_OPTS=${DEBUG_OPTS}"
}
cb(){
xclip -sel clip
}
# copy most recent downloads to current directory
cpd() {
cp ~/Downloads/$(ls -1 -t ~/Downloads | fzf) ./
}