-
Notifications
You must be signed in to change notification settings - Fork 4
/
dot_utils.sh.tmpl
303 lines (270 loc) · 6.88 KB
/
dot_utils.sh.tmpl
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
alias ls='ls -FG'
alias ll='ls -lah'
# alias ll='exa -lah'
alias mv='mv -nv'
alias unlock="security unlock-keychain ~/Library/Keychains/login.keychain"
alias rehash='hash -r'
alias p4="ping 4.2.2.2"
which prettyping >/dev/null && alias ping="prettyping"
alias ia="open -a 'IA Writer' "
alias cz=chezmoi
alias gdiff="git diff --no-index"
alias kcatss="kcat -X security.protocol=ssl -X enable.ssl.certificate.verification=false"
which bat >/dev/null && {
alias cat="bat"
export BAT_THEME='base16'
}
{{ if .shared }}
# configure the shell for a kube context and namespace
kc() {
options() {
kubectl config get-contexts -o name | parallel -i -- \
"kubectl --context {} get ns -o name | sed 's/namespace\///' | xargs -n 1 -I% echo % {} \({}\)"
}
if [ -n "$1" ]; then
selected=$(options | fzf -q "$1" --nth=1,2 --with-nth=1,3)
else
selected=$(options | fzf --nth=1,2 --with-nth=1,3)
fi
if [ -z "$selected" ]; then
return
fi
ns=$(echo "$selected" | awk '{print $1}')
context=$(echo "$selected" | awk '{print $2}')
kubectx $context
kubens $ns
}
k() {
kubectl "$@"
}
# run a watch command with kubectl
kw() {
watch kubectl --context $(kubectx -c) --namespace $(kubens -c) "$@"
}
stern() {
~/.local/bin/stern/stern --context $(kubectx -c) --namespace $(kubens -c) "$@"
}
# kubectl each
ke() {
gh-k8s-clusters --profile general --status ready | xargs -t -I % kubectl --context % --namespace $(kubens -c) "$@"
}
kpe() {
stamp=$1
shift
gh-k8s-clusters --status ready --stamp $stamp | xargs -t -I % kubectl --context % --namespace $(kubens -c) "$@"
}
# kubectl github each
kge() {
gh-k8s-clusters --profile dotcom --status ready | xargs -t -I % kubectl --context % --namespace $(kubens -c) "$@"
}
# vault login
vl() {
. vault-login
VAULT_ADDR=$(vault-addr) vault token renew -increment=3h58m >/dev/null || echo "renew failed"
export AUTH_TOKEN=$(vault-secret -a hydro-tools -e production -k AUTH_TOKEN)
}
alias ht="hydro-tools"
alias at="./aqueduct-tools"
{{ end }}
# quickly get to the real path (e.g. go project symlinks)
alias cdr='cd $(pwd -P)'
{{ if .mac }}
alias scan-ssh="dns-sd -B _ssh._tcp"
alias awake="pmset -g assertions | grep -i sleep"
{{ end }}
# update homebrew
function up() {
brew update
brew outdated
brew upgrade --fetch-HEAD
brew cu --cleanup --no-brew-update --yes
brew cleanup
# mas upgrade
antigen update
gh extension upgrade --all
chezmoi status
}
# Run a command, copying the command line first and then capturing the output.
# This is in support of the alfred github details snippet
function details() {
if [ $# = 0 ]; then
echo -n "details <command> captures the command and its output "
echo "for the 'details' alfred snippet"
return
fi
echo "capturing output of ${*}..."
echo "\$ $*" | pbcopy
sleep 1 # let alfred pick it up
"$@" | pbcopy
echo "done."
}
function ss() {
local bundle="bundle"
if [ -x bin/bundle ]; then
bundle="bin/bundle"
fi
if [[ -e script/server ]]; then
if [[ -e Gemfile ]]; then
$bundle exec script/server "$@"
else
script/server "$@"
fi
elif [[ -e bin/rails ]]; then
$bundle exec rails server "$@"
else
echo "no server here"
return 1
fi
}
function sc() {
if [ -x bin/console ]; then
bin/console
return
fi
local bundle="bundle"
if [ -x bin/bundle ]; then
bundle="bin/bundle"
fi
if [[ -e script/console ]]; then
$bundle exec script/console "$@"
elif [[ -e bin/rails ]]; then
$bundle exec rails console "$@"
else
echo "no console here"
return 1
fi
}
alias sb='script/bootstrap'
# alias sv='script/vendor'
alias st='script/test'
alias cst='clear; script/test'
function ssh-setup() {
ssh $1 'mkdir .ssh; chmod 700 .ssh; touch .ssh/authorized_keys; chmod 600 .ssh/authorized_keys'
cat ~/.ssh/id_rsa.pub | ssh $1 'cat - >> ~/.ssh/authorized_keys'
}
# p() {
# if [ -n "$1" ]; then
# ps -O ppid -U $USER | grep -i "$1" | grep -v grep
# else
# ps -O ppid -U $USER
# fi
# }
alias dc="docker-compose"
function docker-cleanup() {
set -x
docker ps -aq --no-trunc -f status=exited | xargs docker rm
docker ps -aq --no-trunc -f status=created | xargs docker rm
docker images -q --filter dangling=true | xargs docker rmi
docker system prune --volumes -f
docker volume prune -f
# https://github.com/docker/for-mac/issues/371
# docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n fstrim /var/lib/docker
# set +x
}
# from https://github.com/junegunn/fzf/wiki/examples
fkill() {
local pid
if [ "$UID" != "0" ]; then
pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
fi
if [ "x$pid" != "x" ]; then
echo $pid | xargs kill -${1:-9}
fi
}
if [ -n "$(command -v code)" ]; then
export EDITOR="code"
function e() {
if [ -n "$1" ]; then
if [ -d "$1" ]; then
# local dir
# dir=${1%/} # strip trailing slash so vim stays happy
# $SHELL -l -c "cd \"$dir\" && mvim +Refresh ."
code -n $1
else
# local file dir
# file=$(basename "$1")
# dir=$(dirname "$1")
# $SHELL -l -c "cd \"$dir\" && mvim +Refresh \"$file\""
code -n $1
fi
else
# mvim "$@"
code -n . "$@"
fi
}
elif [ -n "$(command -v vim)" ]; then
export EDITOR="vim"
elif [ -n "$(command -v vi)" ]; then
export EDITOR="vi"
fi
# mysql
export MYSQL_PS1="\u@\h \d> "
# format ag output for markdown
alias mdformat='ruby -lne '\''x, y = $_.split(/\s+/,2); puts "* #{x} `#{y}`"'\'''
type nodenv &> /dev/null && eval "$(nodenv init -)"
if [ -x ~/.rbenv/bin ]; then
eval "$(~/.rbenv/bin/rbenv init - bash)"
else
type rbenv &> /dev/null && eval "$(rbenv init -)"
fi
function bo() {
local bundle="bundle" gems gem
if [ -x bin/bundle ]; then
bundle="bin/bundle"
fi
if ! gems=$($bundle list --paths); then
echo "$gems"
return 1
fi
if [ $# -gt 0 ]; then
gem=$(echo "$gems" | fzf -d/ --nth=-1 --with-nth=-1 -1 -q "$@")
else
gem=$(echo "$gems" | fzf -d/ --nth=-1 --with-nth=-1 -1)
fi
if [ -n "$gem" ]; then
e "$gem"
else
return 1
fi
}
function be() {
local bundle="bundle"
if [ -x bin/bundle ]; then
bundle="bin/bundle"
fi
$bundle exec "$@"
}
# git utils
function g() {
if [[ $# -gt 0 ]]; then
git "$@"
else
git status --short --branch -uall
fi
}
alias gam="git amend"
alias gap="git ap"
alias gbn="git branchname"
alias gc-="git co -"
alias gnb="git co -b"
alias gci="git ci"
alias gcm="git checkout \$(git main-branch)"
alias gd="git diff"
alias gi="git di"
alias gdi="git di"
function gp() {
git pull --stat --all --prune --progress --autostash &&
git clean-merged-branches
}
function gpp() {
git pull --stat --all --prune --progress --autostash &&
git push
}
function main-branch() {
git fetch origin
git remote set-head origin -a
git branch -m master main
git branch -u origin/main main
}