-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
212 lines (184 loc) · 5.97 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
# key bindings
bindkey -v
bindkey '^w' backward-kill-word
bindkey '^r' history-incremental-search-backward
export KEYTIMEOUT=1
# aliases
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias ip='ip --color=auto'
alias ll='ls -alFh'
alias xcopy='xclip -selection clipboard'
alias xpaste='xclip -selection clipboard -out'
# history
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt extended_history
setopt append_history
setopt share_history
setopt hist_find_no_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
# prompts
autoload -U colors && colors
setopt prompt_subst
PS1=$'%{\e[01;92m%n@%m%}%{\e[0m:%}%{\e[01;94m%~%}\n%(?..%{\e[01;91m%}[%?])%# %{\e[0m%}'
PS4='+%N:%i:%_>'
RPROMPT='$(rprompt)'
function rprompt {
if [[ -z "$AWS_PROFILE" && -z "$KUBECONFIG" ]]; then
return
fi
if [[ "$AWS_PROFILE" =~ "prod" && ! "$AWS_PROFILE" =~ "readonly" ]]; then
aws_profile_color="red"
else
aws_profile_color="green"
fi
if [[ "$KUBECONFIG" =~ "prod" ]]; then
kube_config_color="red"
else
kube_config_color="green"
fi
if [[ ! -z "$KUBECONFIG" ]]; then
kube_config="$(basename $KUBECONFIG)"
fi
echo "( %{$fg[$aws_profile_color]%}$AWS_PROFILE%{$reset_color%} | %{$fg[$kube_config_color]%}$kube_config%{$reset_color%} )"
}
# completion
zmodload zsh/complist
setopt complete_in_word
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*:defualt' menu 'select=0'
zstyle ':completion::*:::' completer _complete _prefix
autoload -Uz compinit
compinit
# Enable ESC v to edit command line
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
. ~/.zsh/terminal.zsh
export JQ_COLORS="1;32:0;39:0;39:0;39:0;32:1;39:1;39"
if [ $commands[kubectl] ]; then
alias k=kubectl
kubeconfig() {
local new_config="$1"
if [[ -z "$new_config" ]]; then
new_config=$(find "$HOME/.kube" -maxdepth 1 -type f \
| xargs --max-args 1 basename \
| fzf --header 'Cluster')
fi
if [[ -z "$new_config" ]]; then
return
fi
if [[ ! -f "$HOME/.kube/$new_config" ]]; then
echo 1>&2 "ERROR: Kube config file not found $HOME/.kube/$new_config"
return
fi
export KUBECONFIG="$HOME/.kube/$new_config"
}
_kubeconfig() {
_alternative \
"args:kubernetes contexts:($(find "$HOME/.kube" -maxdepth 1 -type f \
| xargs --max-args 1 basename))"
}
compdef _kubeconfig kubeconfig
fi
if [ $commands[aws] ]; then
awsprofile() {
local new_aws_profile="$1"
if [[ -z "$new_aws_profile" ]]; then
new_aws_profile=$(env --unset=AWS_PROFILE aws configure list-profiles \
| fzf --header "AWS profile")
fi
if [[ -z "$new_aws_profile" ]]; then
return
fi
if ! aws --profile "$new_aws_profile" sts get-caller-identity > /dev/null 2>&1; then
bwpass 'discovery sso' > /dev/null # exports BW_SESSION
expect-gimme-aws-creds "$new_aws_profile" "$(bwpass 'discovery sso')"
fi
export AWS_PROFILE="$new_aws_profile"
aws sts get-caller-identity
}
_awsprofile() {
_alternative \
"args:aws profiles:($(aws configure list-profiles))"
}
compdef _awsprofile awsprofile
awsecrlogin() {
aws_account_id=$(aws sts get-caller-identity --query 'Account' --output text)
aws --region "eu-west-1" ecr get-login-password \
| docker login \
--username AWS \
--password-stdin \
"https://$aws_account_id.dkr.ecr.eu-west-1.amazonaws.com"
}
if [ $commands[aws_completer] ]; then
autoload bashcompinit && bashcompinit
complete -C "$(which aws_completer)" aws
fi
fi
if [ $commands[bw] ]; then
bwpass() {
bw_create_session() {
if ! bw login --check > /dev/null 2>&1; then
bw login
elif ! (echo "" | bw list items > /dev/null 2>&1); then
bw unlock
else
echo "export BW_SESSION=\"$BW_SESSION\""
fi
}
bw_get_session() {
while true; do
session_output="$(bw_create_session)"
if [[ -n "$session_output" ]]; then
break
fi
done
if [[ -n "$session_output" ]]; then
export BW_SESSION="$(echo "$session_output" \
| grep "export" \
| sed -r 's/.*BW_SESSION="(.*)".*/\1/g')"
fi
}
bw_get_session
unfunction bw_create_session
unfunction bw_get_session
if [[ -z "$1" ]]; then
result="$(bw list items)"
else
search_term="$1"
result="$(bw list items --search "$search_term")"
fi
if echo -E "$result" | grep --quiet 'Session key is invalid.'; then
unset BW_SESSION
result="$(echo -E "$result" | tail --lines +2)"
fi
number_of_results="$(echo -E "$result" | jq 'length')"
if [[ "$number_of_results" == "1" ]]; then
item_id="$(echo -E "$result" | jq --raw-output '.[0].id')"
bw get password "$item_id"
else
>&2 echo "Usage: $(basename "$0") [search_term]"
>&2 echo "Expected to find exactly on match, found $number_of_results."
>&2 echo ""
echo -E "$result" | jq --raw-output '.[] | .name + " (" + .login.uris[0].uri + ")"' >&2
fi
}
fi
if [ $commands[nix-shell] ]; then
nixzsh() {
nix-shell "$HOME/nix-shells/$1" --run zsh
}
_nixzsh() {
_alternative \
"args:nix-shells:($(find "$HOME/nix-shells" -maxdepth 1 -type f \
| xargs --max-args 1 basename))"
}
compdef _nixzsh nixzsh
fi
if [ -f "$HOME/.cargo/env" ]; then
source $HOME/.cargo/env
fi