-
Notifications
You must be signed in to change notification settings - Fork 3
/
.aliases
344 lines (272 loc) · 10.3 KB
/
.aliases
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
#!/usr/bin/env bash
# Eza / Exa
alias eza="eza -oghla --time-style long-iso --git-repos-no-status"
alias exa="eza"
alias ls="eza"
alias lg="eza --git --git-ignore"
# Easier navigation: .., ..., ...., ..... and -
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias -- -="cd -"
alias g="git"
alias h="history"
# deactivated in favour of "autojump"
#alias j="jobs"
alias V="vagrant"
alias VV="vagrant global-status"
# Terraform
alias tg="terragrunt"
alias tf="terraform"
alias tffr="terraform fmt -recursive -diff"
alias tfppl="terraform providers lock \
-platform=linux_amd64 \
-platform=darwin_amd64 \
-platform=darwin_arm64 \
-platform=windows_amd64"
alias tfpo="terraform plan -out=out.tfplan"
# Just
just_bootstrap() {
if [[ -f justfile ]]; then
echo "Error: justfile already exists"
return 1
fi
curl -sL "https://raw.githubusercontent.com/dsiebel/repository-template/main/Justfile" > justfile
}
# Make
make_bootstrap() {
if [[ -f Makefile || -f .bootstrap.mk ]]; then
echo "Error: make-bootstrap seems to be installed already."
return 1
fi
curl -LO "https://raw.githubusercontent.com/dsiebel/make-bootstrap/master/.bootstrap.mk"
curl -LO "https://raw.githubusercontent.com/dsiebel/make-bootstrap/master/Makefile"
}
# Process chooser for caffeinate, to prevent mac from sleeping
alias caf='ps | fzf | cut -w -f 1 | xargs caffeinate -dimw'
# Process based top (like Activity Monitor)
alias ptop='top -stats pid,command,cpu,idlew,power -o power -d'
# Git
alias gbav='git branch -av'
alias gbll="git for-each-ref --format='%(refname)' 'refs/heads/**'"
alias gbls="git branch --list --format='%(refname:short)'"
alias gds='git diff --staged'
alias glf="git log --oneline --abbrev-commit --date=relative --pretty=format:'%h %s (%cr) <%an>' | fzf --height 100% --multi --preview 'git show {+1}'"
alias glfs="git log --oneline --abbrev-commit --date=relative --pretty=format:'%h %s (%cr) <%an>' | fzf --height 100% --multi --preview 'git show {+1}' | xargs -I1 git show"
## git branches via fzf
alias gw='git branch | fzf | xargs git switch'
## git worktree
#alias gwinit='__git_worktree_init'
#alias gw='cd "$(git worktree list | cut -f1 -d" " | grep -v ".bare" | fzf || echo ".")"'
#alias gwa='git worktree add'
#alias gwaf='git worktree add -f'
#alias gwab='git worktree add -b'
#alias gwls='git worktree list'
#alias gwl='git worktree lock'
#alias gwmv='git worktree move'
#alias gwp='git worktree prune'
#alias gwrm='git worktree remove'
#alias gwul='git worktree unlock'
__git_worktree_init() {
local repo="${1?Argument 1 (repository) is required}"
local dir="${2:-$(basename "${repo%.git}")}"
if [[ ! -d "${dir}" ]]; then
echo "Creating directory ${dir}"
mkdir -p "${dir}"
fi
cd "${dir}" || :
if [[ -f "${dir}/.git" || -d "${dir}/.git" ]]; then
echo "ERROR: .git file/directory already exists."
return
fi
git clone --bare "${repo}" .bare
echo "gitdir: ./.bare" > .git
if [[ -n "$2" ]]; then
git worktree add "$2"
fi
}
# Docker
alias D="docker"
alias DR="docker run --rm -it"
alias DC="docker-compose"
alias DM="docker-machine"
alias deti="docker exec -ti"
alias drti="docker run --rm -ti"
alias drtib="docker run --rm -ti --entrypoint bash"
alias drtis="docker run --rm -ti --entrypoint sh"
alias dps="docker ps"
alias dpsa="docker ps -a"
## Damn typo!
alias docker-composer='docker-compose'
## Delete all stopped containers and untagged images
alias docker-cleanup-stopped='docker rm $(docker ps -aq -f status=exited)'
alias docker-cleanup-untagged='docker rmi $(docker images -q --filter dangling=true)'
alias docker-cleanup="docker-cleanup-stopped; docker-cleanup-untagged"
## Remove all containers, images and networks created by docker-compose up
alias docker-compose-cleanup="_docker_compose_cleanup"
_docker_compose_cleanup() {
docker-compose -f "${1:-docker-compose.yml}" down --rmi all
}
# Google Cloud SDK
gcloudcfg() {
if (( $# == 0 )); then
gcloud config configurations list
elif (( $# == 1 )); then
gcloud config configurations activate "$@"
else
gcloud config configurations "$@"
fi
}
alias G="gcloud"
alias GX="gcloudcfg"
# Kubernetes and tools
alias K="kubectl"
alias KX="kubectx"
alias KNS="kubens"
alias I="istioctl"
alias MK="minikube"
alias kx="kubie ctx"
alias kns="kubie ns"
alias kgpall="kubectl get pods --all-namespaces"
alias kgpwall="kubectl get pods --all-namespaces -w"
alias kdebug="kubectl run test-\$(whoami)-\$(date +%Y%m%d%H%M%S) --rm --restart=Never -it --image=fleshgrinder/debian:stretch-debug -- bash"
alias kg="kubectl get"
alias kd="kubectl describe"
alias kdel="kubectl delete"
alias ke="kubectl edit"
alias ka="kubectl apply"
alias kcl="kubectl config get-contexts"
alias kcv="kubectl config view"
alias kcx="kubectl config current-context"
alias kcux="kubectl config use-context"
alias kdf="kubectl diff -f"
alias kgimg="kubectl get pods -o jsonpath='{..image}' | tr -s '[[:space:]]' \"\\n\" | sort -u"
alias khealth="kubectl get --raw='/healthz?verbose'"
alias krs="kubectl rollout status"
alias kp="kubectl patch"
alias kpd="kubectl patch deployment"
alias kpdd="__kubectl_patch_date deployment"
alias kpdw="__kubectl_patch_date_watch"
alias kpdwd="__kubectl_patch_date_watch deployment"
alias kkillc="__kubectl_kill_container"
alias kdaf="__kubectl_diff_apply"
__kubectl_kill_container() {
kubectl exec -i "$1" -c "$2" -- kill -15 1
}
__kubectl_patch_date() {
kubectl patch "$1" "$2" -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"$(date -u +"%s")\"}}}}}"
}
__kubectl_patch_date_watch() {
kubectl patch "$1" "$2" -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"$(date -u +"%s")\"}}}}}"
kubectl rollout status "$1" "$2"
}
__kubectl_diff_apply() {
local target="${1?Argument 1 (directory/file) is required}"
shift
local args=("$@")
if [[ -d "${target}" ]]; then
args+=("--recursive")
fi
kubectl diff "${args[@]}" --filename "${target}"
local has_diff="$?"
if (( has_diff == 0 )); then
echo "No changes"
return 0
elif (( has_diff > 1)); then
return "${has_diff}"
fi
echo
while true; do
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "Proceed? [y/n]: "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r yn </dev/tty
case $yn in
Y*|y*)
kubectl apply "${args[@]}" --filename "${target}"
return 0
#return $(kubectl apply "${args[@]}" --filename "${target}")
;;
N*|n*)
echo "Aborted."
return 1
;;
esac
done
}
__kubectl_delete_evicted_pods() {
args=()
if [[ "$1" == "all" ]]; then
args+=("--all-namespaces")
else
args+=("--namespace" "$1")
fi
kubectl get pods "${args[@]}" -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c
}
# Skaffold
alias S="\${SKAFFOLD_EXEC_PATH:-skaffold}"
alias s="\${SKAFFOLD_EXEC_PATH:-skaffold}"
alias sbi="S build --build-image"
alias srt="S run --tail"
alias stt="S render --digest-source tag"
# Get week number
alias week='date +%V'
alias dad='curl -H "Accept: text/plain" https://icanhazdadjoke.com/'
# Stopwatch
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'
# BackupBackup via rsync
# I can't seem to remember the options
alias backup='rsync -vahWEP --stats'
# IP addresses
alias pubip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\\.){3}[0-9]*' | grep -Eo '([0-9]*\\.){3}[0-9]*' | grep -v '127.0.0.1'"
alias ips="sudo ifconfig -a | grep -o 'inet6\\? \\(addr:\\)\\?\\s\\?\\(\\(\\([0-9]\\+\\.\\)\\{3\\}[0-9]\\+\\)\\|[a-fA-F0-9:]\\+\\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"
# Show active network interfaces
alias ifactive="ifconfig | pcregrep -M -o '^[^\\t:]+:([^\\n]|\\n\\t)*status: active'"
# Flush Directory Service cache
alias dsflush="dscacheutil -flushcache && killall -HUP mDNSResponder"
# Edit hosts file and flush Directory Service cache afterwards
alias hosts='sudo vim /etc/hosts; dsflush'
# View HTTP traffic
alias sniff="sudo ngrep -d 'en1' -t '^(GET|POST) ' 'tcp and port 80'"
alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E \"Host\\: .*|GET \\/.*\""
# Empty the Trash on all mounted volumes and the main HDD.
# Also, clear Apple’s System Logs to improve shell startup speed.
# Finally, clear download history from quarantine. https://mths.be/bum
alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl; sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent'"
# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
# Hide/show all desktop icons (useful when presenting)
alias hidedesktop="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
alias showdesktop="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"
# URL-encode strings
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"'
# Ring the terminal bell, and put a badge on Terminal.app’s Dock icon
# (useful when executing time-consuming commands)
alias badge="tput bel"
# One of @janmoesen’s ProTip™s
for method in GET HEAD POST PUT DELETE TRACE OPTIONS; do
# shellcheck disable=SC2139,SC2140
alias "$method"="lwp-request -m \"$method\""
done
# Kill all the tabs in Chrome to free up memory
# [C] explained: http://www.commandlinefu.com/commands/view/402/exclude-grep-from-your-grepped-output-of-ps-alias-included-in-description
alias chromekill="ps ux | grep '[C]hrome Helper --type=renderer' | grep -v extension-process | tr -s ' ' | cut -d ' ' -f2 | xargs kill"
# Lock the screen (when going AFK)
alias afk="/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
# copy file interactive
alias cp='cp -i'
# move file interactive
alias mv='mv -i'
# untar
alias untar='tar -xvf'
# Reload the shell (i.e. invoke as a login shell)
# shellcheck disable=SC2139
alias reload="exec $SHELL -l"
# Print each PATH entry on a separate line
alias path='echo -e ${PATH//:/\\n}'
# quickly generate a random hexadecimal 32 character password
alias genpwd='openssl rand hex 32'
alias x='extract'