Styling fixes

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2017-03-28 21:38:12 -07:00
parent 231f170968
commit 98847e0649
No known key found for this signature in database
GPG Key ID: 5C02521D7B216AD6

100
kubectx
View File

@ -1,4 +1,6 @@
#!/bin/bash #!/bin/bash
#
# kubectx(1) is a utility to manage and switch between kubectl contexts.
# Copyright 2017 Google Inc. # Copyright 2017 Google Inc.
# #
@ -14,32 +16,34 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
[[ -n $DEBUG ]] && set -x
set -eou pipefail set -eou pipefail
KUBECONFIG="${KUBECONFIG:-${HOME}/.kube/config}"
KUBECTX="${HOME}/.kube/kubectx" KUBECTX="${HOME}/.kube/kubectx"
function usage { usage() {
cat <<"EOF" cat <<"EOF"
USAGE: USAGE:
kubectx : list the contexts kubectx : list the contexts
kubectx <NAME> : switch to context kubectx <NAME> : switch to context
kubectx - : switch to the previous context kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : create alias for context kubectx <NEW_NAME>=<NAME> : create alias for context
kubectx -h|--help : show this message kubectx -h,--help : show this message
EOF EOF
exit 1
} }
function current_context { current_context() {
kubectl config view -o=jsonpath='{.current-context}' kubectl config view -o=jsonpath='{.current-context}'
} }
function get_contexts { get_contexts() {
kubectl config view \ kubectl config view \
-o=jsonpath='{range .contexts[*].name}{@}{"\n"}{end}' -o=jsonpath='{range .contexts[*].name}{@}{"\n"}{end}'
} }
function list_contexts { list_contexts() {
set -u pipefail set -u pipefail
local cur="$(current_context)" local cur="$(current_context)"
local bold=$(tput bold) local bold=$(tput bold)
@ -47,7 +51,7 @@ function list_contexts {
local normal=$(tput sgr0) local normal=$(tput sgr0)
for c in $(get_contexts); do for c in $(get_contexts); do
if [[ "$c" = "$cur" ]]; then if [[ "${c}" = "${cur}" ]]; then
echo "${bold}${yellow}$c${normal}" echo "${bold}${yellow}$c${normal}"
else else
echo "$c" echo "$c"
@ -55,63 +59,63 @@ function list_contexts {
done done
} }
function read_context { read_context() {
if [[ -f "$KUBECTX" ]]; then if [[ -f "${KUBECTX}" ]]; then
cat "$KUBECTX" cat "${KUBECTX}"
fi fi
} }
function save_context { save_context() {
local prev="$(read_context)" local prev="$(read_context)"
if [[ "$prev" != "$1" ]]; then if [[ "${prev}" != "${1}" ]]; then
printf %s "$1" > "$KUBECTX" printf %s "${1}" > "${KUBECTX}"
fi fi
} }
function switch_context { switch_context() {
kubectl config use-context "$1" kubectl config use-context "${1}"
} }
function set_context { set_context() {
set -e local prev="$(current_context)"
local prev="$(kubectl config current-context)"
switch_context "$1" switch_context "${1}"
if [[ -n "$prev" ]]; then # BUG(ahmetb) handle saved == $1, should be noop
save_context "$prev" if [[ -n "${prev}" ]]; then
save_context "${prev}"
fi fi
} }
function swap_context { swap_context() {
set -e set -e
local ctx="$(read_context)" local ctx="$(read_context)"
if [[ -z "$ctx" ]]; then if [[ -z "${ctx}" ]]; then
echo "No previous context found." echo "error: No previous context found." >&2
exit 1 exit 1
fi fi
set_context "$ctx" set_context "${ctx}"
} }
function user_of_context { user_of_context() {
kubectl config view \ kubectl config view \
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.user}" -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.user}"
} }
function cluster_of_context { cluster_of_context() {
kubectl config view \ kubectl config view \
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}" -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}"
} }
function alias_context { alias_context() {
local old_name="$1" local old_name="${1}"
local new_name="$2" local new_name="${2}"
local old_user="$(user_of_context $old_name)" local old_user="$(user_of_context "${old_name}")"
local old_cluster="$(cluster_of_context $old_name)" local old_cluster="$(cluster_of_context "${old_name}")"
if [[ -z "$old_user" || -z "$old_cluster" ]]; then if [[ -z "$old_user" || -z "$old_cluster" ]]; then
echo "Cannot retrieve context ${old_name}". echo "error: Cannot retrieve context ${old_name}." >&2
exit 1 exit 1
fi fi
@ -119,22 +123,30 @@ function alias_context {
--cluster="${old_cluster}" \ --cluster="${old_cluster}" \
--user="${old_user}" \ --user="${old_user}" \
echo "Aliased ${old_name} as ${new_name}." echo "Aliased ${old_name} as ${new_name}." >&2
} }
main() {
if [[ "$#" -eq 0 ]]; then if [[ "$#" -eq 0 ]]; then
list_contexts list_contexts
elif [[ "$#" -eq 1 ]]; then elif [[ "$#" -eq 1 ]]; then
if [[ "$1" = '-h' || "$1" = '--help' ]]; then if [[ "${1}" == "-" ]]; then
usage
elif [[ "$1" = "-" ]]; then
swap_context swap_context
elif [[ "$1" =~ (.+)=(.+) ]]; then elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" =~ -(.*) ]]; then
echo "error: unrecognized flag" >&2; usage
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else else
set_context "$1" set_context "${1}"
fi fi
else elif [[ "$#" -gt 1 ]]; then
echo "error: too many flags" >&2
usage usage
fi else
usage
fi
}
main "$@"