Repository navigation
Expand file tree
/
Copy pathdot_functions
More file actions
1684 lines (1451 loc) · 60 KB
/
Copy pathdot_functions
File metadata and controls
1684 lines (1451 loc) · 60 KB
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---------------------------- Shell & PATH Utils ---------------------------- #
# Messages start with a verb: past tense for finished work, "Failed to <verb>"
# for failures, a participle plus … for work in progress, or an imperative hint.
# The prefix holds the only formatting colon, so detail goes in parentheses.
# Colons inside data values are fine.
log() {
local level="$1"; shift
local red green yellow blue gray reset
if [[ -t 2 ]]; then
red='\033[0;31m' green='\033[0;32m' yellow='\033[0;33m'
blue='\033[0;34m' gray='\033[0;90m' reset='\033[0m'
fi
case "$level" in
error|red) printf "${red}Error:${reset} %s\n" "$*" >&2 ;;
success|green) printf "${green}Success:${reset} %s\n" "$*" ;;
warning|yellow) printf "${yellow}Warning:${reset} %s\n" "$*" >&2 ;;
info|blue) printf "${blue}Info:${reset} %s\n" "$*" ;;
debug) [[ -n "${DEBUG:-}" ]] && printf "${gray}Debug:${reset} %s\n" "$*" >&2 ;;
*) echo "$level" "$@" ;;
esac
}
# This draws only on a terminal, because run:daily points stderr at a report
# file. It cuts the line to the terminal width, since a wrapped line breaks the
# redraw. Call progress:clear before any other output.
progress:bar() {
[[ -t 2 ]] || return 0
local done="$1" total="$2" message="$3" detail="${4:-}"
local width=24 filled body cols="${COLUMNS:-0}"
(( total > 0 )) || return 0
(( cols > 0 )) || cols=80
filled=$(( done * width / total ))
body="${message} ${(l:filled::█:)}${(l:width-filled::░:)} ${done}/${total}${detail:+ (${detail})}"
(( ${#body} > cols - 10 )) && body="${body[1,cols-11]}…"
printf '\r\033[K\033[0;34mInfo:\033[0m %s' "$body" >&2
}
progress:clear() {
[[ -t 2 ]] && printf '\r\033[K' >&2
return 0
}
retry() {
local attempts="$1" delay="$2"; shift 2
local i
for (( i = 1; i <= attempts; i++ )); do
if "$@"; then
return 0
fi
(( i < attempts )) && sleep "$delay"
done
return 1
}
cmd:exists() {
[[ $# -eq 1 ]] || {
echo "Usage: cmd:exists <command>" >&2
return 1
}
command -v "$1" &>/dev/null
}
# This is narrower than cmd:exists, which also resolves aliases. The plugin
# bundle loads before .aliases, so `command -v` would report every plugin alias
# as taken, while .aliases owns the names it sets.
alias:_taken() {
(( ${+commands[$1]} + ${+functions[$1]} + ${+builtins[$1]} ))
}
alias:safe() {
local name="${1%%=*}"
if alias:_taken "$name"; then
log warning "Skipped alias '${name}' (would shadow existing command)"
else
alias:_set "$1"
fi
}
# A silent alias:safe for polyfills, where a missing command is expected
alias:fallback() {
alias:_taken "${1%%=*}" || alias:_set "$1"
}
# Resolves the target the way zsh would at expansion time
alias:_set() {
local -a parts=( ${(z)${1#*=}} )
local target="${(eQ)parts[1]}"
[[ -n "$target" ]] && ! cmd:exists "$target" \
&& log warning "Defined alias '${1%%=*}' (target '${target}' is not available)"
alias "$1"
}
# The common-aliases plugin ships suffix aliases for Linux desktop tools, and
# the ones whose tool is missing would fail on first use
alias:prune-suffix() {
local suffix target
local -a parts
for suffix in ${(k)saliases}; do
parts=( ${(z)saliases[$suffix]} )
target="${(eQ)parts[1]}"
if [[ -n "$target" ]] && ! cmd:exists "$target"; then
unalias -s -- "$suffix"
fi
done
}
# The plugin bundle has no alias:safe, so this is how a plugin update that
# claims a command name stays visible. Deliberate shadows such as log() appear
# too.
#
# This reads $commands. whence -p forks once per name, which takes seconds
# across every alias, and it answers from the filesystem, so the
# case-insensitive volume reports /bin/lS for the lS alias.
alias:shadows() {
local name
rehash
for name in ${(ko)aliases}; do
[[ -n "${commands[$name]}" ]] \
&& printf '%-14s %-44s %s\n' "$name" "${aliases[$name]}" "${commands[$name]}"
done
# Nobody types _ names, which are completion functions and plugin internals
for name in ${(ko)functions}; do
[[ "$name" == _* || -z "${commands[$name]}" ]] && continue
printf '%-14s %-44s %s\n' "$name" '(function)' "${commands[$name]}"
done
}
# The job runs detached because every caller reaches the network. In front of
# the prompt that makes the day's first shell far slower, and slower still
# offline. The next shell prints the output.
run:daily() {
local name="$1"; shift
local stamp="${HOME}/.cache/daily/${name}"
local report="${stamp}.report"
# Shells with no terminal, such as an editor capturing the login environment
# or Claude Code's shell snapshot, would lose the only record of a failed
# update
[[ -t 1 && -s "$report" ]] && { cat "$report"; command rm -f "$report" }
# A run alive past its timeout means its watchdog failed too, so this warns in
# every terminal. It removes a stale pid file (reboot, SIGKILL, reused pid).
# The glob qualifier avoids a fork unless a pid file is overdue.
local pidfile="${stamp}.pid"
local -i limit=$(( ${RUN_DAILY_TIMEOUT:-300} / 60 + 2 ))
local -a overdue=( ${pidfile}(N.mm+${limit}) )
if (( ${#overdue} )); then
local pid=$(<"$pidfile")
if [[ "$pid" == <-> ]] && kill -0 "$pid" 2>/dev/null \
&& [[ "$(ps -o command= -p "$pid" 2>/dev/null)" == *_run:daily* ]]; then
[[ -t 1 ]] && log warning "Detected ${name} still running after ${limit} minutes (stop it with 'kill -- -${pid}')" 2>&1
else
command rm -f "$pidfile"
fi
fi
# `^ms-0` excludes a stamp dated in the future, which `mh-24` alone would
# match forever. `ms+0` would also reject a stamp written this second.
local -a fresh=( ${stamp}(N.mh-24^ms-0) )
(( ${#fresh} )) && return 0
mkdir -p "${stamp:h}"
# Stamp before launching, so a burst of new tabs does not start one copy each.
# The lock in _run:daily stops tabs that still pass the check together.
touch "$stamp"
# This starts a new session, not a background job. A `&!` job stays a process
# group on this terminal, and the terminal stops the group (SIGTTOU, SIGTTIN)
# when anything in it touches the tty, as antidote 2.3.0 does on every run.
# The `$(...)` subshell then busy-loops on its stopped child at 100% CPU until
# killed, a macOS /bin/zsh 5.9 bug fixed in 5.9.2. macOS has no setsid(1), so
# perl calls setsid(2).
#
# The warning goes in the report because the GitKraken ZDOTDIR shim in Zed
# terminals discards startup stderr.
[[ -x /usr/bin/perl ]] || {
log warning "Skipped ${name} (/usr/bin/perl is missing, so it cannot run detached)" 2>>"$report"
return 0
}
/usr/bin/perl -MPOSIX -e 'defined(my $pid = fork) or exit 1; exit 0 if $pid;
POSIX::setsid() > 0 or exit 1; exec @ARGV or exit 1' \
/usr/bin/nice -n 5 /bin/zsh -fc 'source "${HOME}/.functions" && _run:daily "$@"' \
run:daily "$name" "$report" "${RUN_DAILY_TIMEOUT:-300}" "$@" </dev/null >/dev/null 2>&1
}
# timeout(1) cannot run the callers, which are functions, so a watchdog limits
# the job. The process that hangs sits one to three levels below the job. The
# watchdog collects the process tree first, because killing a parent puts its
# children beyond pgrep. It signals the job last, because the parent kills the
# watchdog when the job dies. TERM comes first because antidote traps it to
# remove its tmpdir.
_run:daily() {
local name="$1" report="$2" timeout="$3"; shift 3
local job watchdog lockfd
# A second copy would race this one on the same clones and report.new. The
# lock ends with this shell, even on SIGKILL.
if zmodload -F zsh/system b:zsystem 2>/dev/null; then
: >>"${report}.lock"
zsystem flock -t 0 -f lockfd "${report}.lock" 2>/dev/null || return 0
fi
# $$ leads this session's process group, so `kill -- -$$` stops the whole run
local pidfile="${report%.report}.pid"
print -r -- $$ >| "$pidfile"
"$@" >| "${report}.new" 2>&1 </dev/null &
job=$!
( sleep "$timeout"
local -a tree=( $job ) level=( $job )
while level=( ${(f)"$(pgrep -P ${(j:,:)level} 2>/dev/null)"} ); do tree=( $level $tree ); done
# Without this line a daily timeout looks like a run with no output
log warning "Timed out ${name} after ${timeout}s (killed it, run it by hand to see why)" 2>>"${report}.new"
# This runs detached because the parent kills the watchdog as soon as TERM
# arrives, and a descendant that ignores TERM would keep running
( sleep 5; kill -KILL $tree 2>/dev/null ) &!
kill -TERM $tree 2>/dev/null ) & watchdog=$!
wait $job
kill $watchdog 2>/dev/null
command mv -f "${report}.new" "$report"
command rm -f "$pidfile"
}
# This prepends a directory already on PATH again, or an earlier append would
# leave it behind /usr/bin. `typeset -gU PATH path` in .zshenv drops the older
# copy.
path:add() {
local dir
for dir in "$@"; do
[[ -d "$dir" ]] && export PATH="$dir:$PATH"
done
}
path:append() {
local dir
for dir in "$@"; do
[[ -d "$dir" ]] && case ":$PATH:" in
*":$dir:"*) ;;
*) export PATH="$PATH:$dir" ;;
esac
done
}
mkd() {
mkdir -p "$@" && cd "${@[-1]}" || return
}
cdf() {
local dir
# `cd ""` succeeds, so this has to catch a failed osascript
dir="$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')" || return
cd "$dir"
}
# -------------------------------- Zsh Plugins ------------------------------- #
# antidote calls a `conditional:` function with no arguments, so the tool a
# plugin needs has to be in the guard's name
local _tool
for _tool in mongocli multipass pre-commit sbt terraform; do
functions[have:${_tool}]="cmd:exists ${_tool}"
done
unset _tool
# macOS ships a /usr/bin/rails stub, so cmd:exists always succeeds
have:rails() {
local bin
bin="$(whence -p rails)" || return 1
[[ "$bin" != "/usr/bin/rails" ]]
}
# antidote never revisits a clone, so nothing else moves the plugins off their
# first commit
plugins:update() {
local antidote="${HOMEBREW_PREFIX}/opt/antidote/share/antidote/antidote.zsh"
local bundle="${HOME}/.cache/zsh/plugins.zsh"
local output
[[ -f "$antidote" && -f "${HOME}/.plugins" ]] || return 0
source "$antidote"
if ! output=$(antidote update 2>&1 </dev/null); then
log warning "Failed to update zsh plugins"
print -r -- "$output" >&2
return 0
fi
# Write beside the old bundle, because a failed rebuild must leave a working
# one
if ! output=$(antidote bundle < "${HOME}/.plugins" 2>&1 >| "${bundle}.new"); then
log warning "Failed to rebuild the plugin bundle (keeping the current one)"
print -r -- "$output" >&2
command rm -f "${bundle}.new"
return 0
fi
command mv -f "${bundle}.new" "$bundle"
log success "Updated zsh plugins"
return 0
}
# Sourcing the bundle inside a function is safe. Only the git plugin's
# `git_version` scratch variable stops reaching the shell.
plugins:load() {
local list="${HOME}/.plugins"
local bundle="${HOME}/.cache/zsh/plugins.zsh"
local antidote="${HOMEBREW_PREFIX}/opt/antidote/share/antidote/antidote.zsh"
[[ -f "$list" ]] || return 0
# Plugins write generated completions here on first use, so this directory has
# to be on fpath before they load
export ZSH_CACHE_DIR="${HOME}/.cache/oh-my-zsh"
[[ -d "${ZSH_CACHE_DIR}/completions" ]] || mkdir -p "${ZSH_CACHE_DIR}/completions"
fpath=("${ZSH_CACHE_DIR}/completions" $fpath)
# Plugins read the cache as they load, so this cannot wait for .completions.
# Without the cache the composer plugin spends about 230 ms per shell finding
# its vendor path.
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path "${ZSH_CACHE_DIR}/zcompcache"
# Plugins call compdef as they load, but compinit waits for .completions,
# which finishes fpath. A second compinit would discard the first, so the calls
# queue for .completions to replay.
typeset -ga COMPDEF_DEFERRED=()
(( ${+functions[compdef]} )) || \
compdef() { COMPDEF_DEFERRED+=( "${(j: :)${(q)@}}" ) }
if [[ ! "$bundle" -nt "$list" && -f "$antidote" ]]; then
[[ -d "${bundle:h}" ]] || mkdir -p "${bundle:h}"
source "$antidote"
# `>|` truncates first, so a failed rebuild would leave an empty bundle that
# is newer than the list. Shells started together after a list change all
# rebuild, so each writes its own file for mv to swap in.
if antidote bundle < "$list" >| "${bundle}.new.$$"; then
command mv -f "${bundle}.new.$$" "$bundle"
else
command rm -f "${bundle}.new.$$"
fi
fi
# Plugins regenerate completions in `&|` jobs on every shell. With job control
# on, each is a background group the terminal can stop, leaving its `=(...)`
# waiter spinning at 100% CPU (see run:daily). Only MONITOR is restored,
# because local_options would also undo the options the plugins set.
local monitor=$options[monitor]
unsetopt monitor
[[ -f "$bundle" ]] && source "$bundle"
[[ "$monitor" == on ]] && setopt monitor
return 0
}
# ----------------------------- Files & Archives ----------------------------- #
targz() {
local tmpFile="${1%/}.tar"
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1
local size
size=$(stat -f"%z" "${tmpFile}" 2>/dev/null)
local cmd=""
if (( size < 52428800 )) && cmd:exists zopfli; then
cmd="zopfli"
elif cmd:exists pigz; then
cmd="pigz"
else
cmd="gzip"
fi
log info "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…"
"${cmd}" -v "${tmpFile}" || { command rm -f "${tmpFile}"; return 1; }
[[ -f "${tmpFile}" ]] && command rm -f "${tmpFile}"
local zippedSize
zippedSize=$(stat -f"%z" "${tmpFile}.gz" 2>/dev/null)
log success "Created ${tmpFile}.gz ($((zippedSize / 1000)) kB)"
}
fs() {
if (( $# )); then
du -sh -- "$@"
else
du -sh .[^.]*(N) ./*(N)
fi
}
dataurl() {
[[ -f "${1:-}" ]] || { log error "Failed to encode (needs an existing file, run 'dataurl <file>')"; return 1; }
local mimeType
mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
server() {
local port="${1:-8000}"
{ retry 5 1 curl -fs "http://localhost:${port}/" -o /dev/null && open "http://localhost:${port}/"; } &
python3 -m http.server "${port}"
}
gz() {
[[ -f "${1:-}" ]] || { log error "Failed to compare (needs an existing file, run 'gz <file>')"; return 1; }
local origsize
origsize=$(wc -c < "$1")
(( origsize )) || { log error "Failed to compare (${1} is empty)"; return 1; }
local gzipsize
gzipsize=$(gzip -c "$1" | wc -c)
local ratio
ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l)
printf "orig: %d bytes\n" "$origsize"
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"
}
trec() {
tree -aC -I "${TREE_IGNORE}" --dirsfirst "$@" | less -FRNX
}
tre() {
tree -a -I "${TREE_IGNORE}" --dirsfirst "$@"
}
# ------------------------------ Open & Launch ------------------------------- #
o() {
open "${@:-.}"
}
# -------------------------- Networking, TLS & DNS --------------------------- #
digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
getcertnames() {
if [[ -z "${1}" ]]; then
log error "Failed to read certificate names (no domain given)"
return 1
fi
local domain="${1}"
echo "Testing ${domain}…"
echo ""
local tmp
tmp=$(printf 'GET / HTTP/1.0\nEOT\n' \
| openssl s_client -connect "${domain}:443" -servername "${domain}" 2>&1)
if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then
local certText
certText=$(openssl x509 -text -certopt "no_aux, no_header, no_issuer, no_pubkey, \
no_serial, no_sigdump, no_signame, no_validity, no_version" <<< "${tmp}")
echo "Common Name:"
echo ""
grep "Subject:" <<< "${certText}" | sed -e "s/^.*CN=//" -e "s/\/emailAddress=.*//"
echo ""
echo "Subject Alternative Name(s):"
echo ""
grep -A 1 "Subject Alternative Name:" <<< "${certText}" \
| sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2
return 0
else
log error "Failed to find a certificate for ${domain}"
return 1
fi
}
dns:change() {
if (($# < 2)); then
echo "Usage: dns:change <network_service> <comma_separated_dns_ips>" >&2
return 1
fi
local network_service="$1"
# networksetup rejects " 8.8.8.8" from a spaced list
local -a fields nameservers
IFS=',' read -rA fields <<<"$2"
local ns
for ns in "${fields[@]}"; do
read -r ns <<< "$ns"
[[ -n "$ns" ]] && nameservers+=("$ns")
done
(( ${#nameservers} )) || { echo "dns:change: no DNS servers in '$2'" >&2; return 1; }
sudo networksetup -setdnsservers "${network_service}" "${nameservers[@]}"
}
# ------------------------------------ SSH ----------------------------------- #
ssh:reagent() {
local agent
# Without (N) an unmatched glob is fatal and aborts the rest of .zshrc
for agent in /tmp/ssh-*/agent.*(N=); do
export SSH_AUTH_SOCK="${agent}"
if ssh-add -l &>/dev/null; then
log success "Found working SSH agent"
ssh-add -l
return 0
fi
done
unset SSH_AUTH_SOCK
log warning "Found no working SSH agent (reconnect with forwarding)"
return 1
}
# Test the inherited socket first, because ssh:reagent's glob only matches
# sockets sshd created for a forwarded agent, never launchd's. `ssh-add -l`
# exits 2 only when it cannot reach an agent, and 1 for one holding no keys.
ssh:agent() {
ssh-add -l &>/dev/null
(( $? == 2 )) || return 0
ssh:reagent &>/dev/null || eval "$(ssh-agent)" &>/dev/null
}
# --------------------------------- Secrets ---------------------------------- #
# Every read of the tokens file uses IFS=$'\t\t'. zsh treats a doubled IFS
# whitespace character as a plain separator (zshparam(1)), so empty fields
# survive the read instead of shifting the ones after them.
_SECRETS_TOKENS_FILE="${SECRETS_TOKENS_FILE}"
_SECRETS_KEYCHAIN="$HOME/Library/Keychains/${SECRETS_KEYCHAIN_NAME:-dotfiles}.keychain-db"
typeset -gA _SECRETS_FLAG_MAP=(name -l where -s kind -D comment -j)
_SECRETS_LOOKUP_FLAG="${_SECRETS_FLAG_MAP[${SECRETS_KEYCHAIN_LOOKUP_FIELD:-name}]:--l}"
# An empty SECRETS_KEYCHAIN_PASSWORD inherits the login session's unlock state,
# and a non-empty one is a master password
_secrets:ensure-keychain() {
if [[ ! -f "$_SECRETS_KEYCHAIN" ]]; then
local pw="${SECRETS_KEYCHAIN_PASSWORD:-}"
security create-keychain -p "$pw" "${_SECRETS_KEYCHAIN%-db}" >/dev/null
# Lock on sleep with no idle timeout
security set-keychain-settings -l "$_SECRETS_KEYCHAIN"
security unlock-keychain -p "$pw" "$_SECRETS_KEYCHAIN"
fi
if ! security list-keychains -d user 2>/dev/null | grep -qF -- "$_SECRETS_KEYCHAIN"; then
# xargs would re-parse the quotes `security` prints. An apostrophe in a path
# aborts the read and leaves `-s` setting a search list without
# login.keychain.
local -a paths=( ${${${(@f)"$(security list-keychains -d user)"}#*\"}%\"} )
security list-keychains -d user -s "$_SECRETS_KEYCHAIN" "${paths[@]}"
fi
}
_secrets:write-header() {
printf '# id\taccount\twhere\tkind\tcomment\tpassword\n# Managed by secrets:export. Run secrets:import on a new machine to load them into the keychain\n' > "$1"
}
_secrets:ensure-tokens-file() {
[[ -d "${_SECRETS_TOKENS_FILE:h}" ]] || mkdir -p "${_SECRETS_TOKENS_FILE:h}"
[[ -f "$_SECRETS_TOKENS_FILE" ]] || _secrets:write-header "$_SECRETS_TOKENS_FILE"
chmod 600 "$_SECRETS_TOKENS_FILE"
}
# dump-keychain never emits password data. It hex-encodes values holding
# non-ASCII bytes, and LC_ALL=C keeps sprintf("%c") byte-oriented while awk
# decodes them.
_secrets:entries() {
[[ -f "$_SECRETS_KEYCHAIN" ]] || return 0
security dump-keychain "$_SECRETS_KEYCHAIN" 2>/dev/null | LC_ALL=C awk '
function hx(c) { return index("0123456789abcdef", c) - 1 }
function hexdec(h, i, out) {
h = tolower(h); out = ""
for (i = 1; i < length(h); i += 2)
out = out sprintf("%c", hx(substr(h, i, 1)) * 16 + hx(substr(h, i + 1, 1)))
return out
}
function val(line) {
if (line ~ /<blob>="/) {
sub(/^[^=]*="/, "", line); sub(/"$/, "", line); return line
}
if (line ~ /<blob>=0x/) {
sub(/^[^=]*=0x/, "", line); sub(/[[:space:]].*$/, "", line)
return hexdec(line)
}
return ""
}
function flush() {
if (cls == "\"genp\"" && (id != "" || acct != ""))
print id "\t" acct "\t" where "\t" kind "\t" comment
}
/^keychain:/ { flush(); cls = id = acct = where = kind = comment = "" }
/^class:/ { cls = $2 }
/^ *0x00000007 <blob>=/ { id = val($0) } # Label (-l)
/^ *"acct"<blob>=/ { acct = val($0) }
/^ *"svce"<blob>=/ { where = val($0) }
/^ *"desc"<blob>=/ { kind = val($0) }
/^ *"icmt"<blob>=/ { comment = val($0) }
END { flush() }
' | LC_ALL=C sort -f
}
# This uses _secrets:entries, the only parser that decodes hex values, and
# matches the lookup field so it agrees with _secrets:password
_secrets:attrs() {
local id acct where kind comment key
while IFS=$'\t\t' read -r id acct where kind comment; do
case "${SECRETS_KEYCHAIN_LOOKUP_FIELD:-name}" in
where) key="$where" ;;
kind) key="$kind" ;;
comment) key="$comment" ;;
*) key="$id" ;;
esac
[[ "$key" == "$1" && "$acct" == "$2" ]] || continue
print -r -- "${where}"$'\t'"${kind}"$'\t'"${comment}"
return 0
done < <(_secrets:entries)
return 1
}
_secrets:password() {
security find-generic-password "$_SECRETS_LOOKUP_FLAG" "$1" -a "$2" -w "$_SECRETS_KEYCHAIN" 2>/dev/null
}
# macOS keys generic passwords by (Service, Account), not by the Label used as
# the id, so `add-generic-password -U` replaces whatever entry holds the pair
_secrets:storage-taken() {
security find-generic-password -s "$1" -a "$2" "$_SECRETS_KEYCHAIN" >/dev/null 2>&1
}
_secrets:install() {
local id="$1" acct="$2" where="$3" kind="$4" password="$5" comment="${6:-}"
_secrets:ensure-keychain
# `security -i` keeps the secret out of the process table. Its parser reads
# the line itself, so this double quotes each value and escapes backslashes
# first.
local -a args=(-U -A -s "$where" -a "$acct" -l "$id" -D "$kind" -w "$password" "$_SECRETS_KEYCHAIN")
[[ -n "$comment" ]] && args=( -j "$comment" "${args[@]}" )
local line="add-generic-password" arg escaped
for arg in "${args[@]}"; do
escaped="${arg//\\/\\\\}"
line+=" \"${escaped//\"/\\\"}\""
done
if ! print -r -- "$line" | security -i; then
log error "Failed to update keychain"
return 1
fi
}
# macOS does not enforce a unique (id, account) pair, so this checks it
secret:set() {
(( $# >= 4 && $# <= 5 )) || { echo "Usage: secret:set <id> <account> <where> <kind> [comment]" >&2; return 1; }
local id="$1" account="$2" where="$3" kind="$4" comment="${5:-}" password
if _secrets:password "$id" "$account" >/dev/null 2>&1; then
log error "Rejected $id / $account (already exists, run secret:remove first to replace the value)"
return 1
fi
if _secrets:storage-taken "$where" "$account"; then
log error "Rejected $id / $account (another entry already holds this where and account, run secrets:list to find it)"
return 1
fi
printf "Password for %s/%s: " "$id" "$account"
read -rs password
echo
[[ -n "$password" ]] || { log error "Canceled (password cannot be empty)"; return 1; }
_secrets:install "$id" "$account" "$where" "$kind" "$password" "$comment" || return 1
log success "Added $id / $account ($kind)"
secrets:export
}
secret:get() {
(( $# == 2 )) || { echo "Usage: secret:get <id> <account>" >&2; return 1; }
_secrets:password "$1" "$2" || { log error "Failed to find $1 / $2"; return 1; }
}
secret:copy() {
(( $# == 2 )) || { echo "Usage: secret:copy <id> <account>" >&2; return 1; }
local password snapshot ttl="${SECRETS_CLIPBOARD_TTL:-30}"
password="$(_secrets:password "$1" "$2")" \
|| { log error "Failed to find $1 / $2"; return 1; }
# The ConcealedType marker keeps clipboard managers such as Raycast from
# recording the value, and CurrentHostOnly keeps it off Universal Clipboard
printf '%s' "$password" | osascript -l JavaScript -e '
ObjC.import("AppKit")
const data = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile
const pb = $.NSPasteboard.generalPasteboard
pb.prepareForNewContentsWithOptions($.NSPasteboardContentsCurrentHostOnly)
pb.setStringForType($.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding), "public.utf8-plain-text")
pb.setStringForType($(""), "org.nspasteboard.ConcealedType")' >/dev/null \
|| { log error "Failed to copy $1 / $2 to the clipboard"; return 1; }
snapshot=$(printf '%s' "$password" | shasum -a 256 | awk '{print $1}')
unset password
( sleep "$ttl"
[[ "$(pbpaste 2>/dev/null | shasum -a 256 | awk '{print $1}')" == "$snapshot" ]] \
&& printf '' | pbcopy
) &!
log success "Copied $1 / $2 to the clipboard (auto-clears in ${ttl}s)"
}
secret:remove() {
(( $# == 2 )) || { echo "Usage: secret:remove <id> <account>" >&2; return 1; }
if security delete-generic-password "$_SECRETS_LOOKUP_FLAG" "$1" -a "$2" "$_SECRETS_KEYCHAIN" >/dev/null 2>&1; then
log success "Removed $1 / $2"
else
log warning "Skipped $1 / $2 (not in keychain)"
fi
secrets:export
}
secret:rename() {
(( $# >= 4 && $# <= 7 )) || {
echo "Usage: secret:rename <old_id> <old_account> <new_id> <new_account> [new_where] [new_kind] [new_comment]" >&2
return 1
}
local old_id="$1" old_acct="$2" new_id="$3" new_acct="$4" password new_where new_kind new_comment
# same_lookup means the target cannot collide under the lookup field.
# same_storage means `add-generic-password -U` updates this item in place.
local same_lookup=0
[[ "$old_id" == "$new_id" && "$old_acct" == "$new_acct" ]] && same_lookup=1
password="$(_secrets:password "$old_id" "$old_acct")" \
|| { log error "Failed to find $old_id / $old_acct"; return 1; }
if (( ! same_lookup )) && _secrets:password "$new_id" "$new_acct" >/dev/null; then
log error "Rejected $new_id / $new_acct (target already exists)"
return 1
fi
local cur_where cur_kind cur_comment
IFS=$'\t\t' read -r cur_where cur_kind cur_comment < <(_secrets:attrs "$old_id" "$old_acct")
new_where="${5:-$cur_where}"
new_kind="${6:-$cur_kind}"
new_comment="${7:-$cur_comment}"
local same_storage=0
[[ "$new_where" == "$cur_where" && "$new_acct" == "$old_acct" ]] && same_storage=1
if (( ! same_storage )) && _secrets:storage-taken "$new_where" "$new_acct"; then
log error "Rejected $new_id / $new_acct (another entry already holds this where and account, run secrets:list to find it)"
return 1
fi
_secrets:install "$new_id" "$new_acct" "$new_where" "$new_kind" "$password" "$new_comment" || return 1
# Delete by the storage pair, because the new item may share the old Label
if (( ! same_storage )) \
&& ! security delete-generic-password -s "$cur_where" -a "$old_acct" "$_SECRETS_KEYCHAIN" >/dev/null 2>&1; then
log warning "Skipped removing $old_id / $old_acct (copy left behind, remove it with secret:remove)"
fi
log success "Renamed $old_id / $old_acct → $new_id / $new_acct ($new_kind)"
secrets:export
}
secrets:list() {
if [[ ! -f "$_SECRETS_KEYCHAIN" ]]; then
log warning "Failed to find keychain ($_SECRETS_KEYCHAIN)"
return 1
fi
{
print -r -- $'ID\tACCOUNT\tKIND\tUSED BY\tWHERE'
local id account where kind comment
while IFS=$'\t\t' read -r id account where kind comment; do
print -r -- "$id"$'\t'"$account"$'\t'"${kind:-(none)}"$'\t'"${comment:-(none)}"$'\t'"${where:-(none)}"
done < <(_secrets:entries)
} | column -t -s $'\t'
}
# This imports entries the keychain lacks and leaves stale file passwords for
# secrets:export to overwrite. A password of "placeholder" marks a stub row. It
# reports stubs and rows whose Where and Account belong to another entry, and
# never imports them.
secrets:import() {
if [[ ! -f "$_SECRETS_TOKENS_FILE" ]]; then
log warning "Failed to find tokens file ($_SECRETS_TOKENS_FILE)"
return 1
fi
local id acct where kind comment file_pw kc_pw state total=0 drift=0 imported=0 rc
local -a rows
while IFS=$'\t\t' read -r id acct where kind comment file_pw; do
[[ -z "$id" || "$id" == \#* ]] && continue
(( ++total ))
kc_pw="$(_secrets:password "$id" "$acct")"; rc=$?
if (( rc == 44 )); then # errSecItemNotFound
if [[ -z "$file_pw" || "$file_pw" == placeholder ]]; then
state="missing"; (( ++drift ))
elif _secrets:storage-taken "$where" "$acct"; then
state="conflict"; (( ++drift ))
elif _secrets:install "$id" "$acct" "$where" "$kind" "$file_pw" "$comment"; then
state="imported"; (( ++imported ))
else
state="failed"; (( ++drift ))
fi
elif (( rc != 0 )); then # Locked or denied, so never write blind
state="failed"; (( ++drift ))
elif [[ "$file_pw" == placeholder ]]; then
state="placeholder"; (( ++drift ))
elif [[ "$file_pw" != "$kc_pw" ]]; then
state="drift"; (( ++drift ))
else
state="ok"
fi
rows+=("$state"$'\t'"$id"$'\t'"$acct")
done < "$_SECRETS_TOKENS_FILE"
{
print -r -- $'STATUS\tID\tACCOUNT'
print -lr -- "${rows[@]}" | LC_ALL=C sort
} | column -t -s $'\t'
(( imported > 0 )) && log success "Imported $imported secret(s) into the keychain"
if (( drift > 0 )); then
log warning "Detected drift in $drift / $total entries (run secrets:export to sync, or secret:set to restore missing ones)"
return 1
fi
log success "Synced $total / $total entries"
}
# Lets secrets:export carry a value forward when the keychain read fails
_secrets:backed-up-password() {
[[ -f "$_SECRETS_TOKENS_FILE" ]] || return 1
local f_id f_acct f_where f_kind f_comment f_pw
while IFS=$'\t\t' read -r f_id f_acct f_where f_kind f_comment f_pw; do
[[ -z "$f_id" || "$f_id" == \#* ]] && continue
if [[ "$f_id" == "$1" && "$f_acct" == "$2" ]]; then
print -r -- "$f_pw"
return 0
fi
done < "$_SECRETS_TOKENS_FILE"
return 1
}
# Returns zero because secrets:export is the tail call of secret:set,
# secret:remove and secret:rename, which one unreadable entry must not fail
_secrets:report-unread() {
(( $1 )) && log warning "Carried $1 secret(s) over from the previous backup (failed to read them)"
(( $2 )) && log error "Dropped $2 secret(s) from the backup (failed to read, with no backed-up value to keep)"
return 0
}
secrets:export() {
_secrets:ensure-tokens-file
local tmp id account where kind comment password prior count=0 kept=0 dropped=0
tmp="$(mktemp)"
trap "command rm -f ${(q)tmp}" EXIT
_secrets:write-header "$tmp"
while IFS=$'\t\t' read -r id account where kind comment; do
# (Service, Account) is unique, so this finds the entry under any lookup
# field
if password="$(security find-generic-password -s "$where" -a "$account" -w "$_SECRETS_KEYCHAIN" 2>/dev/null)"; then
print -r -- "${id}"$'\t'"${account}"$'\t'"${where}"$'\t'"${kind}"$'\t'"${comment}"$'\t'"${password}" >> "$tmp"
(( ++count ))
# An item added outside secret:set has no -A ACL, so a denied prompt fails
# this read alone
elif prior="$(_secrets:backed-up-password "$id" "$account")" && [[ -n "$prior" ]]; then
print -r -- "${id}"$'\t'"${account}"$'\t'"${where}"$'\t'"${kind}"$'\t'"${comment}"$'\t'"${prior}" >> "$tmp"
(( ++kept ))
log warning "Kept the backed-up value for $id / $account (failed to read password)"
else
(( ++dropped ))
log error "Dropped $id / $account from the backup (failed to read password, no backed-up value to keep)"
fi
done < <(_secrets:entries)
# A fresh machine's iCloud backup can sync down after bootstrap has already
# run
if (( count == 0 )) && grep -qvE '^[[:space:]]*#|^[[:space:]]*$' "$_SECRETS_TOKENS_FILE"; then
command rm -f "$tmp"
trap - EXIT
log error "Refused to overwrite populated tokens file from an empty keychain (run secrets:import first)"
return 1
fi
while IFS=$'\t\t' read -r id account; do
[[ -n "$id" ]] && log warning "Dropped $id / $account (no longer in keychain)"
done < <(comm -23 \
<(awk -F'\t' '!/^[[:space:]]*#/ && NF>=2 {print $1 "\t" $2}' "$_SECRETS_TOKENS_FILE" | sort) \
<(awk -F'\t' '!/^[[:space:]]*#/ && NF>=2 {print $1 "\t" $2}' "$tmp" | sort))
# Skipping the write means iCloud only re-uploads on a real update
if cmp -s "$tmp" "$_SECRETS_TOKENS_FILE"; then
command rm -f "$tmp"
trap - EXIT
_secrets:report-unread "$kept" "$dropped"
log success "Verified $count secret(s) already in sync"
return 0
fi
if ! command mv "$tmp" "$_SECRETS_TOKENS_FILE"; then
log error "Failed to write the tokens file (${_SECRETS_TOKENS_FILE})"
return 1
fi
trap - EXIT
_secrets:report-unread "$kept" "$dropped"
log success "Exported $count secret(s) to iCloud Drive"
}
# ----------------------- macOS System Tweaks & Utilities -------------------- #
# A Focus mode hides runaway-watch's notification, so every new terminal repeats
# it. A list older than ten minutes means the agent stopped, and `^ms-0`
# excludes one dated in the future.
runaway:alerts() {
local alerts="${XDG_CACHE_HOME:-$HOME/.cache}/runaway-watch/alerts"
[[ -t 1 ]] || return 0
local -a current=( ${alerts}(N.L+0mm-10^ms-0) )
(( ${#current} )) || return 0
log warning "Detected runaway processes (details with runaway-watch status)" 2>&1
print -r -- "$(<"$alerts")"
}
# This uses sudo_local because every macOS update replaces /etc/pam.d/sudo
sudo:touch-id() {
local file='/etc/pam.d/sudo_local'
local line='auth sufficient pam_tid.so'
if [[ ! -f "${file}.template" ]]; then
log error "Failed to find ${file}.template (needs macOS 14 or newer)"
return 1
fi
if [[ -f "$file" ]] && sudo grep -qE '^[^#]*pam_tid\.so' "$file"; then
log info "Skipped pam_tid line (already present in ${file})"
return 0
fi
log info "Enabling Touch ID for sudo (${file})…"
if ! print -r -- "$line" | sudo tee -a "$file" >/dev/null; then
log error "Failed to write ${file}"
return 1
fi
sudo chmod 0444 "$file"
log success "Enabled Touch ID for sudo"
}
# On macOS 26 this only unhides "App Store and known developers" in Privacy &
# Security, which still has to be selected there
gatekeeper:disable() {
sudo spctl --global-disable
}
gatekeeper:enable() {
sudo spctl --global-enable
}
hostname:get() {
scutil --get ComputerName
}
hostname:set() {
if [[ $# -ne 1 ]]; then
echo "Usage: hostname:set <new_hostname>" >&2
return 1
fi
local new_hostname="$1"
# LocalHostName is the Bonjour name, so scutil rejects anything but letters,
# digits and hyphens, including the default_hostname of "void(0)"
local local_hostname="${new_hostname//[^a-zA-Z0-9-]/-}"
while [[ "$local_hostname" == *--* ]]; do
local_hostname="${local_hostname//--/-}"
done
local_hostname="${local_hostname#-}"
local_hostname="${local_hostname%-}"
if [[ -z "$local_hostname" ]]; then
log error "Failed to derive a local host name from \"$new_hostname\""
return 1
fi
sudo scutil --set ComputerName "$new_hostname" \
&& sudo scutil --set HostName "$new_hostname" \
&& sudo scutil --set LocalHostName "$local_hostname" \
|| { log error "Failed to set hostname \"$new_hostname\""; return 1; }
if [[ "$local_hostname" != "$new_hostname" ]]; then
log info "Set the local host name to \"$local_hostname\" (Bonjour names allow only letters, digits and hyphens)"
fi
log success "Set hostname \"$new_hostname\" (restart may be required)"
}
hostname:reset() {
hostname:set "${DEFAULT_HOSTNAME}"
}
dock:reset() {
defaults delete com.apple.dock
killall Dock