-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.zsh
414 lines (362 loc) · 15 KB
/
aliases.zsh
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
# Also defined in .zshrc, keep them synchronized
export GITHUB_DIR="$HOME/Code/GitHub"
export ALEDADE_DIR="$HOME/Code/Aledade"
# Put these first so they're still available even if there's a syntax error in
# one of the other aliases or functions
alias reload="exec zsh"
alias zshrc="code $GITHUB_DIR/dotfiles/.zshrc"
alias aliases="code $GITHUB_DIR/dotfiles/aliases.zsh"
alias test-outreach-f="cd $ALEDADE_DIR/outreach \
&& echo 'npm run test aledade/static/js/...'"
alias test-outreach-b="cd $ALEDADE_DIR/outreach \
&& echo 'make only=api/test_api_core.py::ApiCoreTests python-test' \
&& echo ' ^ remove \`aledade/tests/\` from the file path' \
&& echo '' \
&& echo 'Be sure to run \`make up-dev\` in another tab first!' \
"
alias start-outreach-b="cd $ALEDADE_DIR/outreach && echo 'make up-dev'"
alias start-outreach-f="cd $ALEDADE_DIR/outreach && echo 'npm run watch'"
alias start-dbt="cd $ALEDADE_DIR/dbt \
&& echo 'make ozy-dev (run \`set_dbt_vars\` if needed)'"
alias start-event-cli-step-1="cd $ALEDADE_DIR/ingestion-biz-logic \
&& echo ' git checkout develop \' \
&& echo ' && git pull \' \
&& echo ' && poetry env use 3.8 \' \
&& echo ' && poetry install \' \
&& echo ' && set_db_vars\n' \
&& git checkout develop \
&& git pull \
&& poetry env use 3.8 \
&& poetry install \
&& set_db_vars"
alias start-event-cli-step-2="cd $ALEDADE_DIR/ingestion-biz-logic \
&& echo ' Run \`start-event-cli-step-1\` first, then paste & modify the following:' \
&& echo ' (if you start the command with a space it is not saved to history)' \
&& echo ' DB_URL=postgresql://cburton:\$(echo \$DB_PASSWORD)@db-dev.aledade.com:5432/aledade poetry run event-cli -i ../toil/ARCH-X\ description/ARCH-X.jsonl -d'"
alias dcu="docker-compose up"
alias dcd="docker-compose down"
alias dcp="docker-compose pull"
alias dcb="docker-compose build"
alias dsp="docker system prune --all --force"
alias sad="say -v karen 'all done'"
alias website-go="cd $GITHUB_DIR/website-source && npm run start"
alias website-publish="cd $GITHUB_DIR/website-source && npm run publish"
# NixOS Stuff
FLAKE_LOCATION="/home/cburton/Code/GitHub/dotfiles/nix/f16"
alias nixos-update="echo 'sudo nix flake update $FLAKE_LOCATION#' \
&& sudo nix flake update $FLAKE_LOCATION#"
alias nixos-build="echo 'sudo nixos-rebuild build --flake $FLAKE_LOCATION#' \
&& sudo nixos-rebuild build --flake $FLAKE_LOCATION#"
alias nixos-switch="echo 'sudo nixos-rebuild switch --flake $FLAKE_LOCATION#' \
&& sudo nixos-rebuild switch --flake $FLAKE_LOCATION#"
alias nixos-boot="echo 'sudo nixos-rebuild boot --flake $FLAKE_LOCATION#' \
&& sudo nixos-rebuild boot --flake $FLAKE_LOCATION#"
alias nixos-rollback="echo 'sudo nixos-rebuild switch --rollback --flake $FLAKE_LOCATION#' \
&& sudo nixos-rebuild switch --rollback --flake $FLAKE_LOCATION#"
alias nixos-trim-dry="echo 'sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 14d --dry-run' \
&& sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 14d --dry-run"
alias nixos-trim="echo 'sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 14d' \
&& sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 14d"
alias nixos-history="echo 'sudo nix profile history --profile /nix/var/nix/profiles/system | sed '\''/No changes\./d; s/://; /^$/d'\''' \
&& sudo nix profile history --profile /nix/var/nix/profiles/system | sed '/No changes\./d; s/://; /^$/d'"
alias nixos-collect-garbage="echo 'You meant nix-collect-garbage. Running that now.' \
&& nix-collect-garbage"
alias get_sha="python '$GITHUB_DIR/dotfiles/nix/get_sha.py'"
alias port-forwarding-check="echo natpmpc && natpmpc"
function port-forwarding-start() {
while true; do
date;
natpmpc -a 1 0 udp 60 -g 10.2.0.1 && \
natpmpc -a 1 0 tcp 60 -g 10.2.0.1 || \
{ echo -e "ERROR with natpmpc command \a" ; break ; }
sleep 50;
done
}
# omz already includes $git_main_branch and $git_develop_branch
# https://kapeli.com/cheat_sheets/Oh-My-Zsh_Git.docset/Contents/Resources/Documents/index
alias gitdefault="git rev-parse --abbrev-ref origin/HEAD | cut -c8-"
alias gitcurrent="git rev-parse --abbrev-ref HEAD"
alias feature_branch_url='echo "https://app-$(gitcurrent | cut -c1-32).aledade.com"'
alias gpl="git pull"
alias gps="git push"
alias gpsh="git push"
alias gs="git stash"
alias gsp="git stash pop"
alias gri="git rebase -i"
alias grc="git rebase --continue"
alias gamend="git add -A && git commit -a --amend -C HEAD"
alias gamend_staged="git commit --amend -C HEAD"
alias gcom="git commit -m"
# # List gitignored files that were somehow committed
# git ls-files --ignored --exclude-standard
# Delete a file and all history of it
alias gdestroy="git rm -r"
# Single quotes are so the $(...) isn't evaluated until runtime
alias grid='git rebase -i --autosquash origin/$(git_develop_branch)'
alias grim="git rebase -i --autosquash origin/$(git_main_branch)"
# overwrite a `git restore` alias with my `git reset` function
unalias grst
function grst() {
while true; do
echo -n "Reset current branch to remote origin version? [Y/n]: "
read yn
case $yn in
[Nn]* ) break;;
* ) echo "" && git fetch && git reset --hard origin/$(gitcurrent);;
esac
yn=""
break;
done
echo ""
return 0;
}
gcomt() {
echo 'test'
PREF="$(gitcurrent | grep -oE '[aApPrRcChH]{3,4}-(\d{4,6})' | tr aprch APRCH)"
if [ -z $PREF ] && [[ "$PREF" == "" ]]; then
MSG="${1}"
else
MSG="${PREF}: ${1}"
fi
printf "
Committing locally with message \`\`$MSG\`\`\n
"
git commit -m $MSG
}
migrate-to-github() {
REPO="$(basename "$(git rev-parse --show-toplevel)")"
CURRENT_REMOTE="$(git remote get-url --push origin)"
NEW_REMOTE="[email protected]:aledade-org/$REPO.git"
while true; do
echo -n "Change remote origin?\n Old: '$CURRENT_REMOTE'\n New: '$NEW_REMOTE'\n[Y/n]: "
read -r yn
case $yn in
[Nn]* ) echo "Cancelled. Run 'git remote set-url origin NEW_GIT_URL_GOES_HERE' to update the URL manually" && break;;
* ) echo "" \
&& git remote set-url origin "$NEW_REMOTE" \
&& echo "Remote origin set. Current remote:" \
&& git remote get-url --push origin
esac
yn=""
break;
done
echo ""
return 0;
}
# Overwrite oh-my-zsh versions with better defaults
alias gst="git status -s -b"
alias glog="glol"
alias gap="git add --patch"
alias gdc="git diff --cached"
alias gp="echo 'Error: \"gp\" is ambiguous. Use \"gpl\" to pull or \"gps\" to push.' && return 1"
alias gpu="echo 'Error: \"gpu\" is ambiguous. Use \"gpl\" to pull or \"gps\" to push.' && return 1"
# alias ssh-start="eval \"$(ssh-agent -s)\" && ssh-add --apple-load-keychain"
function ssh-start() {
eval `ssh-agent -s`
find "$HOME/.ssh" -type f -iname "id_*" ! -iname "*.pub" | \
while read line; do ssh-add $line ; done
}
function ssh-stop() {
kill $(ps aux | grep 'ssh-agent' | awk '{print $2}') &> /dev/null
echo "all 'ssh-agent' processes killed"
}
# Hacking this together... zsh is annoying about shell expansion https://superuser.com/a/1731518
alias ss='ssh-start && echo "${(z)history[(r)*]}" && "${(z)history[(r)*]}"'
alias be="bundle exec"
alias update-npm-packages="npx npm-check-updates -i"
# https://www.stefanjudis.com/snippets/a-native-shell-alternative-to-the-trash-cli/
function trash() {
echo "🗑️ Moving files to trash..."
for var in "$@"
do
mv "$var" "$HOME/.trash"
echo "Deleted $var"
done
}
function eod() {
pushd "$GITHUB_DIR/worklog"
git add -p
echo $(date '+%B %_d') | awk '{print "feat(worklog): end of day -",tolower($0)}' | git commit -F -
}
function get_current_mac() {
ifconfig en0 ether | grep -oE '(.{2}:){5}.{2}'
}
function spoof_mac(){
OLD_MAC="$(get_current_mac)"
RANDOM_MAC=$(openssl rand -hex 6 | sed -E 's/(..)/\1:/g; s/.$//')
# These have to run as root in quick succession.
# The first line turns off wifi, the second line sets the new MAC.
sudo sh -c "\
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z ; \
ifconfig en0 lladdr $RANDOM_MAC"
# Confirm it worked
NEW_MAC="$(get_current_mac)"
if [[ $NEW_MAC == $RANDOM_MAC ]] ;then
echo 'Success? yes!'
echo '(The wifi should reboot automatically, but it might take a second)'
else
echo 'Success? no... Trying again sometimes works though.'
fi
echo -e " Old: $OLD_MAC\n Random: $RANDOM_MAC\n New: $NEW_MAC"
# Turn wifi back on
networksetup -detectnewhardware
}
# Might just be a macos thing
# TODO: look up how to do this in nixOS
alias flush-dns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder"
# TODO: see if this can work with kde wallet rather than apple keychain
function get_bw_status() {
echo $(bw status | jq .status | cut -d '"' -f2)
}
function unlock_bitwarden() {
if get_bw_status | grep "^locked$" ;then
echo 'Unlocking vault...'
success_msg=$(
BW_PASSWORD=$(security find-generic-password -a bitwarden_api_password -w) \
bw unlock --passwordenv BW_PASSWORD && unset BW_PASSWORD
)
export BW_SESSION=$(echo $success_msg | grep -o -m 1 '".*"' | tr -d '"')
echo 'Successfully unlocked vault'
else
echo "(vault cannot be unlocked. \`get_bw_status\` returns \"$(get_bw_status)\")"
fi
}
function login_to_bitwarden() {
if [[ $(get_bw_status) = "unlocked" ]]; then
# Already logged in, nothing to do
return 0
elif [[ $(get_bw_status) = "locked" ]]; then
unlock_bitwarden
elif [[ $(get_bw_status) = "unauthenticated" ]]; then
echo 'Logging in...'
login_msg=$(
BW_CLIENTID=$(security find-generic-password -a bitwarden_api_client_id -w) \
BW_CLIENTSECRET=$(security find-generic-password -a bitwarden_api_client_secret -w) \
bw login --apikey && \
unset BW_CLIENTID && \
unset BW_CLIENTSECRET
)
echo 'Successfully logged in'
unlock_bitwarden
else
echo "Unrecognized status. \`get_bw_status\` returns: $(get_bw_status)"
return 1
fi
}
# Restart the logi options daemon
function logi-restart() {
kill $(ps aux | grep "[L]ogiMgrDaemon" | awk '{print $2}')
echo 'Logi options daemon restarting. It may take a few seconds to take effect.'
}
# Allow using the intel version of homebrew, even on M1 hardware
if [[ $IS_M1 == 1 ]]; then
alias ibrew='arch -x86_64 /usr/local/bin/brew'
alias mbrew='arch -arm64e /opt/homebrew/bin/brew'
# alias brew=ibrew
fi
function bup() {
echo "Updating homebrew...\n"
brew update
echo ""
brew outdated --cask --greedy
while true; do
echo -n "\nUpgrade outdated casks and packages? [Y/n]: "
read yn
case $yn in
[Nn]* ) break;;
* ) brew upgrade --cask --greedy && \
brew upgrade;;
esac
yn=""
break;
done
echo ""
return 0;
}
alias bs="brew search"
alias bsc="brew search --casks"
# `bc` is already a command, don"t overwrite it
alias bic="brew install --cask"
alias buc="brew upgrade --cask --greedy && brew upgrade"
alias please="sudo"
# Add Aledade-specific aliases
if command -v scutil &> /dev/null && [ "$(scutil --get ComputerName)" = "Aledade-M3680" ]; then
alias ol='aws sso login'
function set_node_vars() {
login_to_bitwarden
echo 'Setting node vars'
export GITHUB_TOKEN=$(bw get password "[Aledade] GitHub npm token")
echo 'Successfully set node vars'
bw lock
}
function set_dbt_vars() {
login_to_bitwarden
echo 'Setting dbt vars'
export DBT_PROFILES_DIR="$ALEDADE_DIR/dbt"
export DBT_SNOWFLAKE_USER='cburton'
export DBT_SNOWFLAKE_PASSWORD=''
export DBT_SNOWFLAKE_DB='EDW_CBURTON'
# defaults to DATAVELOCITY but might need to use DEV sometimes
export DBT_ROLE='DATAVELOCITY'
export DBT_POSTGRES_USER='cburton'
export DBT_POSTGRES_PASSWORD=$(bw get password "[Aledade] db-dev password")
# Defaults to wh_datavelocity, but when using the dev role this should be wh_dev
export DBT_SNOWFLAKE_WAREHOUSE='wh_datavelocity'
# # Just use the defaults for these
# export DBT_SCHEMA='public'
# export DBT_PORT='5432'
echo 'Successfully set dbt vars'
bw lock
}
function set_db_vars() {
login_to_bitwarden
echo 'Setting db vars'
export DB_USERNAME='cburton'
export DB_PASSWORD=$(bw get password "[Aledade] db-dev password")
echo 'Successfully set db vars'
bw lock
}
function set_dag_vars() {
# echo 'Remember to run this within a poetry shell! (`poetry shell`)'
login_to_bitwarden
echo 'Setting snowflake DAG vars'
export DB_USERNAME='cburton'
export DB_PASSWORD=$(bw get password "[Aledade] db-dev password")
export SNOWFLAKE_USER=cburton
export SNOWFLAKE_PASS=
export SNOWFLAKE_ACCT=qla80296.us-east-1
export DB_URL="postgresql://$DB_USERNAME:[email protected]/aledade"
export ERGOMETER_BASE_URL="https://ergometer-dev.aledade.com"
export ERGOMETER_AUTH_CLIENT_ID=$(bw get password "[Aledade] ERGOMETER_AUTH_CLIENT_ID")
export ERGOMETER_AUTH_SECRET=$(bw get password "[Aledade] ERGOMETER_AUTH_SECRET")
# Alternative names used in some scripts
export ERGOMETER_URL=$ERGOMETER_BASE_URL
export CLIENT_ID=$ERGOMETER_AUTH_CLIENT_ID
export CLIENT_SECRET=$ERGOMETER_AUTH_SECRET
# Yet another set of names
export PATHFINDER_ERGOMETER_BASE_URL=$ERGOMETER_BASE_URL
export PATHFINDER_ERGOMETER_AUTH_CLIENT_ID=$ERGOMETER_AUTH_CLIENT_ID
export PATHFINDER_ERGOMETER_AUTH_SECRET=$ERGOMETER_AUTH_SECRET
echo 'Successfully set snowflake DAG vars'
bw lock
}
function rebuild_biz_logic() {
echo 'docker compose down biz-logic && docker compose build biz-logic && docker compose up -d biz-logic'
docker compose down biz-logic
docker compose build biz-logic
docker compose up -d biz-logic
}
function open_ibl_container() {
echo '(Remember to rebuild the biz-logic container first!)'
if [[ -z $DB_URL || -z $SNOWFLAKE_USER || -z $SNOWFLAKE_ACCT ]]; then
echo 'DAG variables not set. Running `set_dag_vars` before continuing.'
set_dag_vars
fi
CONTAINER_ID=ingestion-biz-logic-biz-logic-1
docker exec -it $CONTAINER_ID /bin/bash -c "export SNOWFLAKE_USER=$SNOWFLAKE_USER && \
export SNOWFLAKE_PASS=$DB_PASSWORD && \
export SNOWFLAKE_ACCT=$SNOWFLAKE_ACCT && \
export DB_URL=$DB_URL && \
bash"
}
fi