-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmctx
executable file
·155 lines (135 loc) · 3.75 KB
/
tmctx
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
#!/usr/bin/env bash
# @file tmctx
# Simple switch TMC contexts
# @author Alister Lewis-Bowen <alister@lewis-bowen.org>
# Adapted for TMC from kubectx
# @ref https://github.com/ahmetb/TMCTX
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
SELF_CMD="$0"
TMCTX="$HOME/.config/tmctx"
TMC_CONFIG_DIR="$HOME/.vmware-cna-saas"
usage() {
cat <<EOF
USAGE:
tmctx : list the contexts
tmctx <NAME> : switch to context <NAME>
tmctx - : switch to the previous context
tmctx -c, --current : show the current context name
tmctx -h,--help : show this message
EOF
}
exit_err() { echo >&2 "${1}"; exit 1; }
current_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/^[ ]*//'
}
get_contexts() {
tmc system context list | \
awk '{print $1}' | \
grep -v NAME | \
sort -n
}
list_contexts() {
set -u pipefail
local cur ctx_list
cur="$(current_context)" || exit_err "error getting current context"
ctx_list=$(get_contexts) || exit_err "error getting context list"
local yellow darkbg normal
yellow=$(tput setaf 3 || true)
darkbg=$(tput setab 0 || true)
normal=$(tput sgr0 || true)
local cur_ctx_fg cur_ctx_bg
cur_ctx_fg=${TMCTX_CURRENT_FGCOLOR:-$yellow}
cur_ctx_bg=${TMCTX_CURRENT_BGCOLOR:-$darkbg}
for c in $ctx_list; do
if [[ -n "${_TMCTX_FORCE_COLOR:-}" || \
-t 1 && -z "${NO_COLOR:-}" ]]; then
# colored output mode
if [[ "${c}" = "${cur}" ]]; then
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
else
echo "${c}"
fi
else
echo "${c}"
fi
done
}
read_context() {
if [[ -f "${TMCTX}" ]]; then
cat "${TMCTX}"
fi
}
save_context() {
local saved
saved="$(read_context)"
[ "${saved}" != "${1}" ] && printf %s "${1}" > "${TMCTX}"
}
switch_context() {
tmc system context use "${1}"
}
choose_context_interactive() {
local choice
choice="$(_TMCTX_FORCE_COLOR=1 \
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --height=40% --layout=reverse --info=inline --border --ansi --no-preview || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
else
set_context "${choice}"
fi
}
set_context() {
local prev
prev="$(current_context)" || exit_err "error getting current context"
switch_context "${1}"
if [[ "${prev}" != "${1}" ]]; then
save_context "${prev}"
fi
}
swap_context() {
local ctx
ctx="$(read_context)"
if [[ -z "${ctx}" ]]; then
echo "error: No previous context found." >&2
exit 1
fi
set_context "${ctx}"
}
main() {
if [[ "$#" -eq 0 ]]; then
if [[ -t 1 && -z "${TMCTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
choose_context_interactive
else
list_contexts
fi
elif [[ "$#" -gt 1 ]]; then
echo "error: too many arguments" >&2
usage
exit 1
elif [[ "$#" -eq 1 ]]; then
if [[ "${1}" == "-" ]]; then
swap_context
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
current_context
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage
exit 1
else
set_context "${1}"
fi
else
usage
exit 1
fi
}
main "$@"