-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
1444 lines (1191 loc) · 36.9 KB
/
.bashrc
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#===============================================================================
# Part of Dave James Miller's dotfiles - https://github.com/d13r/dotfiles
# To uninstall: source ~/.dotfiles/uninstall
#===============================================================================
#
# This file is executed when Bash is loaded, but ONLY in an interactive session
#
# Also see: .bash_profile
#
#===============================================================================
# Setup
#===============================================================================
#---------------------------------------
# Safety checks
#---------------------------------------
# Make sure .bash_profile is loaded first
source ~/.bash_profile
# Only load this file once
[[ -n $BASHRC_SOURCED ]] && return
BASHRC_SOURCED=true
# Only load in interactive shells
[[ -z $PS1 ]] && return
# Prevent errors if $HOME is not set (e.g. Proxmox after running Upgrade)
[[ -z $HOME ]] && return
#---------------------------------------
# Third party scripts
#---------------------------------------
# Auto-completion - seems to be loaded automatically on some servers but not on others
shopt -s extglob
[[ -f /etc/bash_completion ]] && source /etc/bash_completion
if ! declare -f _completion_loader &>/dev/null; then
# Lazy-load not available
for file in $HOME/.local/share/bash-completion/completions/*; do
source "$file"
done
fi
# fzf - fuzzy finder
if [[ -f ~/.fzf.bash ]]; then
# Manual install
source ~/.fzf.bash
else
# Ubuntu package
[[ -f /usr/share/doc/fzf/examples/key-bindings.bash ]] && source /usr/share/doc/fzf/examples/key-bindings.bash
[[ -f /usr/share/doc/fzf/examples/completion.bash ]] && source /usr/share/doc/fzf/examples/completion.bash
fi
# Google Cloud Shell
[[ -f /google/devshell/bashrc.google ]] && source /google/devshell/bashrc.google
# lesspipe
[[ -x /usr/bin/lesspipe ]] && eval "$(/usr/bin/lesspipe)"
# nvm - https://github.com/nvm-sh/nvm#installing-and-updating
export NVM_DIR="$HOME/.nvm"
[[ -s "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
[[ -s "$NVM_DIR/bash_completion" ]] && source "$NVM_DIR/bash_completion"
# Python virtualenv
if [[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]]; then
export VIRTUAL_ENV_DISABLE_PROMPT=1
source /usr/local/bin/virtualenvwrapper_lazy.sh
fi
# Ruby rvm
if [[ -s ~/.rvm/scripts/rvm ]]; then
rvm_project_rvmrc=0
source ~/.rvm/scripts/rvm
fi
#===============================================================================
# Aliases
#===============================================================================
# Can't use 'maybe-sudo' because then 'sudo <something>' is expanded to
# 'sudo maybe-sudo <something>' which fails to run
if [[ $EUID -gt 0 ]] && is-executable sudo; then
sudo='sudo'
else
sudo=''
fi
if [[ $EUID -gt 0 ]] && is-executable sudo && ! id -nG | command grep -wq docker; then
sudo_for_docker='sudo'
else
sudo_for_docker=''
fi
alias a2disconf="$sudo a2disconf"
alias a2dismod="$sudo a2dismod"
alias a2dissite="$sudo a2dissite"
alias a2enconf="$sudo a2enconf"
alias a2enmod="$sudo a2enmod"
alias a2ensite="$sudo a2ensite"
alias aar="$sudo add-apt-repository"
alias acs='command apt search' # } Sudo not needed
alias acsh='command apt show' # }
alias addgroup="$sudo addgroup"
alias adduser="$sudo adduser"
alias agac="$sudo apt autoclean"
alias agar="$sudo apt autoremove"
alias agi="$sudo apt install"
alias agp="$sudo apt purge --auto-remove"
alias agr="$sudo apt remove --auto-remove"
alias agu="$sudo apt update && $sudo apt full-upgrade --auto-remove"
alias agupdate="$sudo apt update"
alias agupgrade="$sudo apt upgrade"
alias ap='ansible-playbook'
alias apt-add-repository="$sudo apt-add-repository"
alias apt-mark="$sudo apt-mark"
alias apt="$sudo apt"
alias art='artisan'
alias b='bin'
if is-executable batcat && ! is-executable bat; then
alias bat='batcat'
fi
alias cas='c /etc/apache2/sites-available'
alias cat="$HOME/.bin/bat-or-cat"
alias certbot="$sudo certbot"
alias chmox='chmod' # Common typo
alias cp='cp -i'
alias cy='cypress'
alias dpkg-reconfigure="$sudo dpkg-reconfigure"
alias db='dbeaver'
alias dc='docker-compose' # See function below
alias dcr='drush cr'
alias dive="$sudo_for_docker dive"
alias docker="$sudo_for_docker docker"
alias gcm='g co master'
alias goland='_idea goland'
alias grep='grep-less'
alias groupadd="$sudo groupadd"
alias groupdel="$sudo groupdel"
alias groupmod="$sudo groupmod"
alias host='_domain-command host'
alias k='kubectl'
alias ka='kubectl apply -f'
alias kcx='kubectl config use-context'
alias kd='kubectl describe'
alias ke='kubectl edit'
alias kg='kubectl get'
alias kns='kubectl config set-context --current --namespace'
alias krm='kubectl delete'
if is-mac; then
ls_common='-GF'
else
ls_common="-hF --color=always --hide='*.pyc' --hide='*.sublime-workspace' --hide='\$RECYCLE.BIN' --hide='desktop.ini'"
fi
alias l="ls $ls_common -l"
alias la="ls $ls_common -lA"
alias land='_idea goland'
alias ll="ls $ls_common -l"
alias ls="ls $ls_common"
alias lsa="ls $ls_common -A"
alias mux='tmuxinator'
alias ncdu='ncdu --color dark'
alias nslookup='_domain-command nslookup'
alias php5dismod="$sudo php5dismod"
alias php5enmod="$sudo php5enmod"
alias phpdismod="$sudo phpdismod"
alias phpenmod="$sudo phpenmod"
alias phpstorm='_idea phpstorm'
alias poweroff="$sudo poweroff"
alias pow="$sudo poweroff"
alias pu='phpunit'
alias puw='when-changed -r -s -1 app database tests -c "clear; bin/phpunit"'
alias pw='PASSWORD_STORE_DIR="$HOME/.password-store/MI" pvview'
alias reboot="$sudo reboot"
alias reload='exec bash -l'
alias rm='rm -i'
alias s='sudo '
alias scra="$sudo systemctl reload apache2 && $sudo systemctl status apache2"
alias service="$sudo service"
alias shutdown="$sudo poweroff"
alias sshak='ssh -o StrictHostKeyChecking=accept-new'
alias sshstop='ssh -O stop'
alias storm='_idea phpstorm'
alias sudo='sudo ' # Expand aliases
alias sw='sw ' # Expand aliases
alias tailscale="$sudo tailscale"
alias tree='tree -C'
alias u='c ..'
alias uu='c ../..'
alias uuu='c ../../..'
alias uuuu='c ../../../..'
alias uuuuu='c ../../../../..'
alias uuuuuu='c ../../../../../..'
alias ufw="$sudo ufw"
alias updb='drush updb -y'
alias updatedb="$sudo updatedb"
alias update-grub="$sudo update-grub"
alias useradd="$sudo useradd"
alias userdel="$sudo userdel"
alias usermod="$sudo usermod"
alias v='vagrant'
alias watch='watch --color '
alias whois='_domain-command whois'
alias yum="$sudo yum"
#===============================================================================
# Functions
#===============================================================================
# This is a valid function name, but not a valid alias name
-() {
cd - "$@"
}
c() {
# 'cd' and 'ls'
if [[ $@ != . ]]; then
cd "$@" >/dev/null || return
fi
_ls-current-directory
}
cd() {
local dir=$PWD
builtin cd "$@" &&
_dirhistory-push-past "$dir" &&
_record-last-directory
}
cg() {
# cd to git root
# Look in parent directories
path=$(cd .. && git rev-parse --show-toplevel 2>/dev/null)
# Look in child directories
if [[ -z $path ]]; then
path=$(find . -mindepth 2 -maxdepth 2 -type d -name .git 2>/dev/null)
if [[ $(echo "$path" | wc -l) -gt 1 ]]; then
echo 'Multiple repositories found:' >&2
echo "$path" | sed 's/^.\// /g; s/.git$//g' >&2
return 2
else
path=${path%/.git}
fi
fi
# Go to the directory, if found
if [[ -z $path ]]; then
echo 'No Git repository found in parent directories or immediate children' >&2
return 1
fi
c "$path"
}
com() {
if [[ ${1-} = 'ul' ]]; then
shift
composer update --lock "$@"
else
composer "$@"
fi
}
cv() {
if ! local dir=$(findup -d vendor/d13r); then
echo 'No vendor/d13r/ directory found' >&2
return 1
fi
if [[ -z $1 ]]; then
c "$dir/vendor/d13r"
elif [[ -d "$dir/vendor/d13r/$1" ]]; then
c "$dir/vendor/d13r/$1"
else
local matches=("$dir/vendor/d13r/"*"$1"*)
if [[ ${#matches[@]} -eq 0 || ! -d "${matches[0]}" ]]; then
c "$dir/vendor/d13r"
echo >&2
c "$1" # Will fail
elif [[ ${#matches[@]} -eq 1 ]]; then
c "${matches[0]}"
else
c "$dir/vendor/d13r"
echo >&2
echo 'Multiple matches found:' >&2
printf '%s\n' "${matches[@]}" >&2
fi
fi
}
composer() {
if dir=$(findup -x bin/composer); then
"$dir/bin/composer" "$@"
else
command composer "$@"
fi
}
cw() {
# cd to web root
if [[ -d ~/repo ]]; then
c ~/repo # cPanel
elif [[ -d /local ]]; then
c /local # Maths
elif [[ -d /var/www ]]; then
c /var/www # Ubuntu
elif is-wsl; then
c "$(wsl-documents-path)" # WSL
else
echo 'web root not found' >&2
fi
}
cwc() {
# cd to wp-content/
wp_content=$(_find-wp-content) || return
c $wp_content
}
cwp() {
# cd to WordPress plugins
wp_content=$(_find-wp-content) || return
if [ -d $wp_content/plugins ]; then
c $wp_content/plugins
else
echo "Cannot find wp-content/plugins/ directory" >&2
return 1
fi
}
cwt() {
# cd to WordPress theme
wp_content=$(_find-wp-content) || return
if [ -d $wp_content/themes ]; then
wp_theme=$(find $wp_content/themes -mindepth 1 -maxdepth 1 -type d -not -name twentyten -not -name twentyeleven)
if [ $(echo "$wp_theme" | wc -l) -eq 1 ]; then
# Only 1 non-default theme found - assume we want that
c $wp_theme
else
# 0 or 2+ themes found - go to the main directory
c $wp_content/themes
fi
else
echo "Cannot find wp-content/themes/ directory" >&2
return 1
fi
}
desc() {
if [[ $# -ne 1 ]]; then
echo 'Usage: desc COMMAND' >&2
return 1
fi
local command=$1
if [[ $(type -t "$command") != 'file' ]]; then
type "$command"
return
fi
local file=$(command -v "$command")
local description=$(file "$file")
if [[ $description = *ASCII* || $description = *text* ]]; then
BAT_STYLE='header,grid' cat "$file"
else
echo "$description"
fi
}
docker-compose() {
if dir=$(findup -x bin/docker-compose); then
"$dir/bin/docker-compose" "$@"
else
command maybe-sudo-for-docker docker-compose "$@"
fi
}
drupal() {
if dir=$(findup -f bin/drupal); then
"$dir/bin/drupal" "$@"
elif dir=$(findup -f vendor/bin/drupal); then
"$dir/vendor/bin/drupal" "$@"
else
command drupal "$@"
fi
}
drush() {
if dir=$(findup -f bin/drush); then
"$dir/bin/drush" "$@"
elif dir=$(findup -f vendor/bin/drush); then
"$dir/vendor/bin/drush" "$@"
else
command drush "$@"
fi
}
dump-path() {
echo -e "${PATH//:/\\n}"
}
exitif() {
test "$@" && exit || return 0
}
g() {
if [[ $# -gt 0 ]]; then
git "$@"
elif is-executable lazygit; then
lazygit
else
git status
fi
}
gs() {
if [[ $# -eq 0 ]]; then
# 'gs' typo -> 'g s'
g s
else
command gs "$@"
fi
}
hacked() {
# Switch a Composer package to dist mode
# Has to be a function because it deletes & recreates the working directory
if [ "$(basename "$(dirname "$(dirname "$PWD")")")" != "vendor" ]; then
echo "Not in a Composer vendor directory" >&2
return 1
fi
if [ ! -e .git ]; then
echo "Not in development mode" >&2
return 1
fi
if [ -n "$(git status --porcelain)" ]; then
echo "There are uncommitted changes" >&2
return 1
fi
ask "Delete this directory and reinstall in production mode?" Y || return
local package=$(basename "$(dirname "$PWD")")/$(basename "$PWD")
local oldpwd=${OLDPWD:-}
local pwd=$PWD
# Delete the dev version
cd ../../..
rm -rf "$pwd"
# Install the dist version
composer update --prefer-dist "$package"
# Go back to that directory + restore "cd -" path
cd "$pwd"
OLDPWD=$oldpwd
}
hackit() {
# Switch a Composer package to dev (source) mode
# Has to be a function because it deletes & recreates the working directory
if [ "$(basename "$(dirname "$(dirname "$PWD")")")" != "vendor" ]; then
echo "Not in a Composer vendor directory" >&2
return 1
fi
if [ -e .git ]; then
echo "Already in development mode" >&2
return 1
fi
ask "Delete this directory and reinstall in development mode?" Y || return
local package=$(basename "$(dirname "$PWD")")/$(basename "$PWD")
local oldpwd=${OLDPWD:-}
local pwd=$PWD
# Delete the dist version
cd ../../..
rm -rf "$pwd"
# Install the dev version
composer update --prefer-source "$package"
# Go back to that directory + restore "cd -" path
cd "$pwd"
OLDPWD=$oldpwd
# Switch to the latest development version
echo
git checkout master
}
ide() {
if findup -f .idea/php.xml >/dev/null; then
_idea phpstorm
elif findup -f .idea/go.xml >/dev/null; then
# Untested (3 Dec 2024)
_idea goland
elif findup -d .idea >/dev/null; then
echo 'Cannot determine the project type'
return 1
else
echo 'Cannot find .idea/ folder'
return 1
fi
}
man() {
# http://boredzo.org/blog/archives/2016-08-15/colorized-man-pages-understood-and-customized
# https://unix.stackexchange.com/questions/119/colors-in-man-pages/147#comment488743_147
# mb=start blink, mb=start bold, me=end blink/bold
# us=start underline, ue=end underline
# so=start standout (file information footer), se=end standout
LESS_TERMCAP_mb=$'\e[91;5m' \
LESS_TERMCAP_md=$'\e[96m' \
LESS_TERMCAP_me=$'\e[0m' \
LESS_TERMCAP_us=$'\e[97;4m' \
LESS_TERMCAP_ue=$'\e[0m' \
LESS_TERMCAP_so=$'\e[37;100m' \
LESS_TERMCAP_se=$'\e[0m' \
GROFF_NO_SGR=1 \
command man "$@"
}
mark() {
mkdir -p $HOME/.marks
local mark=${1:-$(basename "$PWD")}
local target=${2:-$PWD}
if ! [[ $mark =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo "Invalid mark name"
return 1
fi
ln -nsf "$target" "$HOME/.marks/$mark" &&
alias $mark="c -P '$target'"
}
marks() {
mkdir -p $HOME/.marks
if is-mac; then
CLICOLOR_FORCE=1 command ls -lF "$HOME/.marks" | sed '1d;s/ / /g' | cut -d' ' -f9-
else
command ls -l --color=always "$HOME/.marks" | sed '1d;s/ / /g' | cut -d' ' -f9- | {
if is-executable column; then
column -t
else
cat
fi
}
fi
}
md() {
mkdir -p "$1" && cd "$1"
}
mv() {
# 'mv' - interactive if only one filename is given
# https://gist.github.com/premek/6e70446cfc913d3c929d7cdbfe896fef
if [ "$#" -ne 1 ]; then
command mv -i "$@"
elif [ ! -e "$1" ]; then
command file "$@"
else
read -p "Rename to: " -ei "$1" newfilename &&
[[ -n $newfilename ]] &&
mv -iv "$1" "$newfilename"
fi
}
nextd() {
local dir=$PWD
while [[ ${dirhistory_future[0]} == $dir ]]; do
dirhistory_future=("${dirhistory_future[@]:1}")
done
if [[ ${#dirhistory_future[@]} -gt 0 ]]; then
if builtin cd "${dirhistory_future[0]}"; then
_dirhistory-push-past "$dir"
_record-last-directory
_ls-current-directory
fi
fi
}
php() {
if dir=$(findup -x bin/php); then
"$dir/bin/php" "$@"
else
command php "$@"
fi
}
prevd() {
local dir=$PWD
while [[ ${dirhistory_past[0]} == $dir ]]; do
dirhistory_past=("${dirhistory_past[@]:1}")
done
if [[ ${#dirhistory_past[@]} -gt 0 ]]; then
if builtin cd "${dirhistory_past[0]}"; then
_dirhistory-push-future "$dir"
_record-last-directory
_ls-current-directory
fi
fi
}
prompt() {
local set_titlebar=false
prompt_style=''
while [[ -n $1 ]]; do
case "$1" in
# Stop parsing parameters
--) shift; break ;;
# Set titlebar as well
-t|--titlebar) set_titlebar=true ;;
# Presets
-l|--live) prompt_style='bg=red' ;;
-s|--staging) prompt_style='bg=yellow,fg=black' ;;
-d|--dev) prompt_style='bg=green,fg=black' ;;
-x|--special) prompt_style='bg=blue' ;;
# Other colours/styles (e.g. '--bg=red' - see ~/.bash/style)
--*) prompt_style="$prompt_style,${1:2}" ;;
# Finished parsing parameters
*) break ;;
esac
shift
done
prompt_message="$@"
if $set_titlebar; then
titlebar_message="$@"
fi
}
sc() {
case "${1:-}" in
d|down) shift; systemctl stop "$@" ;;
e) shift; systemctl edit "$@" ;;
l) shift; systemctl log "$@" ;;
r) shift; systemctl reload-or-restart "$@" ;;
rl) shift; systemctl reload "$@" ;;
rs) shift; systemctl restart "$@" ;;
s) shift; systemctl status "$@" ;;
u|up) shift; systemctl start "$@" ;;
*) systemctl "$@"
esac
}
scratch() {
if [[ ! -d /scratch ]]; then
echo '/scratch does not exist' >&2
return 1
fi
if [[ ! -d "/scratch/$USER" ]]; then
(umask 077 && mkdir -p "/scratch/$USER")
# Also create temp directory since I have a symlink in ~/temp
mkdir -p "/scratch/$USER/temp"
fi
c "/scratch/$USER"
}
setup() {
local basename name
# If a name is given, that's either a global script, subdirectory or repo URL
if [[ $# -gt 0 ]]; then
name=$1
# Global script
if [[ -f "$HOME/.bin/setup/$name" ]]; then
shift
"$HOME/.bin/setup/$name" "$@"
return
fi
# Repo URL or existing subdirectory
basename=$(basename "$name")
if [[ ! -e $basename ]]; then
# The 'clone' helper supports various short URL formats
clone "$name" "$basename"
echo
fi
cd "$basename"
fi
# If the setup script is in the normal place, call it directly
if [[ -x bin/setup ]]; then
bin/setup
return
fi
# Otherwise use 'bin' to find it (it must be installed first)
bin setup
}
status() {
# Show the result of the last command
local status=$?
if [[ $status -eq 0 ]]; then
style bg=lgreen,fg=black 'Success'
else
style bg=red,fg=lwhite "Failed with code $status"
fi
return $status
}
sudo() {
# Preserve the path - https://stackoverflow.com/a/29400598/167815
if [[ $1 = 'cp' || $1 = 'mv' || $1 = 'rm' ]]; then
# Add additional safety checks for cp, mv, rm
exe=$1
shift
sudo-preserve-env "$exe" -i "$@"
else
sudo-preserve-env "$@"
fi
}
sw() {
if [[ $# -gt 0 ]]; then
sudo -u www-data "$@"
else
sudo -su www-data
fi
}
systemctl() {
if [[ -n ${COMP_WORDS:-} ]]; then
# Bash completion (no sudo because it would interrupt the prompt asking for a password)
command systemctl "$@"
elif in-array '--user' "$@"; then
# User mode (no sudo)
command systemctl "$@"
elif [[ ${1:-} = 'dr' ]]; then
# Alias: dr=daemon-reload
shift
maybe-sudo systemctl daemon-reload "$@"
elif [[ ${1:-} = 'log' ]]; then
# Custom command: sc log [unit] [grep]
if [[ -n ${3:-} ]]; then
maybe-sudo journalctl --lines 100 --follow --unit "$2" --grep "$3"
elif [[ -n ${2:-} ]]; then
maybe-sudo journalctl --lines 100 --follow --unit "$2"
else
maybe-sudo journalctl --lines 100 --follow
fi
else
maybe-sudo systemctl "$@"
fi
}
terraform() {
if dir=$(findup -f bin/terraform); then
"$dir/bin/terraform" "$@"
else
command terraform "$@"
fi
}
tf() {
case "${1:-}" in
a) # Apply
shift
terraform apply "$@"
;;
an) # Apply No Refresh
shift
terraform apply -refresh=false "$@"
;;
ar) # Apply Refresh Only
shift
terraform apply -refresh-only "$@"
;;
i) # Init
shift
terraform init "$@"
;;
ia) # Init and Apply
shift
terraform init && terraform apply "$@"
;;
ian) # Init and Apply No Refresh
shift
terraform init && terraform apply -refresh=false "$@"
;;
iar) # Init and Apply Refresh Only
shift
terraform init && terraform apply -refresh-only "$@"
;;
mv) # State Move
shift
terraform state mv "$@"
;;
o) # Output
shift
terraform output "$@"
;;
p) # Plan
shift
terraform plan "$@"
;;
v) # Validate
shift
terraform validate "$@"
;;
*)
terraform "$@"
;;
esac
}
unmark() {
local marks=${@:-$(basename "$PWD")}
for mark in $marks; do
if [[ -L $HOME/.marks/$mark ]]; then
rm -f "$HOME/.marks/$mark" && unalias $mark
else
echo "No such mark: $mark" >&2
fi
done
}
xdebug() {
if [[ ${1:-} = 'on' ]]; then
export XDEBUG_SESSION=${2:-1}
elif [[ ${1:-} = 'off' ]]; then
unset XDEBUG_SESSION
fi
if [[ ${XDEBUG_SESSION:-} = 1 ]]; then
echo "Xdebug step debugging is enabled"
elif [[ -n ${XDEBUG_SESSION:-} ]]; then
echo "Xdebug step debugging is enabled (trigger_value=$XDEBUG_SESSION)"
else
echo "Xdebug step debugging is disabled"
fi
}
yarn() {
# Make 'yarn' more like 'composer'
case $1 in
in|ins) shift; args=(install) ;;
out) shift; args=(outdated) ;;
re|rem) shift; args=(remove) ;;
up|update) shift; args=(upgrade) ;;
*) args=() ;;
esac
if dir=$(findup -x bin/yarn); then
"$dir/bin/yarn" "${args[@]}" "$@"
else
command yarn "${args[@]}" "$@"
fi
}
#---------------------------------------
# Helper functions
#---------------------------------------
_dirhistory-push-future() {
if [[ ${#dirhistory_future[@]} -eq 0 || ${dirhistory_future[0]} != "$1" ]]; then
dirhistory_future=("$1" "${dirhistory_future[@]:0:49}")
fi
}
_dirhistory-push-past() {
if [[ ${#dirhistory_past[@]} -eq 0 || ${dirhistory_past[0]} != "$1" ]]; then
dirhistory_past=("$1" "${dirhistory_past[@]:0:49}")
fi
}
_domain-command() {
command=$1
shift
# Accept URLs and convert to domain name only
domain=$(echo "$1" | sed 's#https\?://\([^/]*\).*/#\1#')
if [[ -n $domain ]]; then
shift
command $command "$domain" "$@"
else
command $command "$@"
fi
}
_find-wp-content() {
if dir=$(findup -d wp-content); then
echo "$dir/wp-content"
elif dir=$(findup -d public/wp-content); then
echo "$dir/public/wp-content"
else
echo "Cannot find wp-content/ directory" >&2
return 1
fi
}
_idea() {
local ide=$1
local args=()
local path
shift
if [[ $# -eq 0 ]] && path=$(findup -d .idea); then
# Automatically launch the current project
if is-wsl; then
path=$(wslpath -aw "$path" | sed 's/\\\\wsl.localhost\\/\\\\wsl$\\/')
fi
args=($path)
elif [[ -d ${1:-} ]] && is-wsl; then
# Convert the path to WSL format
path=$(wslpath -aw "$1" | sed 's/\\\\wsl.localhost\\/\\\\wsl$\\/')
shift
args=($path)
fi
# Run the IDE in the background
command $ide "${args[@]}" "$@" &>> "$HOME/.cache/$ide.log" &
}
_ls-current-directory() {
echo
style lwhite,bold,underline "$PWD"