-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
537 lines (471 loc) · 14.5 KB
/
.zshrc
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
###
# zsh setup
###
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# In zsh, $path is an array with the values of $PATH, setting -U ensures it
# only contains unique entries
typeset -U path=("$path[@]")
# partial completion suggestions
zstyle ':completion:*' list-suffixes
zstyle ':completion:*' expand prefix suffix
# load bashcompinit for some old bash completions
autoload bashcompinit && bashcompinit
# Set up yadm completions
if [ -d /usr/share/doc/yadm/completion/zsh ]; then
fpath=(/usr/share/doc/yadm/completion/zsh $fpath)
fi
# Add deno completions to search path
if [[ ":$FPATH:" != *":$HOME/.zsh/completions:"* ]]; then export FPATH="$HOME/.zsh/completions:$FPATH"; fi
mkdir -p ~/.zfunc
fpath+=("$HOME/.zfunc")
if command -v pdm > /dev/null 2>&1; then
pdm completion zsh > ~/.zfunc/_pdm
fi
# Enable advanced auto completion
if command -v brew >/dev/null 2>&1 && [ -n "$ZSH_VERSION" ]; then
# Brew install zsh to "insecure" directories, -i silently ignores the results
# of those security checks
autoload -Uz compinit && compinit -i
else
autoload -Uz compinit && compinit
fi
# enable vim mode
set -o vi
# Enable command correction
setopt CORRECT
setopt CORRECT_ALL
# Where should the history file be saved
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
# Size of history file
SAVEHIST=5000
HISTSIZE=2000
# share history across multiple zsh sessions
setopt SHARE_HISTORY
# append to history
setopt APPEND_HISTORY
# adds commands as they are typed, not at shell exit
setopt INC_APPEND_HISTORY
# expire duplicates first
setopt HIST_EXPIRE_DUPS_FIRST
# do not store duplications
setopt HIST_IGNORE_DUPS
#ignore duplicates when searching
setopt HIST_FIND_NO_DUPS
# removes blank lines from history
setopt HIST_REDUCE_BLANKS
# Enable additional features in history
setopt EXTENDED_HISTORY
# Show `!!` command before executing
setopt HIST_VERIFY
# You may need to manually set your language environment
export LANG=en_US.UTF-8
###
# Path setup
###
# Local installations; add to front
path[1,0]="$HOME/.local/bin"
# Man files
if command -v man >/dev/null 2>&1; then
if ! [ "$MANPATH" ]; then
export MANPATH="$(man -w)"
fi
if ! echo $MANPATH | grep -q "$HOME/.local/share/man"; then
MANPATH="$HOME/.local/share/man:$MANPATH"
fi
fi
###
# Autocompletion setup
###
# Kubernetes
if command -v kubectl >/dev/null 2>&1; then
source <(kubectl completion zsh)
fi
# Helm
if command -v helm >/dev/null 2>&1; then
source <(helm completion zsh)
fi
# Minikube
if command -v minikube >/dev/null 2>&1; then
source <(minikube completion zsh)
fi
###
# Language setup
###
# Node
## npm packages
if command -v npm >/dev/null 2>&1 && [ -d "$HOME/.npm/bin" ] 2>&1; then
path+=("$HOME/.npm/bin")
fi
## n
if command -v n >/dev/null 2>&1; then
export N_PREFIX="$HOME/.n"
path+=("$N_PREFIX/bin")
fi
# deno
[[ -s "$HOME/.deno/env" ]] && source "$HOME/.deno/env"
# Ruby
if command -v gem >/dev/null 2>&1; then
export GEM_HOME="$HOME/.gem"
path+=("$GEM_HOME/bin")
fi
# Haskell
[ -f "$HOME/.ghcup/env" ] && . "$HOME/.ghcup/env" # ghcup-env
# Go
## GVM resets GOPATH so it is setup first
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
if command -v go >/dev/null 2>&1; then
export GOPATH=$HOME/.go
path+=("$GOPATH/bin")
## Modules; required for 1.11 through 1.12
export GO111MODULE=on
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
fi
# Python
## Set python3 as python if default python is not 3
if ! [[ "$(python -V 2>/dev/null)" =~ "Python 3" ]] && command -v python3 >/dev/null 2>&1; then
alias python="/usr/bin/env python3"
fi
alias pip="python -m pip"
export PYTHON_BASE="$(python -m site --user-base)"
export PYTHON_SITE="$(python -m site --user-site)"
path+=("$PYTHON_BASE/bin")
# update script to update all local pip packages
pip-update() {
pip --disable-pip-version-check list --user --outdated --format=json | \
python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" | \
xargs -n1 pip install --user -U
}
# Rust / Cargo
if command -v cargo >/dev/null 2>&1; then
path+=("$HOME/.cargo/bin")
fi
# Composer / PHP
[[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc
[[ -e ~/.phpbrew/bashrc ]] && export PHPBREW_RC_ENABLE=1
if command -v composer >/dev/null 2>&1; then
export COMPOSER_HOME="$HOME/.config/composer"
path+=("$COMPOSER_HOME/vendor/bin")
fi
if command -v php >/dev/null 2>&1; then
alias php="/usr/bin/env php -c $HOME/.config/php/php.ini"
fi
# OpenJDK on macOS
if [ -d "/usr/local/opt/openjdk/bin" ]; then
path+=("/usr/local/opt/openjdk/bin")
fi
# Docker desktop CLI on macOS
if [ -d "$HOME/.docker/bin" ]; then
path+=("$HOME/.docker/bin")
fi
##
# User configuration
###
## GPG
if command -v tty >/dev/null 2>&1; then
export GPG_TTY="$(tty)"
fi
# Tmux
# Force it to believe the terminal supports 256 colors
if command -v tmux >/dev/null 2>&1; then
alias tmux="/usr/bin/env tmux -2"
fi
# make less more friendly for non-text input files, see lesspipe(1)
if command -v lesspipe >/dev/null 2>&1; then
eval "$(SHELL=/bin/sh lesspipe)"
fi
if command -v eza >/dev/null 2>&1; then
# Use lsd as ls alias
alias ls="eza --color=auto --icons --group-directories-first"
else
# enable color support of ls and also add handy aliases
if command -v dircolors >/dev/null 2>&1; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
fi
alias ls="ls --color=auto"
fi
# colored GCC warnings and errors
export GCC_COLORS="error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01"
# color for grep
alias grep="grep --color=auto"
# Powerline
if [ -d "/usr/share/powerline" ]; then
export POWERLINE_PYTHON_BINDINGS="/usr/share/powerline"
else
export POWERLINE_PYTHON_BINDINGS="$PYTHON_SITE/powerline/bindings"
fi
export POWERLINE_VIM=""
if [ -f "/usr/share/vim/vimfiles/plugin/powerline.vim" ]; then
POWERLINE_VIM="/usr/share/vim/vimfiles/"
else
POWERLINE_VIM="$POWERLINE_PYTHON_BINDINGS/vim/"
fi
export POWERLINE_TMUX="/usr/share/tmux/powerline.conf";
if ! [ -f "$POWERLINE_TMUX" ]; then
POWERLINE_TMUX="$POWERLINE_PYTHON_BINDINGS/tmux/powerline.conf"
if ! [ -f "$POWERLINE_TMUX" ]; then
# Handle case where tmux config isn't installed
POWERLINE_TMUX="/tmp/powerline.conf"
touch "$POWERLINE_TMUX"
fi
fi
if command -v powerline-daemon >/dev/null 2>&1; then
powerline-daemon -q
fi
# starship
if command -v starship >/dev/null 2>&1; then
eval "$(starship init zsh)"
fi
# Support for vagrant access outside of a WSL environment
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
# Get latest release of a github repo
# Checks the tags for the latest release. Removes leading `v` from tag names and
# strips invalid JSON that comes back from the GitHub API
# https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#releases
# https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#tags
get-latest-github() {
if ! command -v jq >/dev/null; then
# can't confirm version without jq
return 1
fi
local OWNER="$1"
local REPO="$2"
if command -v gh >/dev/null 2>&1; then
# Use gh if available
local TAG_JSON="$(gh api repos/$OWNER/$REPO/tags)"
else
# otherwise fall back to curl
local TAG_JSON="$(curl -s -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/$OWNER/$REPO/tags")"
fi
if [ "$?" -ne 0 ]; then
# curl error
return 1
fi
# return latest tag
echo "$TAG_JSON" | tr '\r\n' ' ' | jq -r '.[].name' | sed 's/^v//' | grep -v '-' | sort --version-sort --reverse | head -n 1
return 0
}
# Check if ansible requirements are latest
is-latest-ansible-requirements() {
if ! command -v jq >/dev/null; then
# can't confirm version without jq
return 1
fi
local REQUIREMENT_NAMESPACES=()
local REQUIREMENTS=()
local REPO_OVERRIDES=()
# ansible.posix
REQUIREMENT_NAMESPACES+=("ansible")
REQUIREMENTS+=("posix")
REPO_OVERRIDES+=("")
# ansible.windows
REQUIREMENT_NAMESPACES+=("ansible")
REQUIREMENTS+=("windows")
REPO_OVERRIDES+=("")
# chocolatey.chocolatey
REQUIREMENT_NAMESPACES+=("chocolatey")
REQUIREMENTS+=("chocolatey")
REPO_OVERRIDES+=("chocolatey/chocolatey-ansible")
# community.general
REQUIREMENT_NAMESPACES+=("community")
REQUIREMENTS+=("general")
REPO_OVERRIDES+=("")
# community.windows
REQUIREMENT_NAMESPACES+=("community")
REQUIREMENTS+=("windows")
REPO_OVERRIDES+=("")
for ((i = 1; i <= $#REQUIREMENTS; i++)); do
local REQUIREMENT="${REQUIREMENTS[$i]}"
local REQUIREMENT_NAMESPACE="${REQUIREMENT_NAMESPACES[$i]}"
local REPO_OVERRIDE="${REPO_OVERRIDES[$i]}"
local LATEST_RELEASE=""
# in both cases, remove leading `v` if present
if [ -n "$REPO_OVERRIDE" ]; then
# split repo override by slash and pass as owner/repo arguments to latest
# release
LATEST_RELEASE="$(get-latest-github $(echo $REPO_OVERRIDE | awk '{split($0,a,"/"); print a[1],a[2]}') | sed 's/^[vV]*//g')"
else
LATEST_RELEASE="$(get-latest-github ansible-collections $REQUIREMENT_NAMESPACE.$REQUIREMENT | sed 's/^[vV]*//g')"
fi
if [ "$?" -ne 0 ]; then
>&2 echo "$REQUIREMENT_NAMESPACE.$REQUIREMENT could not get latest release"
return 1
fi
if ! [ -f "$HOME/.ansible/collections/ansible_collections/$REQUIREMENT_NAMESPACE/$REQUIREMENT/MANIFEST.json" ]; then
>&2 echo "$REQUIREMENT_NAMESPACE.$REQUIREMENT could not find current release"
return 1
fi
# remove leading `v` if present
local CURRENT_VERSION="$(cat "$HOME/.ansible/collections/ansible_collections/$REQUIREMENT_NAMESPACE/$REQUIREMENT/MANIFEST.json" | jq -r '.collection_info.version' | sed 's/^[vV]*//g')"
# final check
if [ "$LATEST_RELEASE" != "$CURRENT_VERSION" ]; then
>&2 echo "$REQUIREMENT_NAMESPACE.$REQUIREMENT non latest, have $CURRENT_VERSION want $LATEST_RELEASE"
return 1
fi
done
return 0
}
# Fetch a config file from expected locations
fetch_config_file() {
local FILE="$1"
# Check for local config override,
# per xdg specification
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
local POSSIBLE_CONFIG_DIRS=()
if [ -n "$XDG_CONFIG_HOME" ]; then
POSSIBLE_CONFIG_DIRS+=("$XDG_CONFIG_HOME")
else
POSSIBLE_CONFIG_DIRS+=("$HOME/.config")
fi
if [ -z "$XDG_CONFIG_DIRS" ]; then
POSSIBLE_CONFIG_DIRS+=("/etc/xdg")
else
# loop through XDG_CONFIG_DIRS
local IFS=":"
local XDG_CONFIG_DIR=""
for XDG_CONFIG_DIR in $XDG_CONFIG_DIRS; do
POSSIBLE_CONFIG_DIRS+=("$XDG_CONFIG_DIR")
done
fi
local POSSIBLE_CONFIG_DIR=""
for POSSIBLE_CONFIG_DIR in ${POSSIBLE_CONFIG_DIRS[@]}; do
if [ -f "$POSSIBLE_CONFIG_DIR/$FILE" ]; then
echo "$POSSIBLE_CONFIG_DIR/$FILE"
return 0
fi
done
# not found
return 1
}
# remove previous update alias (replaced with function below)
unalias update 2>/dev/null
# update command
# usage:
# ```sh
# update [host]
# ```
# host - A specific host to update, updates all if not provided
update() {
local HOST="$1"
local DEPENDENCIES=()
DEPENDENCIES+=("yadm")
DEPENDENCIES+=("ansible-galaxy")
DEPENDENCIES+=("ansible-playbook")
for DEPENDENCY in ${DEPENDENCIES[@]}; do
if ! command -v "$DEPENDENCY" >/dev/null; then
>&2 echo "Please install $DEPENDENCY before running this script"
return 1
fi
done
if [ -z "$IN_UPDATE" ]; then
# Authenticate with gh if not already
if command -v gh >/dev/null 2>&1; then
if ! gh auth status --hostname github.com >/dev/null 2>&1; then
gh auth login --hostname github.com
fi
fi
# get latest zshrc and load it
yadm pull origin main
source "$HOME/.zshrc"
IN_UPDATE="1"
update "$HOST"
return 0
fi
unset IN_UPDATE
# run updates
local ANSIBLE_DIR="$HOME/Projects/configuration/ansible"
local INVENTORY_FILE="$ANSIBLE_DIR/inventory.yaml"
local INVENTORY_OVERRIDE="$(fetch_config_file "/ansible/hosts")"
if [ -n "$INVENTORY_OVERRIDE" ]; then
INVENTORY_FILE="$INVENTORY_OVERRIDE"
fi
is-latest-ansible-requirements || ansible-galaxy collection install \
--force-with-deps \
--requirements-file "$ANSIBLE_DIR/requirements.yaml"
if [ -n "$HOST" ]; then
ansible-playbook \
--ask-vault-pass \
--inventory-file "$INVENTORY_FILE" \
--limit "$HOST" \
"$ANSIBLE_DIR/playbook.yaml"
else
ansible-playbook \
--ask-vault-pass \
--inventory-file "$INVENTORY_FILE" \
"$ANSIBLE_DIR/playbook.yaml"
fi
}
# Set editor
if command -v hx >/dev/null 2>&1; then
export EDITOR="$(which hx)"
elif command -v vim >/dev/null 2>&1; then
export EDITOR="$(which vim)"
elif command -v vi >/dev/null 2>&1; then
export EDITOR="$(which vi)"
fi
# Editor alias
edit() {
if command -v "$EDITOR" >/dev/null 2>&1; then
$EDITOR "$@"
elif command -v hx >/dev/null 2>&1; then
hx "$@"
elif command -v vim >/dev/null 2>&1; then
vim "$@"
elif command -v vi >/dev/null 2>&1; then
vi "$@"
else
>&2 echo "No suitable editor found"
fi
}
# Restart macOS sshd
restart-macos-sshd() {
if command -v launchctl >/dev/null 2>&1; then
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist && \
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
fi
}
# clear caches
clear-caches() {
if command -v dnf >/dev/null 2>&1; then
sudo dnf autoremove -y
sudo dnf clean all
fi
if command -v cargo >/dev/null 2>&1; then
if cargo install --list | grep 'cargo-cache' >/dev/null; then
cargo cache -a
fi
fi
if command -v go >/dev/null 2>&1; then
go clean -modcache
fi
if command -v composer >/dev/null 2>&1; then
composer clear-cache
fi
}
# Attach tmux when ssh session starts, exit when it exits
if command -v tmux >/dev/null 2>&1; then
if ([ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]) \
&& command -v tmux &> /dev/null \
&& [ -n "$PS1" ] \
&& [[ ! "$TERM" =~ screen ]] \
&& [[ ! "$TERM" =~ tmux ]] \
&& [ -z "$TMUX" ]; then
exec tmux new -A -s default && exit
fi
fi
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
## Get submodules to match latest committed commit in parent repo
alias fix-submodules="git submodule update --recursive -f"
# Includes, set this last so it can override other settings
INCLUDES_DIR="$HOME/.local/share/includes" && \
test -d "$INCLUDES_DIR" && \
INCLUDES=("${(@f)$(find -L "$HOME/.local/share/includes" -name '*.zsh')}") && \
for INCLUDE in $INCLUDES; do
source "$INCLUDE"
done