-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
285 lines (256 loc) · 7.1 KB
/
functions
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
# These only work with zsh
find_up() {
if [[ -n "${2}" ]]; then
_path="${2}"
else
_path="${PWD}"
fi
while [[ "${_path}" != "" && ! -e "${_path}/${1}" ]]; do
_path="${_path%/*}"
done
if [[ -n "${_path}" ]]; then
echo "${_path}/${1}"
return 0
fi
return 1
}
find_up_all() {
if [[ -n "${2}" ]]; then
_path="${2}"
else
_path="${PWD}"
fi
_result=()
while [[ "${_path}" != "" ]]; do
[[ -e "${_path}/${1}" ]] && _result+=("${_path}/${1}")
_path="${_path%/*}"
done
if [[ $#_result > 0 ]]; then
printf "%s\n" "${(Oa)_result[@]}"
return 0
fi
return 1
}
# portable way to generate md5 checksums
_md5() {
if which md5sum 1>/dev/null 2>/dev/null; then
md5sum $@
elif which md5 1>/dev/null 2>/dev/null; then
echo "$(md5 $@ | grep -o "[a-f0-9]\{32\}") ${@:-1}"
else
echo "no md5 checksum tool found" >&2
return 127
fi
}
# use gnu tools on macos where available
gnu_exec() {
tool="${1}"
shift
case "${PLATFORM}" in
MacOS)
if whence -p "g${tool}" 1>/dev/null 2>/dev/null; then
command "g${tool}" $*
else
echo "command not found: g${tool}" >&2
return 127
fi
;;
*)
if whence -p "g${tool}" 1>/dev/null 2>/dev/null; then
command "g${tool}" $*
else
"${tool}" $*
fi
;;
esac
}
gsed() {
gnu_exec sed $*
}
greadlink() {
gnu_exec readlink $*
}
# set tmux window title
wt() {
if [ -z "${@}" ]; then
tmux set-window-option automatic-rename "on" 1>/dev/null
else
tmux rename-window "${@}"
fi
}
# ssh wrapper to set tmux window title
ssh() {
setopt RE_MATCH_PCRE
if [ -n "${TMUX_PANE}" ]; then
for p; do
if [[ "${p}" =~ '^([^ ]+@)?([a-zA-Z0-9](?:(?:[a-zA-Z0-9-]*|(?<!-)\.(?![-.]))*[a-zA-Z0-9]+)?)$' ]]; then
title="$(echo "${p}" | sed -r 's/^([^ ]+@)?(.+)$/\2/')"
break
fi
done
wt "${title}"
command ssh "$@"
wt
else
command ssh "$@"
fi
}
# invoke ssh and attach/create tmux session on remote host
tssh() {
# recognise a named session passed as the last argument
# this is crude, and means that:
# 1. the named session must be the last argument
# 2. if any other arguments other than the target host are passed, the session name must also be specified
# to be fixed up whenever there's nothing else to do
if [ -n "${2}" ]; then
SESSION_NAME="${@[-1]}"
SSH_ARGS="${@:1:-1}"
else
SESSION_NAME=tom
SSH_ARGS="${@}"
fi
CMD="ssh -t ${SSH_ARGS[*]} \"bash -l -c 'tmux at -t ${SESSION_NAME} || tmux new -s ${SESSION_NAME}'\""
eval $CMD
}
# use ssh completion for tssh function
compdef tssh=ssh
# print shell colours
shellcolors() {
for x in {0..8}; do
for i in {30..37}; do
for a in {40..47}; do
echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "
done
echo
done
done
echo
}
# print the 256 colour palette
prettycolors() {
for code in {000..255}; do print -P -- "$code: %F{$code}Pretty%f"; done
}
# set a custom suffix for liquidprompt
prompt_env_suffix() {
if [ -n "${1}" ]; then
LP_PS1_POSTFIX=$'%{\e[0;34m%}{'"${*}"$'}%{\e[0m%} '
else
LP_PS1_POSTFIX=
fi
}
#function cd() {
# [[ "${SHELL}" =~ "zsh" ]] && emulate -LR zsh
# builtin cd $@
# result=$?
# last="${@:-1}"
# if [[ $result != 0 ]] && [[ "${last}" =~ "shits" ]]; then
# echo "Did you mean? ${last//shits/shifts}"
# fi
#}
_ENVRC=".envrc"
_ENVRC_SUMFILE="${HOME}/.envrc.md5"
chpwd() {
source_envrc
}
precmd() {
echo -ne "\033]0;${PWD/#${HOME}/~} \007"
}
source_envrc() {
[[ "${SHELL}" =~ "zsh" ]] && emulate -LR zsh
[[ -o interactive ]] || return 0
[[ "${ZSH_SUBSHELL}" != "0" ]] && return 0
rcfile="$(find_up "${_ENVRC}" "${PWD}")"
[[ -n "${_ENVRC_SOURCED}" ]] && if [[ "${_ENVRC_SOURCED}" != "${rcfile}" ]] || [[ -z "${rcfile}" ]]; then
echo $'\e[0;32mUnloading '"${_ENVRC_SOURCED}"'\e[0m'
if typeset -f "envrc_destroy" >/dev/null; then
envrc_destroy
unset -f envrc_destroy
fi
unset _ENVRC_SOURCED
fi
if [[ "${1}" == "reload" || ( -z "${_ENVRC_SOURCED}" && "${PWD}" != "${HOME}" && -r "${rcfile}" ) ]]; then
[[ -f "${_ENVRC_SUMFILE}" ]] || touch "${_ENVRC_SUMFILE}"
md5="$(_md5 "${rcfile}")"
if grep -q "^${md5}$" "${_ENVRC_SUMFILE}"; then
echo $'\e[0;32mSourcing '"${rcfile}"'\e[0m'
export _ENVRC_SOURCED=$rcfile
source "${rcfile}"
else
echo $'\e[0;31mUnknown '"${_ENVRC}"' found at '"${rcfile}"'. Run `envrc allow` to approve\e[0m' >&2
fi
fi
}
envrc() {
if [ -z "${2}" ]; then
_dir="${PWD}"
#rcfiles="$(find_up_all "${_ENVRC}" "${PWD}")"
rcfiles="$(find_up "${_ENVRC}" "${PWD}")"
else
_dir="$(\cd "${2%"${_ENVRC}"}" && pwd)"
rcfiles="${_dir}/${_ENVRC}"
fi
case "${1}" in
"allow")
while read -r rcfile; do
if [[ -n "${rcfile}" ]] && [[ -r "${rcfile}" ]]; then
vared -p "Allow ${rcfile}? [Y/n]: " -c confirm
if [[ "${confirm}" == "y" ]]; then
gsed -i '\| '"${rcfile}"'$|d' "${_ENVRC_SUMFILE}"
md5="$(_md5 "${rcfile}")"
echo "${md5}" >>"${_ENVRC_SUMFILE}"
echo $'\e[0;34mAllowed '"${rcfile}"'\e[0m'
fi
unset confirm
else
echo $'\e[0;31m'"${_ENVRC}"' not found at '"${_dir}"'\e[0m' >&2
return 1
fi
done <<<"${rcfiles}"
[[ "${_dir}" == "${PWD}" ]] && source_envrc reload
;;
"reload")
_ENVRC_SOURCED=
chpwd
;;
"revoke")
if sed -i '\| '"${rcfile}"'$|d' "${_ENVRC_SUMFILE}"; then
echo $'\e[0;34mRevoked '"${rcfile}"'\e[0m'
else
echo $'\e[0;31mError revoking '"${rcfile}"'\e[0m' >&2
fi
;;
esac
}
showcert() {
openssl s_client -connect ${1}:${2:-443} -servername ${1} -showcerts <<<"Q" | openssl x509 -text -noout
}
az_version() {
python_version="3.10.0"
version="${1}"
if [[ "${version}" == "" ]]; then
echo "No version specified!" >&2
echo "Available versions:" >&2
pyenv versions | grep "${python_version}/envs/azure-cli-" | sed "s,${python_version}/envs/azure-cli-\([0-9.]*\).*,\1," 1>&2
return 1
fi
new_pyenv_version="${python_version}/envs/azure-cli-${version}"
old_pyenv_version="$(pyenv version | cut -d' ' -f1)"
old_version="$(pyenv version | cut -d' ' -f1 | sed "s,${python_version}/envs/azure-cli-,,")"
#[[ "${new_pyenv_version}" == "${old_pyenv_version}" ]] && return 0
if [[ -d "${HOME}/.azure" ]]; then
old_config_version="null"
[[ -r "${HOME}/.azure/versionCheck.json" ]] && old_config_version="$(jq -r '.versions["azure-cli"].local' <"${HOME}/.azure/versionCheck.json")"
if [[ "${old_config_version}" == "null" ]]; then
mv "${HOME}/.azure" "${HOME}/.azure-${old_config_version}"
else
mv "${HOME}/.azure" "${HOME}/.azure-${old_version}"
fi
fi
pyenv versions | grep -q "${new_pyenv_version}" || pyenv virtualenv "${python_version}" "azure-cli-${version}"
pyenv local "${new_pyenv_version}"
pip list 2>/dev/null | grep -q "azure-cli" || pip install azure-cli==${version}
rehash
[[ -d "${HOME}/.azure-${version}" ]] && mv "${HOME}/.azure-${version}" "${HOME}/.azure"
az version
}
# vim: set ft=zsh ts=2 sts=2 sw=2 et: