mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-08-06 09:54:10 +00:00
unify message displaying
This commit is contained in:
parent
29850e1a75
commit
45791dad13
50
kubectx
50
kubectx
@ -26,6 +26,10 @@ SELF_CMD="$0"
|
|||||||
KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx"
|
KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
|
if [ -n "$1" ] ; then
|
||||||
|
err "$1"
|
||||||
|
fi
|
||||||
|
|
||||||
local SELF
|
local SELF
|
||||||
SELF="kubectx"
|
SELF="kubectx"
|
||||||
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
||||||
@ -47,13 +51,24 @@ USAGE:
|
|||||||
|
|
||||||
$SELF -h,--help : show this message
|
$SELF -h,--help : show this message
|
||||||
EOF
|
EOF
|
||||||
|
if [ -n "$1" ] ; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_err() {
|
exit_err() {
|
||||||
echo >&2 "${1}"
|
err "${1}"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err() {
|
||||||
|
warn "error: ${1}"
|
||||||
|
}
|
||||||
|
|
||||||
|
warn() {
|
||||||
|
echo >&2 "${1}"
|
||||||
|
}
|
||||||
|
|
||||||
current_context() {
|
current_context() {
|
||||||
$KUBECTL config view -o=jsonpath='{.current-context}'
|
$KUBECTL config view -o=jsonpath='{.current-context}'
|
||||||
}
|
}
|
||||||
@ -117,11 +132,9 @@ choose_context_interactive() {
|
|||||||
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
||||||
fzf --ansi --no-preview || true)"
|
fzf --ansi --no-preview || true)"
|
||||||
if [[ -z "${choice}" ]]; then
|
if [[ -z "${choice}" ]]; then
|
||||||
echo 2>&1 "error: you did not choose any of the options"
|
exit_err "you did not choose any of the options"
|
||||||
exit 1
|
|
||||||
else
|
|
||||||
set_context "${choice}"
|
|
||||||
fi
|
fi
|
||||||
|
set_context "${choice}"
|
||||||
}
|
}
|
||||||
|
|
||||||
set_context() {
|
set_context() {
|
||||||
@ -139,8 +152,7 @@ swap_context() {
|
|||||||
local ctx
|
local ctx
|
||||||
ctx="$(read_context)"
|
ctx="$(read_context)"
|
||||||
if [[ -z "${ctx}" ]]; then
|
if [[ -z "${ctx}" ]]; then
|
||||||
echo "error: No previous context found." >&2
|
exit_err "No previous context found."
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
set_context "${ctx}"
|
set_context "${ctx}"
|
||||||
}
|
}
|
||||||
@ -158,12 +170,11 @@ rename_context() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if ! context_exists "${old_name}"; then
|
if ! context_exists "${old_name}"; then
|
||||||
echo "error: Context \"${old_name}\" not found, can't rename it." >&2
|
exit_err "Context \"${old_name}\" not found, can't rename it."
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if context_exists "${new_name}"; then
|
if context_exists "${new_name}"; then
|
||||||
echo "Context \"${new_name}\" exists, deleting..." >&2
|
warn "Context \"${new_name}\" exists, deleting..."
|
||||||
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
|
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -182,12 +193,12 @@ delete_context() {
|
|||||||
if [[ "${ctx}" == "." ]]; then
|
if [[ "${ctx}" == "." ]]; then
|
||||||
ctx="$(current_context)" || exit_err "error getting current context"
|
ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
fi
|
fi
|
||||||
echo "Deleting context \"${ctx}\"..." >&2
|
warn "Deleting context \"${ctx}\"..."
|
||||||
$KUBECTL config delete-context "${ctx}"
|
$KUBECTL config delete-context "${ctx}"
|
||||||
}
|
}
|
||||||
|
|
||||||
unset_context() {
|
unset_context() {
|
||||||
echo "Unsetting current context." >&2
|
warn "Unsetting current context."
|
||||||
$KUBECTL config unset current-context
|
$KUBECTL config unset current-context
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,8 +209,7 @@ main() {
|
|||||||
elif hash kubectl.exe 2>/dev/null; then
|
elif hash kubectl.exe 2>/dev/null; then
|
||||||
KUBECTL=kubectl.exe
|
KUBECTL=kubectl.exe
|
||||||
else
|
else
|
||||||
echo >&2 "kubectl is not installed"
|
exit_err "kubectl is not installed"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -211,15 +221,11 @@ main() {
|
|||||||
fi
|
fi
|
||||||
elif [[ "${1}" == "-d" ]]; then
|
elif [[ "${1}" == "-d" ]]; then
|
||||||
if [[ "$#" -lt 2 ]]; then
|
if [[ "$#" -lt 2 ]]; then
|
||||||
echo "error: missing context NAME" >&2
|
usage "missing context NAME"
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
delete_contexts "${@:2}"
|
delete_contexts "${@:2}"
|
||||||
elif [[ "$#" -gt 1 ]]; then
|
elif [[ "$#" -gt 1 ]]; then
|
||||||
echo "error: too many arguments" >&2
|
usage "too many arguments"
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
elif [[ "$#" -eq 1 ]]; then
|
elif [[ "$#" -eq 1 ]]; then
|
||||||
if [[ "${1}" == "-" ]]; then
|
if [[ "${1}" == "-" ]]; then
|
||||||
swap_context
|
swap_context
|
||||||
@ -233,9 +239,7 @@ main() {
|
|||||||
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||||
usage
|
usage
|
||||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||||
echo "error: unrecognized flag \"${1}\"" >&2
|
usage "unrecognized flag \"${1}\""
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
||||||
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||||
else
|
else
|
||||||
|
42
kubens
42
kubens
@ -26,6 +26,10 @@ SELF_CMD="$0"
|
|||||||
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens"
|
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
|
if [ -n "$1" ] ; then
|
||||||
|
err "$1"
|
||||||
|
fi
|
||||||
|
|
||||||
local SELF
|
local SELF
|
||||||
SELF="kubens"
|
SELF="kubens"
|
||||||
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
||||||
@ -40,13 +44,24 @@ USAGE:
|
|||||||
$SELF -c, --current : show the current namespace
|
$SELF -c, --current : show the current namespace
|
||||||
$SELF -h,--help : show this message
|
$SELF -h,--help : show this message
|
||||||
EOF
|
EOF
|
||||||
|
if [ -n "$1" ] ; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_err() {
|
exit_err() {
|
||||||
echo >&2 "${1}"
|
err "${1}"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err() {
|
||||||
|
warn "error: ${1}"
|
||||||
|
}
|
||||||
|
|
||||||
|
warn() {
|
||||||
|
echo >&2 "${1}"
|
||||||
|
}
|
||||||
|
|
||||||
current_namespace() {
|
current_namespace() {
|
||||||
local cur_ctx
|
local cur_ctx
|
||||||
|
|
||||||
@ -101,7 +116,7 @@ save_namespace() {
|
|||||||
switch_namespace() {
|
switch_namespace() {
|
||||||
local ctx="${1}"
|
local ctx="${1}"
|
||||||
$KUBECTL config set-context "${ctx}" --namespace="${2}"
|
$KUBECTL config set-context "${ctx}" --namespace="${2}"
|
||||||
echo "Active namespace is \"${2}\".">&2
|
warn "Active namespace is \"${2}\"."
|
||||||
}
|
}
|
||||||
|
|
||||||
choose_namespace_interactive() {
|
choose_namespace_interactive() {
|
||||||
@ -109,8 +124,7 @@ choose_namespace_interactive() {
|
|||||||
# "$FZF_DEFAULT_COMMAND failed", so try to see if we can list namespaces
|
# "$FZF_DEFAULT_COMMAND failed", so try to see if we can list namespaces
|
||||||
# locally first
|
# locally first
|
||||||
if [[ -z "$(list_namespaces)" ]]; then
|
if [[ -z "$(list_namespaces)" ]]; then
|
||||||
echo >&2 "error: could not list namespaces (is the cluster accessible?)"
|
exit_err "could not list namespaces (is the cluster accessible?)"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local choice
|
local choice
|
||||||
@ -118,8 +132,7 @@ choose_namespace_interactive() {
|
|||||||
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
||||||
fzf --ansi --no-preview || true)"
|
fzf --ansi --no-preview || true)"
|
||||||
if [[ -z "${choice}" ]]; then
|
if [[ -z "${choice}" ]]; then
|
||||||
echo 2>&1 "error: you did not choose any of the options"
|
exit_err "you did not choose any of the options"
|
||||||
exit 1
|
|
||||||
else
|
else
|
||||||
set_namespace "${choice}"
|
set_namespace "${choice}"
|
||||||
fi
|
fi
|
||||||
@ -137,8 +150,7 @@ set_namespace() {
|
|||||||
save_namespace "${ctx}" "${prev}"
|
save_namespace "${ctx}" "${prev}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "error: no namespace exists with name \"${1}\".">&2
|
exit_err "no namespace exists with name \"${1}\"."
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,8 +188,7 @@ swap_namespace() {
|
|||||||
ctx="$(current_context)" || exit_err "error getting current context"
|
ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
ns="$(read_namespace "${ctx}")"
|
ns="$(read_namespace "${ctx}")"
|
||||||
if [[ -z "${ns}" ]]; then
|
if [[ -z "${ns}" ]]; then
|
||||||
echo "error: No previous namespace found for current context." >&2
|
exit_err "No previous namespace found for current context."
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
set_namespace "${ns}"
|
set_namespace "${ns}"
|
||||||
}
|
}
|
||||||
@ -189,8 +200,7 @@ main() {
|
|||||||
elif hash kubectl.exe 2>/dev/null; then
|
elif hash kubectl.exe 2>/dev/null; then
|
||||||
KUBECTL=kubectl.exe
|
KUBECTL=kubectl.exe
|
||||||
else
|
else
|
||||||
echo >&2 "kubectl is not installed"
|
exit_err "kubectl is not installed"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -208,18 +218,14 @@ main() {
|
|||||||
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
|
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
|
||||||
current_namespace
|
current_namespace
|
||||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||||
echo "error: unrecognized flag \"${1}\"" >&2
|
usage "unrecognized flag \"${1}\""
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
||||||
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||||
else
|
else
|
||||||
set_namespace "${1}"
|
set_namespace "${1}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "error: too many flags" >&2
|
usage "too many flags"
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user