-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.shell_aliases.sh
618 lines (521 loc) · 18.7 KB
/
.shell_aliases.sh
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
PROVISION_CONFIG="$HOME/development/environment/project/.config"
alias ag="rg -S --hidden --colors='path:fg:0xaa,0xaa,0xff'"
alias agg='ag --hidden --ignore node_modules --ignore .git'
alias b='bash'
alias cp="cp -r"
alias scp="scp -r"
alias di='SudoNix dua interactive'
alias dp="docker ps -a"
alias f='fd --type f .'
alias h="sad"
alias htop="htop --no-color"
alias l="less"
alias ll="ls -lahv --color=always"
alias lsblk="lsblk -f"
alias m="mkdir -p"
alias rm="rm -rf"
alias rsr="rsync --remove-source-files -av --progress"
alias s='sd'
alias ta="tmux attach"
alias tree="tree -a"
alias up='up -o /tmp/up-result.sh'
alias wget="wget -c"
j() { cat $1 | jq -S "${@:2}" | less; }
alias c='cargo --color always '
alias W='watch --color -n 1 '
alias W2='watch --color -n 2 '
alias W5='watch --color -n 5 '
alias BashClean='env -i bash --norc --noprofile'
# 對於區分大小寫,使用 `-f I`
S() { fd --type f . ${3:-.} | h "$1" "$2" "${@:4}"; }
SK() { fd --type f . ${3:-.} | h "$1" "$2" -k "${@:4}"; }
dl() {
CONTAINER="$(docker ps -a | grep $1 | awk '{ print $1; }' || true)"
echo "$CONTAINER"
if [ "$(echo $CONTAINER | wc -l)" = "1" ]; then
docker logs $CONTAINER "${@:2}"
else
docker logs "$@"
fi
}
de() {
CONTAINER="$(docker ps | grep $1 | awk '{print $1}' || true)"
if [ -n "$CONTAINER" ]; then docker exec -it $CONTAINER /bin/bash; fi
}
drb() {
docker run -it --rm "${@:2}" $1 /bin/bash
}
alias ca="~/.local/bin/canto-cli"
alias Lsblk="lsblk -f | less -S"
Diff() { diff --color=always "$@" | less -r; }
DisplayFilesConcatenated() { xargs tail -n +1 | sed "s|==>|\n\n\n\n\n$1==>|; s|<==|<==\n|" | $EDITOR -; }
FileSizeCreate() { head -c "$1" /dev/urandom >"$2"; } # For example: FileSizeCreate 1GB /tmp/foo
FindLinesJustInFirstFile() { comm -23 <(sort "$1") <(sort "$2"); }
FindSortDate() { find "$@" -printf "%T@ %Tc %p\n" | sort -nr; }
GetProcessUsingPort() { fuser $1/tcp 2>&1 | grep -oE '[0-9]*$'; }
GetProcessUsingPortAndKill() { fuser $1/tcp 2>&1 | grep -oE '[0-9]*$' | xargs -I {} kill {}; }
KillPsAux() { awk '{ print $2 }' | xargs -I{} kill "$@" {}; }
LsofDir() { lsof +D $1; } # It uses `+` instead of `-`
LsofNetwork() { lsof -i; }
LsofPort() { lsof -i TCP:$1; }
LsofProcess() { lsof -p $1; } # It expects the PID
RandomFile() { find "$1" -type f | shuf -n 1; }
RandomLine() { sort -R "$1" | head -n 1; }
# will not catch `'` so can wrap generated texts with single quotes
RandomStrGenerator() {
tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom | head -c "$1"
echo
}
SedLines() { if [ "$#" -eq 1 ]; then sed -n "$1,$1p"; else sed -n "$1,$2p"; fi; }
SortJSON() { cat "$1" | jq -S | sponge "$1"; }
TopCPU() { ps aux | sort -nr -k 3 | head "$@" | sed -e 'G;G;'; } # e.g. TopCPU -n 5 | less -S
TopMemory() { ps aux | sort -nr -k 4 | head "$@" | sed -e 'G;G;'; } # e.g. TopMemory -n 5 | less -S
USBClone() {
if [ -z "$I" ] || [ -z "$O" ]; then
echo "Missing params"
return
fi
dd if=$I of=$O bs=1G count=10 status=progress
} # Example: I=/dev/sdb O=/dev/sdc USBClone
Vidir() { vidir -v -; } # To remove files, remove the lines
VidirFind() { find $@ | sort -V | vidir -v -; }
VisudoUser() { sudo env EDITOR=vim visudo -f /etc/sudoers.d/$1; }
alias ClipboardSSHSend="clipboard_ssh send"
alias HostClipboardSSH="clipboard_ssh host"
alias Vpn='(cd ~/development/environment && bash src/scripts/misc/vpn.sh)'
if type ffmpeg >/dev/null 2>&1; then
FfmpegSubs() {
if [ -z "$1" ]; then
echo "缺少影片路徑" && exit 1
fi
# 第一個參數是影片的檔案路徑
ffmpeg -i $1 -map 0:s:${2:-0} ${3:-$1}.srt
}
FfmpegAudioMp3() {
if [ -z "$1" ]; then
echo "缺少視訊路徑" && exit 1
fi
ffmpeg -i $1 -q:a 0 -map a $1.mp3
}
fi
# Cross-platform (including remote VM) function which accepts text from a pipe
ClipboardCopyPipe() {
read INPUT
if [ -z "$INPUT" ]; then return; fi
echo "$INPUT" | clipboard_ssh send
}
# Sample of alias using ClipboardCopyPipe and bitwarden
alias BWAliasExample=$'bw list items --search myapp | jq \'.[0].login.password\' -r | ClipboardCopyPipe'
alias SSHAgent='eval `ssh-agent`'
SSHGeneratePemPublicKey() {
FILE=$1
ssh-keygen -f "$FILE" -e -m pem
}
SSHGenerateStrongKey() {
FILE="$1"
ssh-keygen -t ed25519 -f "$FILE"
}
alias SSHListLocalForwardedPorts='ps x -ww -o pid,command | ag ssh | grep --color=never localhost'
SSHForwardPortLocal() { ssh -fN -L "$1":localhost:"$1" ${@:2}; } # SSHForwardPort 1234 192.168.1.40
alias SSHDConfig='sudo sshd -T'
SSHListConnections() { sudo netstat -tnpa | grep 'ESTABLISHED.*sshd'; }
# Example to how to manually add a key with a timeout
SSHExampleConfigure() {
if [ -n "$(ssh-add -L | grep some_key || true)" ]; then return; fi
eval $(ssh-agent)
ssh-add -t 1h ~/.ssh/some_key
}
alias lang='b ~/development/environment/src/scripts/misc/lang.sh'
alias AliasesReload='source ~/.shell_aliases'
alias EditProvision="(cd ~/development/environment && $EDITOR src/main.sh && cargo run --release)"
alias FDisk='sudo fdisk /dev/sda'
alias FilterLeaf=$'sort -r | awk \'a!~"^"$0{a=$0;print}\' | sort'
alias FlatpackInit='flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo'
alias FormatProto='clang-format -i'
alias GeoInfo='curl -s ipinfo.io | jq .'
alias HierarchyManual='man hier'
alias IPPublic='curl ifconfig.co'
alias IPLocal=$'ifconfig -a | ag "inet\\b" | ag -v " 127" | awk \'{ print $2; }\' | sort'
alias LastColumn="awk '{print "'$NF'"}'"
alias PathShow='echo $PATH | tr ":" "\n" | sort | uniq | less'
alias Provision="(cd ~/development/environment && bash src/main.sh)"
alias ProvisionUpdate="(cd ~/development/environment && IS_PROVISION_UPDATE=1 bash src/main.sh)"
alias PsTree='pstree -s'
alias RsyncDelete='rsync -rhv --delete' # remember to add a slash at the end of source (dest doesn't matter)
alias ShellChangeToBash='chsh -s /bin/bash; exit'
alias SocketSearch='sudo ss -lntup'
alias SyncProvisionCustom='(cd ~/development/environment && bash src/scripts/copy_custom.sh)'
alias TreeDir='tree -d'
alias Visudo='sudo env EDITOR=vim visudo'
alias Xargs='xargs -I{} '
alias YoutubeChooseResolution='yt-dlp -f ' # e.g. YoutubeChooseResolution 12 https://...
alias YoutubeResolutions='yt-dlp -F '
alias YoutubeSubtitles='yt-dlp --all-subs --skip-download'
alias CrontabUser='crontab -e'
alias CrontabRoot='sudo EDITOR=vim crontab -e'
alias Headers='curl -I' # e.g. Headers google.com
alias Ports='SudoNix netstat -tulanp'
alias NetstatConnections='netstat -nputw'
alias AnsiColorsRemove="sed 's/\x1b\[[0-9;]*m//g'"
KillAllTmux() {
(
killall /usr/bin/tmux || true
killall tmux || true
killall $(which tmux) || true
) >/dev/null 2>&1
ps aux | grep tmux | grep -v grep | awk '{print $2}' | xargs -I{} kill {}
}
# 這些別名保留 nix shell 環境
TW() { tmux new-window -e "IN_NIX_SHELL=$IN_NIX_SHELL"; }
TP() { tmux split-window -e "IN_NIX_SHELL=$IN_NIX_SHELL"; }
NmapLocal() {
# https://github.com/nmap/nmap
THIRD_NUM=${1:-1}
nix-shell -p nmap --run "sudo --preserve-env=PATH env nmap -sn '192.168.$THIRD_NUM.0/24'" >/tmp/nmap-result
sudo chown $USER /tmp/nmap-result
sed -i "s|Nmap|\nNmap|" /tmp/nmap-result
less /tmp/nmap-result
}
WorktreeClone() {
git clone --bare "$1" .bare
echo "gitdir: ./.bare" >.git
}
n() {
NOW=$SECONDS
bash ~/development/environment/src/scripts/misc/n.sh $@
AFTER=$SECONDS
PASSED=$(($AFTER - $NOW))
if [ $PASSED -lt 2 ]; then
builtin fg
else
echo "nvim session: $PASSED"
fi
}
ConfigProvisionList() {
INITIAL_SHA=$(find ~/development/environment/project/.config -type f | sort -V | sha256sum | awk '{print $1}')
"$HOME"/.local/bin/provision_choose_config $@ || return
AFTER_SHA=$(find ~/development/environment/project/.config -type f | sort -V | sha256sum | awk '{print $1}')
# Stop if no changes
if [ "$INITIAL_SHA" = "$AFTER_SHA" ]; then return; fi
RebuildNix && Provision
}
alias ConfigProvisionListFzf='ConfigProvisionList fzf'
CargoGenerateClean() {
BIN_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[].targets[] | select( .kind | map(. == "bin") | any ) | .name')
CARGO_TARGET_DIR=target cargo build --release && mv target/release/"$BIN_NAME" . && rm -rf target
echo "Binary '$BIN_NAME' built and moved to current directory"
}
CargoRunClean() {
DIR=$1
COMMAND=$(basename $DIR)
(cd $DIR && CargoGenerateClean)
$DIR/$COMMAND
}
CargoDevGenerate() {
BIN_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[].targets[] | select( .kind | map(. == "bin") | any ) | .name')
CARGO_TARGET_DIR=target cargo build && mv target/debug/"$BIN_NAME" .
echo "Binary '$BIN_NAME' built and moved to current directory"
}
alias NixFlakeUpdateInput='nix flake lock --update-input'
alias NixListChannels='nix-channel —-list'
alias NixListGenerations="nix-env --list-generations"
alias NixListPackages='nix-env --query "*"'
alias NixRemovePackage='nix-env -e'
alias NixReplPkgs="nix repl --expr 'import <nixpkgs>{}'"
alias NixReplFlake='nix repl --expr "builtins.getFlake \"$PWD\""'
alias NixDevelopPath='nix develop path:$(pwd)' # 也可以只運行指令: `NixDevelopPath -c cargo build`
NixFindPointersToFile() {
ITEM="$1"
if [ -z "$(echo $ITEM | grep -F /nix/store || true)" ]; then
ITEM="/nix/store$ITEM"
fi
sudo find -L /home -samefile $ITEM 2>/dev/null
}
alias Nix_FileEval='nix-instantiate --eval'
alias Nix_EnvInstallPackage='nix-env -iA'
NixUpdateChannel() {
if type jq >/dev/null 2>&1; then
UNSTABLE_REV="$(cat ~/development/environment/flake.lock | jq -r '.nodes.unstable.locked.rev')"
if [ ! -f ~/.check-files/nix-channel ] || [ -z "$(cat ~/.check-files/nix-channel | grep $UNSTABLE_REV || true)" ]; then
echo "UNSTABLE_REV: $UNSTABLE_REV"
nix-channel --remove nixpkgs || true
nix-channel --add "https://github.com/NixOS/nixpkgs/archive/$UNSTABLE_REV.tar.gz" nixpkgs
nix-channel --update
nix-channel --list
echo "$UNSTABLE_REV" >~/.check-files/nix-channel
fi
fi
}
NixShell() {
nix-shell -p $@ --command zsh
}
NixClearSpaceOnly() {
nix-collect-garbage -d
}
ClearSpace() {
if [ -n "$(ps aux | ag 'tmux[ ]new-session')" ]; then
echo "您應該先停止 tmux,這樣就不會開啟 nix shell"
return
fi
if [ -n "$(ps aux | grep 'nvim' | grep -v 'grep')" ]; then
echo "你應該先停止nvim"
return
fi
echo "你應該先停止docker容器"
read "?你呼叫這個函數了嗎 'NixGCRootsDelete'?。 按 ctrl-c 停止。 "
sudo echo ''
RebuildNix
sudo rm -rf ~/.cache/composer
sudo rm -rf ~/.cache/go-build
sudo rm -rf ~/.cache/yarn
sudo rm -rf ~/.cargo
sudo rm -rf ~/.completions
sudo rm -rf ~/.config/coc
sudo rm -rf ~/.go-workspace
sudo rm -rf ~/.gradle
sudo rm -rf ~/.local/share/nvim
sudo rm -rf ~/.local/state/nvim
sudo rm -rf ~/.npm
sudo rm -rf ~/.rustup
sudo rm -rf ~/go
sudo rm -rf ~/nix-dirs
NixClearSpaceOnly
if [ -z "$(cd ~/development/environment && git --no-pager diff HEAD -- src/project_templates/web_apps)" ]; then
(cd ~/development/environment &&
sudo rm -rf src/project_templates/web_apps &&
git checkout -- src/project_templates/web_apps)
fi
if type docker >/dev/null 2>&1; then
docker network prune -f || true
docker system prune -af || true
docker volume prune -f || true
fi
if type podman >/dev/null 2>&1; then
podman kill $(podman ps -q)
podman system prune -af || true
fi
nvim --headless "+Lazy! sync" +qa
RebuildNix && Provision
echo '開發環境清理'
}
NixGCRoots() {
if [ -n "$1" ] && [ -n "$(echo $1 | grep .)" ]; then
nix-store --gc --print-roots 2>&1 | ag -v removing | ag -v censored | awk '{ print $1; }' |
ag $1 | xargs -I{} rm -rf {}
fi
nix-store --gc --print-roots 2>&1 | ag -v removing | ag -v censored | awk '{ print $1; }'
}
alias NixGCRootsDelete="bash ~/development/environment/src/scripts/toolbox/nix_garbage_collector_roots.sh -"
NixListShellPkgs() {
echo $PATH | tr ':' '\n' | ag '/nix/store' | sed 's|^[^-]*-||' | sort | sed 's|-[.0-9]*/bin||' | uniq | l
}
NixListReferrers() {
# This is useful when copying from dua result
ITEM="$1"
if [ -z "$(echo $ITEM | grep -F /nix/store || true)" ]; then
ITEM="/nix/store$ITEM"
fi
nix-store --query --referrers $ITEM
}
NixListClosure() {
# This is useful when copying from dua result
ITEM="$1"
if [ -z "$(echo $ITEM | grep -F /nix/store || true)" ]; then
ITEM="/nix/store$ITEM"
fi
nix-store --query --referrers-closure $ITEM
}
NixPathInfo() {
# This is useful when copying from dua result
ITEM="$1"
if [ -z "$(echo $ITEM | grep -F /nix/store || true)" ]; then
ITEM="/nix/store$ITEM"
fi
nix path-info -Sh $ITEM
}
NixEnvironmentUpgrade() {
if [ -n "$(ps aux | ag 'tmux[ ]new-session')" ]; then
echo "您應該先停止 tmux,這樣就不會開啟 nix shell"
return
fi
cd ~/development/environment/src/project_templates/web_apps/tooling
npm upgrade --save --force
cd ~/development/environment
RustUpdateProvisionPackages
nix flake lock --update-input nixpkgs
nix flake lock --update-input unstable
nix flake lock --update-input home-manager
nix flake lock --update-input flake-utils
nix flake lock --update-input ghostty
rm -rf ~/.check-files/nix-channel
NixUpdateChannel
bash ~/development/environment/src/scripts/toolbox/nix_sync_input.sh ALL
echo "其他手動更新:"
grep -r '@upgrade' nix # @TODO: 透過取得最後的 git sha 自動升級它們
echo "環境升級了,現在可以清理GC、清理空間了"
}
alias HomeManagerInitFlake='nix run home-manager/release-24.05 -- init'
alias HomeManagerDeleteGenerations='home-manager expire-generations "-1 second"'
# The space is important to be able to also run other aliases (not only Nix
# binaries):
# https://linuxhandbook.com/run-alias-as-sudo/
alias SudoNix='sudo --preserve-env=PATH env '
alias ProvisionNix="(RebuildNix && Provision)"
# 由於是通用命令而有不同的前綴
RebuildNix() {
if [ -f /etc/os-release ] && [ -n "$(cat /etc/os-release | grep nixos || true)" ]; then
# 它需要 --impure 標誌,因為它導入/etc/nixos/configuration.nix配置
(cd ~/development/environment &&
sudo nixos-rebuild switch --show-trace --flake path:$PWD --impure)
fi
if type home-manager >/dev/null 2>&1; then
NixUpdateChannel
# 現在需要 --impure 來讀取配置
home-manager switch --impure --flake ~/development/environment/
fi
}
# # To patch a binary interpreter path, for example for 'foo:
# patchelf --set-interpreter /usr/lib64/ld-linux-aarch64.so.1 ./foo
# # To read the current interpreter:
# readelf -a ./foo | grep interpreter
# # To print the dynamic libraries:
# ldd -v ./foo
# # To find libraries that need patching
# ldd ./foo | grep 'not found'
# # To find the interpreter in NixOS
# cat $NIX_CC/nix-support/dynamic-linker
# # To list the required dynamic libraries
# patchelf --print-needed ./foo
NixFormat() {
if [ -n "$1" ]; then
alejandra $@
return
fi
alejandra ./**/*.nix
}
alias Nu='nix-shell -p nushell --command nu'
if type vegeta >/dev/null 2>&1; then
VegetaAttack() {
# Example usage: VegetaAttack -rate=100 -duration=10s -targets=targets.txt
vegeta attack $@ | tee /tmp/vegeta-results.bin | vegeta report
}
alias VegetaDocs='echo https://www.scaleway.com/en/docs/tutorials/load-testing-vegeta/'
fi
DockerEnvironment() {
bash ~/development/environment/src/docker_environment/run.sh
}
DockerEnvironmentAlacritty() {
START_SCRIPT='alacritty -v --config-file '$HOME'/.config/alacritty/alacritty.yml -e bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && zsh"' \
bash src/docker_environment/run.sh
}
if type ruby >/dev/null 2>&1; then
if [ ! -f ~/development/environment/project/.config/ruby_system ]; then
mkdir -p "$HOME/.local/gems"
export GEM_HOME="$HOME/.local/gems"
export GEM_PATH="$GEM_HOME"
export PATH="$GEM_HOME/bin:$PATH"
fi
fi
if type kubectl >/dev/null 2>&1; then
source <(kubectl completion zsh)
alias k=kubectl
if [ -f ~/.kube-remote/config ]; then
kr() { KUBECONFIG=$HOME/.kube-remote/config kubectl $@; }
# 用 kubectl 完成
source <(kubectl completion zsh | sed 's/\bkubectl\b/kr/g')
fi
fi
if type helm >/dev/null 2>&1; then
source <(helm completion zsh)
fi
if type magick >/dev/null 2>&1; then
ImageConvertWebpPng() { magick "$1" "${1%.*}.png"; }
fi
if type pkg-config >/dev/null 2>&1; then
PkgConfigPath() {
PKG_CONFIG_PATH=$(pkg-config --variable pc_path pkg-config)
echo $PKG_CONFIG_PATH | tr ':' '\n'
}
alias PkgConfigList='pkg-config --list-all'
alias PkgConfigGTKVersion='pkg-config --modversion gtk4'
fi
if type ps_mem >/dev/null 2>&1; then
alias MemoryPS='sudo --preserve-env=PATH ps_mem | less'
fi
alias UpdateBootstrap='n $BOOTSTRAP_FILE'
alias TmuxNotesPane="tmux split-window -h zsh -c 'cd ~/development/notes && zsh'"
if type pyenv >/dev/null 2>&1; then
alias PyEnvList='pyenv install --list'
alias PyEnvVersions='pyenv versions'
fi
NoteBranch() {
NOTES_TICKET_PREFIX=""
if [ ! -f $PROVISION_CONFIG/notes_ticket_prefix ]; then
echo "缺少 $PROVISION_CONFIG/notes_ticket_prefix"
return
fi
NOTES_TICKET_PREFIX=$(cat $PROVISION_CONFIG/notes_ticket_prefix)
if [ -z "$NOTES_TICKET_PREFIX" ]; then
echo "缺少 $PROVISION_CONFIG/notes_ticket_prefix"
return
fi
BRANCH_NAME=$(git branch --show-current)
if [ -z "$(echo $BRANCH_NAME | grep "^$NOTES_TICKET_PREFIX""-[0-9]*" || true)" ]; then
if [ -z "$(echo $BRANCH_NAME | grep "NO-TICKET_" || true)" ]; then
echo "分支名稱不符合格式"
return
fi
fi
FILE_NAME="~/development/notes/tasks/$BRANCH_NAME.md"
if [ ! -f $FILE_NAME ]; then
echo '- `$BRANCH_NAME`' >$FILE_NAME
fi
tmux split-window -h zsh -c "cd ~/development/notes && zsh -c 'nvim $FILE_NAME'"
}
P12Info() {
FILE_PATH="$1"
FILE_PASS="$2"
if [ -z "$FILE_PATH" ] || [ -z "$FILE_PASS" ]; then
echo "缺少參數"
echo "用法: P12Info <file_path> <file_pass>"
return
fi
openssl pkcs12 -legacy -in "$FILE_PATH" -nodes -passin pass:"$FILE_PASS" |
openssl x509 -noout -subject
}
2FAEncrypt() {
if [ ! -f ~/.2fa ]; then
echo "缺少 ~/.2fa"
return
fi
cp ~/.2fa ~/.2fa.bkp
cat ~/.2fa | age -e -a -p >~/.2fa.age && rm ~/.2fa && rm ~/.2fa.bkp || mv ~/.2fa.bkp ~/.2fa
}
2FADecrypt() {
if [ ! -f ~/.2fa.age ]; then
echo "缺少 ~/.2fa.age"
return
fi
cp ~/.2fa.age ~/.2fa.bkp
cat ~/.2fa.age | age -d >~/.2fa && rm ~/.2fa.age && rm ~/.2fa.bkp || mv ~/.2fa.bkp ~/.2fa
}
2FAAdd() {
if [ -z "$1" ]; then
echo "缺少參數"
echo "用法: 2FAAdd <name>"
return
fi
2fa -add $1
}
ShellSource() {
IS_MAC=$(uname -a | ag Darwin || true)
if [ -n "$IS_MAC" ]; then
. ~/.zshrc
return
fi
if [ -n "$(cat /etc/passwd | grep $USER | awk -F: '{print $7}' | grep zsh || true)" ]; then
. ~/.zshrc
elif [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
}