-
Notifications
You must be signed in to change notification settings - Fork 0
/
openstack-ps1.sh
executable file
·353 lines (305 loc) · 11.2 KB
/
openstack-ps1.sh
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
#!/bin/bash
# Kubernetes prompt helper for bash/zsh
# Displays current context and namespace
# Copyright 2019 Jon Mosco modified by Hugo Larcher for Openstackctx
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Default values for the prompt
# Override these values in ~/.zshrc or ~/.bashrc
OPENSTACK_PS1_BINARY="${OPENSTACK_PS1_BINARY:-openstackctxcli}"
OPENSTACK_PS1_SYMBOL_ENABLE="${OPENSTACK_PS1_SYMBOL_ENABLE:-false}"
OPENSTACK_PS1_SYMBOL_DEFAULT=${OPENSTACK_PS1_SYMBOL_DEFAULT:-$'\u1F3F7 '}
OPENSTACK_PS1_SYMBOL_USE_IMG="${OPENSTACK_PS1_SYMBOL_USE_IMG:-false}"
OPENSTACK_PS1_CONTEXT_ENABLE="${OPENSTACK_PS1_CONTEXT_ENABLE:-true}"
OPENSTACK_PS1_PREFIX="${OPENSTACK_PS1_PREFIX-(}"
OPENSTACK_PS1_SEPARATOR="${OPENSTACK_PS1_SEPARATOR-|}"
OPENSTACK_PS1_DIVIDER="${OPENSTACK_PS1_DIVIDER-:}"
OPENSTACK_PS1_SUFFIX="${OPENSTACK_PS1_SUFFIX-)}"
OPENSTACK_PS1_SYMBOL_COLOR="${OPENSTACK_PS1_SYMBOL_COLOR-blue}"
OPENSTACK_PS1_CTX_COLOR="${OPENSTACK_PS1_CTX_COLOR-red}"
OPENSTACK_PS1_NS_COLOR="${OPENSTACK_PS1_NS_COLOR-cyan}"
OPENSTACK_PS1_BG_COLOR="${OPENSTACK_PS1_BG_COLOR}"
OPENSTACK_PS1_OPENSTACKCONFIG_CACHE="${OPENSTACKCONFIG}"
OPENSTACK_PS1_DISABLE_PATH="${HOME}/.openstack/openstack-ps1/disabled"
OPENSTACK_PS1_LAST_TIME=0
OPENSTACK_PS1_CLUSTER_FUNCTION="${OPENSTACK_PS1_CLUSTER_FUNCTION}"
OPENSTACK_PS1_NAMESPACE_FUNCTION="${OPENSTACK_PS1_NAMESPACE_FUNCTION}"
# Determine our shell
if [ "${ZSH_VERSION-}" ]; then
OPENSTACK_PS1_SHELL="zsh"
elif [ "${BASH_VERSION-}" ]; then
OPENSTACK_PS1_SHELL="bash"
fi
_openstack_ps1_init() {
[[ -f "${OPENSTACK_PS1_DISABLE_PATH}" ]] && OPENSTACK_PS1_ENABLED=off
case "${OPENSTACK_PS1_SHELL}" in
"zsh")
_OPENSTACK_PS1_OPEN_ESC="%{"
_OPENSTACK_PS1_CLOSE_ESC="%}"
_OPENSTACK_PS1_DEFAULT_BG="%k"
_OPENSTACK_PS1_DEFAULT_FG="%f"
setopt PROMPT_SUBST
autoload -U add-zsh-hook
add-zsh-hook precmd _openstack_ps1_update_cache
zmodload -F zsh/stat b:zstat
zmodload zsh/datetime
;;
"bash")
_OPENSTACK_PS1_OPEN_ESC=$'\001'
_OPENSTACK_PS1_CLOSE_ESC=$'\002'
_OPENSTACK_PS1_DEFAULT_BG=$'\033[49m'
_OPENSTACK_PS1_DEFAULT_FG=$'\033[39m'
[[ $PROMPT_COMMAND =~ _openstack_ps1_update_cache ]] || PROMPT_COMMAND="_openstack_ps1_update_cache;${PROMPT_COMMAND:-:}"
;;
esac
_openstack_ps1_get_context
OPENSTACK_ENV_VARIABLES="$(${OPENSTACK_PS1_BINARY} activate "${OPENSTACK_PS1_CONTEXT}")"
retCode=$?
if [ $retCode -eq 0 ]; then
eval "${OPENSTACK_ENV_VARIABLES}"
fi
}
_openstack_ps1_color_fg() {
local OPENSTACK_PS1_FG_CODE
case "${1}" in
black) OPENSTACK_PS1_FG_CODE=0 ;;
red) OPENSTACK_PS1_FG_CODE=1 ;;
green) OPENSTACK_PS1_FG_CODE=2 ;;
yellow) OPENSTACK_PS1_FG_CODE=3 ;;
blue) OPENSTACK_PS1_FG_CODE=4 ;;
magenta) OPENSTACK_PS1_FG_CODE=5 ;;
cyan) OPENSTACK_PS1_FG_CODE=6 ;;
white) OPENSTACK_PS1_FG_CODE=7 ;;
# 256
[0-9] | [1-9][0-9] | [1][0-9][0-9] | [2][0-4][0-9] | [2][5][0-6]) OPENSTACK_PS1_FG_CODE="${1}" ;;
*) OPENSTACK_PS1_FG_CODE=default ;;
esac
if [[ "${OPENSTACK_PS1_FG_CODE}" == "default" ]]; then
OPENSTACK_PS1_FG_CODE="${_OPENSTACK_PS1_DEFAULT_FG}"
return
elif [[ "${OPENSTACK_PS1_SHELL}" == "zsh" ]]; then
OPENSTACK_PS1_FG_CODE="%F{$OPENSTACK_PS1_FG_CODE}"
elif [[ "${OPENSTACK_PS1_SHELL}" == "bash" ]]; then
if tput setaf 1 &>/dev/null; then
OPENSTACK_PS1_FG_CODE="$(tput setaf ${OPENSTACK_PS1_FG_CODE})"
elif [[ $OPENSTACK_PS1_FG_CODE -ge 0 ]] && [[ $OPENSTACK_PS1_FG_CODE -le 256 ]]; then
OPENSTACK_PS1_FG_CODE="\033[38;5;${OPENSTACK_PS1_FG_CODE}m"
else
OPENSTACK_PS1_FG_CODE="${_OPENSTACK_PS1_DEFAULT_FG}"
fi
fi
echo ${_OPENSTACK_PS1_OPEN_ESC}${OPENSTACK_PS1_FG_CODE}${_OPENSTACK_PS1_CLOSE_ESC}
}
_openstack_ps1_color_bg() {
local OPENSTACK_PS1_BG_CODE
case "${1}" in
black) OPENSTACK_PS1_BG_CODE=0 ;;
red) OPENSTACK_PS1_BG_CODE=1 ;;
green) OPENSTACK_PS1_BG_CODE=2 ;;
yellow) OPENSTACK_PS1_BG_CODE=3 ;;
blue) OPENSTACK_PS1_BG_CODE=4 ;;
magenta) OPENSTACK_PS1_BG_CODE=5 ;;
cyan) OPENSTACK_PS1_BG_CODE=6 ;;
white) OPENSTACK_PS1_BG_CODE=7 ;;
# 256
[0-9] | [1-9][0-9] | [1][0-9][0-9] | [2][0-4][0-9] | [2][5][0-6]) OPENSTACK_PS1_BG_CODE="${1}" ;;
*) OPENSTACK_PS1_BG_CODE=$'\033[0m' ;;
esac
if [[ "${OPENSTACK_PS1_BG_CODE}" == "default" ]]; then
OPENSTACK_PS1_FG_CODE="${_OPENSTACK_PS1_DEFAULT_BG}"
return
elif [[ "${OPENSTACK_PS1_SHELL}" == "zsh" ]]; then
OPENSTACK_PS1_BG_CODE="%K{$OPENSTACK_PS1_BG_CODE}"
elif [[ "${OPENSTACK_PS1_SHELL}" == "bash" ]]; then
if tput setaf 1 &>/dev/null; then
OPENSTACK_PS1_BG_CODE="$(tput setab ${OPENSTACK_PS1_BG_CODE})"
elif [[ $OPENSTACK_PS1_BG_CODE -ge 0 ]] && [[ $OPENSTACK_PS1_BG_CODE -le 256 ]]; then
OPENSTACK_PS1_BG_CODE="\033[48;5;${OPENSTACK_PS1_BG_CODE}m"
else
OPENSTACK_PS1_BG_CODE="${DEFAULT_BG}"
fi
fi
echo ${OPEN_ESC}${OPENSTACK_PS1_BG_CODE}${CLOSE_ESC}
}
_openstack_ps1_binary_check() {
command -v $1 >/dev/null
}
_openstack_ps1_symbol() {
[[ "${OPENSTACK_PS1_SYMBOL_ENABLE}" == false ]] && return
case "${OPENSTACK_PS1_SHELL}" in
bash)
if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u1F3F7 ' != "\\u1F3F7 " ]]; then
OPENSTACK_PS1_SYMBOL="${OPENSTACK_PS1_SYMBOL_DEFAULT}"
OPENSTACK_PS1_SYMBOL=$'\u1F3F7 '
OPENSTACK_PS1_SYMBOL_IMG=$'\u1F3F7 '
else
OPENSTACK_PS1_SYMBOL=$'\xF0\x9F\x8F\xB7 '
OPENSTACK_PS1_SYMBOL_IMG=$'\xF0\x9F\x8F\xB7 '
fi
;;
zsh)
OPENSTACK_PS1_SYMBOL="${OPENSTACK_PS1_SYMBOL_DEFAULT}"
OPENSTACK_PS1_SYMBOL_IMG="\u1F3F7 "
;;
*)
OPENSTACK_PS1_SYMBOL="openstack"
;;
esac
if [[ "${OPENSTACK_PS1_SYMBOL_USE_IMG}" == true ]]; then
OPENSTACK_PS1_SYMBOL="${OPENSTACK_PS1_SYMBOL_IMG}"
fi
echo "${OPENSTACK_PS1_SYMBOL}"
}
_openstack_ps1_split() {
type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
local IFS=$1
echo $2
}
_openstack_ps1_file_newer_than() {
local mtime
local file=$1
local check_time=$2
if [[ "${OPENSTACK_PS1_SHELL}" == "zsh" ]]; then
mtime=$(zstat -L +mtime "${file}")
elif stat -c "%s" /dev/null &>/dev/null; then
# GNU stat
mtime=$(stat -L -c %Y "${file}")
else
# BSD stat
mtime=$(stat -L -f %m "$file")
fi
[[ "${mtime}" -gt "${check_time}" ]]
}
_openstack_ps1_update_cache() {
local return_code=$?
[[ "${OPENSTACK_PS1_ENABLED}" == "off" ]] && return $return_code
if ! _openstack_ps1_binary_check "${OPENSTACK_PS1_BINARY}"; then
# No ability to fetch context/namespace; display N/A.
OPENSTACK_PS1_CONTEXT="BINARY-N/A"
OPENSTACK_PS1_NAMESPACE="N/A"
return
fi
if [[ "${OPENSTACKCONFIG}" != "${OPENSTACK_PS1_OPENSTACKCONFIG_CACHE}" ]]; then
# User changed OPENSTACKCONFIG; unconditionally refetch.
OPENSTACK_PS1_OPENSTACKCONFIG_CACHE=${OPENSTACKCONFIG}
_openstack_ps1_get_context
return
fi
# openstackctl will read the environment variable $OPENSTACKCONFIG
# otherwise set it to ~/.openstack/config
local conf
for conf in $(_openstack_ps1_split : "${OPENSTACKCONFIG:-${HOME}/.openstack/config}"); do
[[ -r "${conf}" ]] || continue
if _openstack_ps1_file_newer_than "${conf}" "${OPENSTACK_PS1_LAST_TIME}"; then
_openstack_ps1_get_context
return
fi
done
return $return_code
}
_openstack_ps1_get_context() {
# Set the command time
if [[ "${OPENSTACK_PS1_SHELL}" == "bash" ]]; then
if ((BASH_VERSINFO[0] >= 4 && BASH_VERSINFO[1] >= 2)); then
OPENSTACK_PS1_LAST_TIME=$(printf '%(%s)T')
else
OPENSTACK_PS1_LAST_TIME=$(date +%s)
fi
elif [[ "${OPENSTACK_PS1_SHELL}" == "zsh" ]]; then
OPENSTACK_PS1_LAST_TIME=$EPOCHSECONDS
fi
if [[ "${OPENSTACK_PS1_CONTEXT_ENABLE}" == true ]]; then
OPENSTACK_PS1_CONTEXT="$(${OPENSTACK_PS1_BINARY} current-context 2>/dev/null)"
# Set namespace to 'N/A' if it is not defined
OPENSTACK_PS1_CONTEXT="${OPENSTACK_PS1_CONTEXT:-N/A}"
if [[ ! -z "${OPENSTACK_PS1_CLUSTER_FUNCTION}" ]]; then
OPENSTACK_PS1_CONTEXT=$($OPENSTACK_PS1_CLUSTER_FUNCTION $OPENSTACK_PS1_CONTEXT)
fi
fi
}
# Set openstack-ps1 shell defaults
_openstack_ps1_init
_openstackon_usage() {
cat <<"EOF"
Toggle openstack-ps1 prompt on
Usage: openstackon [-g | --global] [-h | --help]
With no arguments, turn on openstack-ps1 status for this shell instance (default).
-g --global turn on openstack-ps1 status globally
-h --help print this message
EOF
}
_openstackoff_usage() {
cat <<"EOF"
Toggle openstack-ps1 prompt off
Usage: openstackoff [-g | --global] [-h | --help]
With no arguments, turn off openstack-ps1 status for this shell instance (default).
-g --global turn off openstack-ps1 status globally
-h --help print this message
EOF
}
openstackon() {
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_openstackon_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
rm -f -- "${OPENSTACK_PS1_DISABLE_PATH}"
elif [[ "$#" -ne 0 ]]; then
echo -e "error: unrecognized flag ${1}\\n"
_openstackon_usage
return
fi
OPENSTACK_PS1_ENABLED=on
}
openstackoff() {
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_openstackoff_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
mkdir -p -- "$(dirname "${OPENSTACK_PS1_DISABLE_PATH}")"
touch -- "${OPENSTACK_PS1_DISABLE_PATH}"
elif [[ $# -ne 0 ]]; then
echo "error: unrecognized flag ${1}" >&2
_openstackoff_usage
return
fi
OPENSTACK_PS1_ENABLED=off
}
openstack_ps1() {
[[ "${OPENSTACK_PS1_ENABLED}" == "off" ]] && return
[[ -z "${OPENSTACK_PS1_CONTEXT}" ]] && [[ "${OPENSTACK_PS1_CONTEXT_ENABLE}" == true ]] && return
local OPENSTACK_PS1
local OPENSTACK_PS1_RESET_COLOR="${_OPENSTACK_PS1_OPEN_ESC}${_OPENSTACK_PS1_DEFAULT_FG}${_OPENSTACK_PS1_CLOSE_ESC}"
# Background Color
[[ -n "${OPENSTACK_PS1_BG_COLOR}" ]] && OPENSTACK_PS1+="$(_openstack_ps1_color_bg ${OPENSTACK_PS1_BG_COLOR})"
# Prefix
[[ -n "${OPENSTACK_PS1_PREFIX}" ]] && OPENSTACK_PS1+="${OPENSTACK_PS1_PREFIX}"
# Symbol
OPENSTACK_PS1+="$(_openstack_ps1_color_fg $OPENSTACK_PS1_SYMBOL_COLOR)$(_openstack_ps1_symbol)${OPENSTACK_PS1_RESET_COLOR}"
if [[ -n "${OPENSTACK_PS1_SEPARATOR}" ]] && [[ "${OPENSTACK_PS1_SYMBOL_ENABLE}" == true ]]; then
OPENSTACK_PS1+="${OPENSTACK_PS1_SEPARATOR}"
fi
# Context
if [[ "${OPENSTACK_PS1_CONTEXT_ENABLE}" == true ]]; then
OPENSTACK_PS1+="$(_openstack_ps1_color_fg $OPENSTACK_PS1_CTX_COLOR)${OPENSTACK_PS1_CONTEXT}${OPENSTACK_PS1_RESET_COLOR}"
fi
# Suffix
[[ -n "${OPENSTACK_PS1_SUFFIX}" ]] && OPENSTACK_PS1+="${OPENSTACK_PS1_SUFFIX}"
# Close Background color if defined
[[ -n "${OPENSTACK_PS1_BG_COLOR}" ]] && OPENSTACK_PS1+="${_OPENSTACK_PS1_OPEN_ESC}${_OPENSTACK_PS1_DEFAULT_BG}${_OPENSTACK_PS1_CLOSE_ESC}"
echo "${OPENSTACK_PS1}"
}
openstackctx() {
OPENSTACK_ENV_VARIABLES="$(${OPENSTACK_PS1_BINARY})"
retCode=$?
if [ $retCode -eq 0 ]; then
eval "${OPENSTACK_ENV_VARIABLES}"
fi
}