-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
.zshrc
265 lines (213 loc) · 7.56 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
[ -n "${ZPROF}" ] && zmodload zsh/zprof
export TMUX_TZ=$(date +%Z)
export COLORTERM=truecolor
# User config.
# Start emacs as a daemon.
alias emacs="emacsclient -a '' -c -t"
# Docker compose shortcuts in addition to the docker-compose oh-my-zsh plugin.
alias dcu='docker compose up -d --build -t 1'
alias dcup='docker compose up -t 1'
alias dcd='docker compose down -t 1'
alias dcda='docker compose down -t 1 -v --remove-orphans'
alias dcl='docker compose logs'
alias dclf='docker compose logs -f'
alias dce='docker compose exec'
alias dcr='docker compose run --rm'
#alias dcps='docker compose ps'
function dcps() {
local format
local project_name
local column_names
project_name=$(basename ${PWD})
format='{{.ID}}│{{printf "%.40s" .Image}}│{{.Command}}│{{.Label "com.docker.compose.service"}}│{{.Status}}│{{.Ports}}│{{.Networks}}│{{.Size}}'
column_names=$(echo "${format}" \
| sed 's/Label [^}]*/Service/' \
| sed 's/printf "[^"]*" //g' \
| sed 's/{{\.\([^}]*\)}}│*/\1,/g' \
| tr "[a-z]" "[A-Z]"
)
docker ps \
--all \
--filter "label=com.docker.compose.project=${project_name}" \
--format "${format}" \
$@ \
| column -t -s "│" -o ' ' -N "${column_names}"
}
# Set the uid/gid in the env for docker compose to use.
export UID GID
# Docker run with current user settings mounted in.
alias udockerrun='docker run --rm --user $(id -u):$(id -g) -e HOME -v $HOME:$HOME -w $(pwd) -e GOPATH=$HOME/go:/go'
# Docker wrappers for common tools.
alias swagger='udockerrun quay.io/goswagger/swagger'
alias protoc='udockerrun creack/grpc:go1.13-protobuf3.9.0-grpc1.24.0-protocgengo1.3.2'
alias prototool='udockerrun --entrypoint prototool creack/grpc:go1.13-protobuf3.9.0-grpc1.24.0-protocgengo1.3.2'
# Protobuf Go generation.
alias gprotoc='protoc --go_out=plugins=grpc:.'
# Protobuf Go Validations generation.
alias gvprotoc='gprotoc --validate_out=lang=go:.'
# GRPC Gateway generation.
alias gwprotoc='protoc --grpc-gateway_out="logtostderr=true:."'
# Swagger generation.
alias sprotoc='protoc --swagger_out="logtostderr=true:."'
# Recursive grep go file.
alias fggrep="fgrep -R --exclude-dir=vendor --exclude-dir=.cache --color --include='*.go'"
# Oh-my-zsh config.
# Disable completion security check as it is too slow. Don't manually add any completions before checking them.
ZSH_DISABLE_COMPFIX=true
ZSH_THEME="simple"
plugins=(
git
tmux
zsh-autosuggestions
zsh-completions
zsh-syntax-highlighting
nvm
yarn
docker
)
zstyle ':omz:plugins:nvm' lazy yes
NVM_CUSTOM_LAZY=true
# Set tmux autostart unless we are using vscode or emacs tramp.
if [ -n "$VSCODE_IPC_HOOK_CLI" ] || [ "$TERM" = "dumb" ] || [ -z "$TERM" ] || [ -f "$HOME/.notmux" ]; then
ZSH_TMUX_AUTOSTART=false
else
ZSH_TMUX_AUTOSTART=true
fi
# TODO: Add ssh-agent plugin when not in remote server.
# Allow agent-forwarding.
#zstyle :omz:plugins:ssh-agent agent-forwarding on
# Load oh-my-zsh.
export ZSH=~/.oh-my-zsh
source $ZSH/oh-my-zsh.sh
# Enable shared history so we can reference history between terms.
setopt share_history
# Save each command in history to make sure we don't loose it.
setopt inc_append_history
# Tell git to use the current tty for gpg passphrase prompt (needs to be at the end so the tty is within tmux, not out).
export GPG_TTY=$(tty)
function rl() {
local ssh_auth_sock=$(ls -t $(find /tmp/ssh-* -group $USER -name 'agent.*' 2> /dev/null) | head -1)
if [ -S "${ssh_auth_sock}" ]; then
echo "Refreshed ssh agent socket." >&2
export SSH_AUTH_SOCK=${ssh_auth_sock}
# If within tmux, update the session env as well.
[ -n "$TMUX" ] && tmux set-environment SSH_AUTH_SOCK ${SSH_AUTH_SOCK}
fi
}
# If the ssh agent socket is not set or expired, reload it.
if [ -z "$SSH_AUTH_SOCK" ] || [ ! -S "$SSH_AUTH_SOCK" ]; then
rl
fi
# Update the ~/.gitconfig.local link to target a new profile.
function setgit() {
local new_profile=$1
local profile_path="${HOME}/.gitconfig.${new_profile}"
local fail=0
# Make sure the target exists.
if [ ! -f "${profile_path}" ]; then
echo "Git profile '${new_profile}' not found." >&2
fail=1
fi
# Make sure the existing profile is a link and not a hard-set file.
if [ ! -L "${HOME}/.gitconfig.local" ]; then
echo "Error: The ~/.gitconfig.local file is not a link." >&2
fail=1
fi
if [ "${fail}" = 1 ]; then
return 1;
fi
ln -f -s ${profile_path} ${HOME}/.gitconfig.local
}
# Small helper used in the prompt to show the current git profile.
function getgit() {
if [ ! "$USER" = "root" ] && [ -f "${HOME}/.gitconfig.local" ]; then
ls -l ${HOME}/.gitconfig.local | sed 's/.*\.gitconfig\.//'
elif [ "$USER" = "root" ]; then
echo "root"
fi
}
# Small helper to cleanup aws env.
function unsetaws() {
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
unset AWS_REGION
unset AWS_DEFAULT_REGION
unset AWS_PROFILE
unset AWS_DEFAULT_PROFILE
}
# Show the git profile in the prompt.
export PROMPT='%{$fg_bold[yellow]%}[%m]%{$reset_color%}%{$fg_bold[blue]%}($(getgit))%{$reset_color%}'${PROMPT}
# Putty bindings for meta left/right
bindkey '\e\eOD' backward-word
bindkey '\e\eOC' forward-word
# Xterm bindings for meta left/right.
bindkey "^[[1;3D" backward-word
bindkey "^[[1;3C" forward-word
# Set M-l as lowercase word.
bindkey "^[l" down-case-word
# Load more autocompletions.
#autoload -Uz compinit && compinit
#autoload -U +X bashcompinit && bashcompinit
#complete -o nospace -C ${HOME}/go/bin/terraform terraform
#complete -o nospace -C ${HOME}/go/bin/vault vault
#complete -o nospace -C /usr/bin/docker docker
# Load the private config if set.
[ -f ~/.zshrc_priv_config ] && source ~/.zshrc_priv_config
# The NVM_LAZY implementation doesn't support loading the local .nvmrc
# and the NVM_AUTOLOAD is way too slow. Implement a custom loader.
if (( $+NVM_CUSTOM_LAZY )); then
function custom-load-nvmrc() {
# If we don't have a .nvmrc file, stop here.
if [ ! -f .nvmrc ]; then
return;
fi
# Undo the nvm lazy loading so we'll use the actual commands.
#unfunction node npm yarn 2> /dev/null 2> /dev/null
# If we already have nvm loaded, print the versions and stop here.
if [ -n "${NVM_BIN}" ]; then
local loaded_nvm_version=$(echo $NVM_BIN | sed 's|.*versions/node/\(.*\)/bin|\1|')
local loaded_nvm_major_version=$(echo $loaded_nvm_version | sed 's/v\?\([^.]*\).*/\1/')
local expected_nvm_major_version=$(cat .nvmrc | sed 's/v\?\([^.]*\).*/\1/')
if [ "${expected_nvm_major_version}" = "${loaded_nvm_major_version}" ]; then
echo "Loaded node version: ${loaded_nvm_version}, local nvmrc: $(cat .nvmrc)" >&2
return
fi
fi
# Load nvm with the local .nvmrc.
nvm use
}
autoload -U add-zsh-hook
add-zsh-hook chpwd custom-load-nvmrc
custom-load-nvmrc
fi
unset NVM_CUSTOM_LAZY
# Helper to load extra plugins at runtime.
function load-plugin() {
for p in "$@"; do
source $ZSH/plugins/"$p"/"$p".plugin.zsh
done
}
# Lazy load slow plugins.
function helm kubectl aws {
unfunction $0
echo -n "Lazy loading '$0' plugin... " >&2
load-plugin $0
echo "Done." >&2
$0 $@
}
if [ "$(uname -s)" = "Darwin" ]; then
unset LSCOLORS
alias ls="gls --color"
fi
export LS_COLORS=$(vivid generate gruvbox-dark-soft)
[ -n "${ZPROF}" ] && zprof
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
export DOCKER_BUILDKIT=1
# pnpm
export PNPM_HOME="/home/ubuntu/.local/share/pnpm"
case ":$PATH:" in
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac
# pnpm end