-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases
399 lines (354 loc) · 10.7 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
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
export CHARM_HOST="charm.pipefail.com"
export EDITOR=vim
export VISUAL=vim
if type -p nvim >/dev/null ; then
alias vi="nvim -p"
alias vim="nvim -p"
alias vimdiff='nvim -d'
export EDITOR=nvim
export VISUAL=nvim
fi
alias ll='ls -la'
alias la='ls -la'
alias lr='ls -laR'
alias less='less -R '
alias bc='bc -l'
alias '..'='cd ..'
alias '...'='cd ../..'
alias '....'='cd ../..'
alias .='source'
alias isomount='sudo mount -t iso9660 -o loop'
alias ssh='ssh -A'
alias scp='scp -q'
alias chmox='chmod +x'
alias tf="terraform"
alias steam="prime-run flatpak run com.valvesoftware.Steam"
alias less="less -FIXR"
# git helpers
alias gitc="git checkout"
alias gits="git status"
alias gitb="git branch"
alias gitf="git fetch"
alias gitp="git pull"
alias gg="git grep"
alias wya="git show"
alias code='cd $(find ~/code -type d -name .git -maxdepth 3|xargs dirname |fzf --preview="git -C {} log HEAD -1")'
# git helpers with fzf
# https://seb.jambor.dev/posts/improving-shell-workflows-with-fzf/#deleting-branches
function delete-branches() {
git branch |
grep --invert-match '\*' |
cut -c 3- |
fzf --multi --preview="git log {}" |
xargs echo -- branch --delete --force
}
function pr-checkout() {
local jq_template pr_number
jq_template='"'\
'#\(.number) - \(.title)'\
'\t'\
'Author: \(.user.login)\n'\
'Created: \(.created_at)\n'\
'Updated: \(.updated_at)\n\n'\
'\(.body)'\
'"'
pr_number=$(
gh api 'repos/:owner/:repo/pulls' |
jq ".[] | $jq_template" |
sed -e 's/"\(.*\)"/\1/' -e 's/\\t/\t/' |
fzf \
--with-nth=1 \
--delimiter='\t' \
--preview='echo -e {2}' \
--preview-window=top:wrap |
sed 's/^#\([0-9]\+\).*/\1/'
)
if [ -n "$pr_number" ]; then
gh pr checkout "$pr_number"
fi
}
# kubernetes helpers
alias kctl="kubectl"
alias ktl="kubectl"
alias k="kubectl"
alias kcs="kubectl config get-contexts"
alias kui="XDG_CONFIG_HOME=~/.config k9s -c ns"
alias ktr="kubectl template render"
alias kc="kubectl config use-context"
function k8s-debug-pod-volume {
local pod_name=$1
pvc_name=$(kubectl get pod -o json "$pod_name"| jq -r '.spec.volumes[] | select(.persistentVolumeClaim != null).persistentVolumeClaim.claimName')
pv_name=$(kubectl get pvc -ojson "$pvc_name" | jq -r '.spec.volumeName')
node_name=$(kubectl get pod -o json "$pod_name" | jq -r '.spec.nodeName')
echo "pod : $pod_name"
echo "node: $node_name"
echo "PVC : $pvc_name"
echo "PV : $pv_name"
# Cleanup
if [ "$node_name" = "null" ]; then
local answer
echo "The PVC $pvc_name is associated to a stale node and should be deleted along as pod $pod_name to make it redeploy. Delete y/[n]? "
read -r answer
if [ "$answer" = "y" ]; then
kubectl delete pvc "$pvc_name" && kubectl delete pod "$pod_name"
fi
fi
}
if [[ -d ~/dd/cia/kubectl-helpers ]] ; then
source ~/dd/cia/kubectl-helpers/kgn-helpers.sh
source ~/dd/cia/kubectl-helpers/kgng-helpers.sh
fi
alias c="collins"
export PATH="$PATH":~/bin:~/local/bin:~/lang/bin:~/lang/usr/local/scala/bin:~/code/scripts:~/code/scripts/collins:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:~/.rvm/bin:/opt/local/bin:/opt/local/sbin:"$GOPATH/bin":~/.cargo/bin:~/code/go/bin:~/.local/bin
export PAGER='less'
is_git_repo(){
git rev-parse --is-inside-git-dir &>/dev/null
}
git_branch(){
git rev-parse --symbolic-full-name --abbrev-ref HEAD
}
is_git_dirty(){
[ -n "$(git status --short)" ]
}
# show the active collins dc
cdc() {
if [[ -L ~/.collins.yml ]] ; then
echo "$(readlink ~/.collins.yml|awk -F. '{print $4}')"
else
echo "?"
fi
}
# show the active kubernetes context, or use the provided context in $1
# alias kctx='c="$(kubectl config get-contexts --output=name|tail -n +1|fzf)" ; [[ -n $c ]] && kubectl config use-context $c'
alias kctx='c="$(kubectl config get-contexts |tail -n +2|sort -r|tr -d \*|awk "{print \$1}"|fzf)" ; [[ -n $c ]] && kubectl config use-context $c'
# list k8s contexts
kctxs () {
if type -p kubectl >/dev/null && test -r ~/.kube/config ; then
kubectl config get-contexts --output=name
fi
}
# https://github.com/homeport/dyff
which dyff &>/dev/null && export KUBECTL_EXTERNAL_DIFF="dyff between --omit-header --ignore-order-changes --set-exit-code"
# login to all configured clusters in kubeconfig and do the dance for oauth
kloginall() {
for ctx in $(kubectl config get-contexts -o name) ; do
kubectl login $ctx
kubectl get po -n default &>/dev/null || :
done
}
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "dont know what to do with '$1'..." ;;
esac
else
echo "'$1' is not a valid archive type"
fi
}
# throw things onto hastebin
haste(){
url="http://hastebin.com"
d="$(cat $1)" # read from file if given, else stdin
[ $? != 0 ] && return 1
r="$(curl -s -d "$d" "$url/documents")"
# if all cool, generate a link from the json response
#[ $? = 0 ] && echo "$r"|awk -F'\\W+' "{print \"$url/\"\$3}" # apparently awk on OSX is too crufty to support regex in -F
[ $? = 0 ] && echo "$r"|perl -ne "/\W+\w+\W+(\w+)\W+/ and print \"$url/\$1\n\";"
}
showcolors() {
for i in {0..255} ; do
printf "\x1b[38;5;${i}mcolour${i}\n"
done
}
random_color(){
# spits out a random color based on $RANDOM or your input
if [[ -z $1 ]] ; then
r=$RANDOM
else
h="$(md5sum <<< "$1")"
# make sure we bound the size of the int we get from converting hash to int
subst="${h:0:6}"
r="$((0x${subst%% *} ** 2))"
fi
# seems to give a better distribution if 125 is repeated
color_codes=(125 136 166 160 125 61 33 37 64)
i=$(($r % ${#color_codes[@]}))
echo -n "$(tput setaf ${color_codes[$i]})"
}
# setup ssh tunnnel
# i.e. tunnel gconradi@jumpbox dmzhost1 1235 80
# wget localhost:1235
sshtunnel(){
jump="$1"
remote="$2"
localport="${3:-1234}"
jumpport=22
remoteport="${4:-22}"
echo -e "Tunneling localhost:$localport -> $jump:$jumpport -> $remote:$remoteport\nctrl-c to stop"
echo ssh -N -L "$localport:$remote:$remoteport" "$jump"
ssh -N -L "$localport:$remote:$remoteport" "$jump"
}
collinsdc() {
if [[ -z $1 ]] ; then
l=$(basename $(readlink ~/.collins.yml))
echo "${l##.collins.yml.}"
else
if [[ ! -f ~/.collins.yml.$1 ]] ; then
echo "No collins config found for $1" >&2
return 2
fi
ln -sfn ~/.collins.yml.$1 ~/.collins.yml && echo "Using collins config for $1"
fi
}
wav2flac() {
for f in "$@" ; do
echo "Converting ${f} to flac" >&2
ffmpeg -i "$f" -qscale:a 0 "${f/%wav/flac}"
done
}
flac2mp3() {
echo "Converting ${1} to mp3" >&2
ffmpeg -i "$1" -qscale:a 0 "${1/%flac/mp3}"
}
aiff2flacbatch() {
echo "Converting *.aiff to *.flac" >&2
for f in *.aiff ; do ffmpeg -i "$f" "${f%.aiff}.flac" ; done
}
mp42m4a() {
# extract m4a from mp4 stream
for f in "${@}" ; do
echo "Ripping m4a from ${f}" >&2
ffmpeg -i "$f" -vn -acodec copy "${f%mp4}.m4a"
done
}
m4a2mp3() {
# https://superuser.com/questions/704493/ffmpeg-convert-m4a-to-mp3-without-significant-loss
for f in "${@}" ; do
echo "Converting ${f} to mp3" >&2
ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 "${f%.m4a}.mp3"
done
}
cidr2range(){
[[ -z $1 ]] && echo "Specify a CIDR as the first argument" >&2 && return 1
ruby -ripaddr -e "puts IPAddr.new(ARGV.first).to_range.to_a.map(&:to_s).join(' ')" $1
}
epoch2date(){
[[ -z $1 ]] && echo "Specify an epoch timestamp as first argument" >&2 && return 1
ruby -e 'puts "#{ARGV.first} => #{Time.at(ARGV.first.to_i).to_s}"' -- $1
}
httpdme() {
port="${1:-8000}"
echo "Launching http server on $port for root $(pwd)"
ruby -run -e httpd . -p $port
}
# git checkout PR
gitcpr() {
[[ -z $1 ]] && echo "Need to specify a PR number" >&2 && return 1
branch_name="${2:-pr-$1}"
git fetch origin pull/$1/head:$branch_name
git checkout $branch_name
echo "Checked out PR#$1 in $branch_name"
}
gitup() {
branch="${1:-$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')}"
remote="${2:-origin}"
echo "Checking out $branch (\$1) and syncing with $remote (\$2)..." >&2
git checkout $branch && git pull $remote $branch
}
makemusic() {
if ! test -d /Volumes/Portable ; then
echo "You need to plug in the hard drive to store recordings"
return 1
fi
p="/Volumes/Portable/Primary/Production/Recording"
d="$(date +%Y.%m.%d)"
[[ ! -d $p/$d ]] && mkdir $p/$d
open $p/$d
open /Applications/Audacity.app
}
# quick subnetting for IP addresses, cause i can never do the math reliably enough in my head
ipcalc() {
ruby -r ipaddr -e 'ARGV.each {|a| n = IPAddr.new(a) ; puts "#{a}\t#{n.to_range.count}\t#{n.prefix}\t#{n.to_range.first}\t#{n.to_range.last}" }' $@
}
random_pokemon() {
curl -sSL https://randompokemon.com/dex/all.json | jq -r --arg i $(($RANDOM % 898)) '.[$i|tonumber].name'
}
# example:
# scan_cidr 192.168.86.0/24
scan_cidr() {
# TODO: input validation
nmap -sP "$1"
}
# handle differences in stat formats for common task of finding file mtime
# use: stat_mtime myfile.txt
function stat_mtime {
if [[ $OSTYPE =~ darwin.* ]] ; then
stat -f %Sm -t %s $@
else
stat -c %Y $@
fi
}
function manuscript {
data_dir=Documents
remote_host="cyberdeck"
remote_host_path="${remote_host}:$data_dir/"
h="$(hostname)" # TODO: use machine ID on macos?
local_path="$data_dir/manuscript.$h.md"
te=$(date +%s)
$EDITOR ~/$local_path
if [[ $(hostname) = $remote_host ]] ; then
echo "Skipping copy to $remote_host_path (hostname=$(hostname))" >&2
else
echo -n "Copy ~/$local_path to $remote_host_path archive? (y/N) " >&2
read ans
if [[ $ans == y ]] ; then
scp ~/$local_path $remote_host_path
fi
fi
tx=$(date +%s)
echo "<- $(( tx - te ))s"
}
# backup packages for a host
function backup_package_list {
if [[ $OSTYPE =~ darwin.* ]] ; then
brewfile="code/dotfiles/brew/Brewfile.$(hostname)"
brew bundle dump
mv Brewfile ~/$brewfile
echo "$(echo ~)/$brewfile"
else
echo "implement me"
return 2
fi
}
# restore packages for a host
function restore_package_list {
if [[ $OSTYPE =~ darwin.* ]] ; then
brewfile="code/dotfiles/brew/Brewfile.$(hostname)"
echo "Restoring from ~/$brewfile..." >&2
brew bundle --file ~/$brewfile install -v
else
echo implement me
return 2
fi
}
# checksum all files in the given directories, crunch down to a single checksum
function dirhash_recursive {
du -hca "$@" >&2
hash_func="sha256sum"
echo -n "$hash_func:"
find "$@" -type f -exec "$hash_func" {} \; | awk '{print $1}' | sort | "${hash_func}"
}
if [[ -f ~/.aliases-extra ]] ; then
source ~/.aliases-extra
fi