-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmc_prompt.sh
executable file
·136 lines (127 loc) · 4.56 KB
/
tmc_prompt.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
#!/usr/bin/env bash
# @file tmc_prompt.sh
# Functions to build a Bash TMC CLI prompt
# @author Alister Lewis-Bowen <[email protected]>
[[ -n $DEBUG ]] && set -x
# initialize defaults
_tmc_init() {
# shellcheck disable=SC2155
export TMC_PROMPT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export TMC_CONFIG_DIR="$HOME/.vmware-cna-saas"
export TMC_CONTEXT=''
export TMC_PROMPT=''
export TMC_PROMPT_FORMAT='⏣ #CONTEXT# #DEFAULTS#'
export TMC_PROMPT_DEFAULTS_FORMAT='(#MGMT_CLUSTER# ⇢ #PROVISIONER#)'
export TMC_PROMPT_ENABLED='on'
export TMC_PROMPT_DEFAULTS_ENABLED='on'
}
[[ -z "$TMC_PROMPT_SCRIPT_DIR" || "$1" == 'init' ]] && _tmc_init
# fetch the current context
_tmc_fetch_context() {
# Current context direct from tmc command...
# !! Need to parse yaml, e.g. tmc system context current | yq e '.full_name.name' -
grep name "$TMC_CONFIG_DIR/current-context" 2>/dev/null | \
cut -d':' -f2 | \
sed -e 's/^[ ]*//'
}
# parse out the value for a given key from the current context
_tmc_context_value_for_key() {
local key="$1"
tmc system context get "$TMC_CONTEXT" |\
grep "$key" |\
cut -d':' -f2 |\
sed -e 's/^[ ]*//'
}
# build the string used as a TMC
_tmc_build_prompt() {
local default mgmtCluster provisioner
# shellcheck disable=SC2155
export TMC_CONTEXT="$(_tmc_fetch_context)"
if [ -n "$TMC_CONTEXT" ]; then
prompt="${TMC_PROMPT_FORMAT//#CONTEXT#/$TMC_CONTEXT}"
if [[ "$TMC_PROMPT_DEFAULTS_ENABLED" == 'on' ]]; then
mgmtCluster=$(_tmc_context_value_for_key MANAGEMENT_CLUSTER_NAME)
provisioner=$(_tmc_context_value_for_key PROVISIONER_NAME)
default="${TMC_PROMPT_DEFAULTS_FORMAT//#MGMT_CLUSTER#/$mgmtCluster}"
default="${default//#PROVISIONER#/$provisioner}"
prompt="${prompt//#DEFAULTS#/$default}"
else
prompt="${prompt//#DEFAULTS#/}"
fi
export TMC_PROMPT="$prompt"
[[ "$TMC_PROMPT_ENABLED" == 'on' ]] && echo -en "$TMC_PROMPT"
else
return 1
fi
}
# toggle the visibility of the defaults in the TMC prompt, `tmc_defaults on|off`
tmc_defaults() { export TMC_PROMPT_DEFAULTS_ENABLED="$1"; }
# show or toggle the visibility of the TMC ptompt, `tmc_prompt on|off`
# shellcheck disable=SC2120
tmc_prompt() {
local toggle="${1:-}"
if [[ -z "$toggle" ]]; then
_tmc_build_prompt
else
export TMC_PROMPT_ENABLED="$toggle";
fi
}
# configure TMC prompt for the given prompt framework or for the generic PS1
tmc_configure_prompt() {
local framework="$1"
local configFile config
if [ -z "$framework" ]; then
echo "The following prompts are supported:"
echo " 1 ... starship"
echo " 2 ... generic Bash prompt"
until [[ "$REPLY" =~ ^-?[0-9]+$ && "$REPLY" -gt 0 && "$REPLY" -lt 3 ]]; do
# shellcheck disable=SC2162
read -p "✋ Select the prompt type you'd like me to configure [1|2] " -r -n 1
echo
done
echo
framework="$REPLY"
fi
case $framework in
1|starship)
configFile=~/.config/starship.toml
# shellcheck disable=SC1073
config=$(cat <<END_OF_STARSHIP_CONFIG
[custom.tmc]
description = "Display the current tmc context"
command = ". $TMC_PROMPT_SCRIPT_DIR/tmc_prompt.sh; tmc_prompt"
when= "command -v tmc 1>/dev/null 2>&1"
disabled = false
END_OF_STARSHIP_CONFIG
)
echo -e "Copy the following to $configFile:\n"
echo "$config"
echo
read -p "✋ Shall I append this to $configFile for you? [y/N] " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && {
echo "$config" >> "$configFile"
echo "✅ Added the TMC custom prompt to your starship configuration"
}
echo "Copy the following to your runcom, e.g. .bashrc or .bash_profile:\n"
echo "source ${PWD}/tmc_prompt.sh"
;;
powerline-go)
## TODO
;;
2|none)
echo -e "Copy the following to your runcom, e.g. .bashrc or .bash_profile:\n"
echo "source ${PWD}/tmc_prompt.sh"
echo "export PROMPT_COMMAND=\"tmc_prompt; \${PROMPT_COMMAND:-}\""
echo
;;
*)
echo "🤔 I don't recognize a framework called $framework"
;;
esac
}
# Return prompt if this script is called directly
[ "${BASH_SOURCE[0]}" -ef "$0" ] && {
tmc_prompt
[[ $(tty) =~ "not a tty" ]] || echo
}