v3.6.1
===================================================================== --- Changes ------------------------------ - Adjusted USAGE (from zsh/lib/utils/io.zsh) to allow dynamic variable insertion in help dialogues by setting USAGE__<help-group> and using the syntax listed - Various quality-of-life changes and and fixes to experimental kubectl plugin --- Bug fixes ---------------------------- - sourcing 'scwrypts.plugin.zsh' no longer sets __SCWRYPT=1 in your current environment
This commit is contained in:
parent
ab567f6950
commit
2034325ac9
181
plugins/kubectl/driver/kubectl.driver.zsh
Normal file
181
plugins/kubectl/driver/kubectl.driver.zsh
Normal file
@ -0,0 +1,181 @@
|
||||
[[ $SCWRYPTS_KUBECTL_DRIVER_READY -eq 1 ]] && return 0
|
||||
|
||||
k() { _SCWRYPTS_KUBECTL_DRIVER kubectl $@; }
|
||||
h() { _SCWRYPTS_KUBECTL_DRIVER helm $@; }
|
||||
|
||||
|
||||
_SCWRYPTS_KUBECTL_DRIVER() {
|
||||
[ ! $SCWRYPTS_ENV ] && {
|
||||
ERROR "must set SCWRYPTS_ENV in order to use '$(echo $CLI | head -c1)'"
|
||||
return 1
|
||||
}
|
||||
|
||||
which REDIS >/dev/null 2>&1 \
|
||||
|| eval "$(scwrypts -n --name meta/get-static-redis-definition --type zsh --group kubectl)"
|
||||
|
||||
local CLI="$1"; shift 1
|
||||
|
||||
local SCWRYPTS_GROUP CUSTOM_COMMANDS=(meta)
|
||||
for SCWRYPTS_GROUP in ${SCWRYPTS_GROUPS[@]}
|
||||
do
|
||||
CUSTOM_COMMANDS+=($(eval echo '$SCWRYPTS_KUBECTL_CUSTOM_COMMANDS__'$SCWRYPTS_GROUP))
|
||||
done
|
||||
|
||||
##########################################
|
||||
|
||||
local USAGE="
|
||||
usage: - [...args...] [...options...] -- [...$CLI options...]
|
||||
|
||||
args: -
|
||||
|
||||
options: -
|
||||
--subsession [0-9] use indicated subsession (use for script clarity instead of positional arg)
|
||||
|
||||
-h, --help display this help dialogue
|
||||
-v, --verbose output debugging information
|
||||
|
||||
description: -
|
||||
"
|
||||
|
||||
local USAGE__usage=$(echo $CLI | head -c1)
|
||||
|
||||
local USAGE__args="$(
|
||||
{
|
||||
echo "\\033[0;32m[0-9]\\033[0m^if the first argument is a number 0-9, uses or creates a subsession (default 0)"
|
||||
echo " ^ "
|
||||
for C in ${CUSTOM_COMMANDS[@]}
|
||||
do
|
||||
echo "\\033[0;32m$C\\033[0m^$(SCWRYPTS_KUBECTL_CUSTOM_COMMAND_DESCRIPTION__$C 2>/dev/null)"
|
||||
done
|
||||
} | column -ts '^'
|
||||
)"
|
||||
|
||||
local USAGE__options="
|
||||
-n, --namespace set the namespace
|
||||
-k, --context set the context
|
||||
"
|
||||
|
||||
local USAGE__description="
|
||||
Provides 'k' (kubectl) and 'h' (helm) shorthands to the respective
|
||||
utility. These functions leverage redis and scwrypts environments to
|
||||
allow quick selection of contexts and namespaces usable across all
|
||||
active shell instances.
|
||||
|
||||
The scwrypts group 'kubectl' has simple selection executables for
|
||||
kubecontext and namespace, but also provides the library to enable
|
||||
enriched, use-case-sensitive setup of kubernetes context.
|
||||
|
||||
All actions are scoped to the current SCWRYPTS_ENV
|
||||
currently : \\033[0;33m$SCWRYPTS_ENV\\033[0m
|
||||
|
||||
"
|
||||
|
||||
##########################################
|
||||
|
||||
local USER_ARGS=()
|
||||
|
||||
local CUSTOM_COMMAND=0
|
||||
local VERBOSE=0
|
||||
local HELP=0
|
||||
local ERRORS=0
|
||||
|
||||
[ ! $SUBSESSION ] && local SUBSESSION=0
|
||||
[[ $1 =~ ^[0-9]$ ]] && SUBSESSION=$1 && shift 1
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
meta )
|
||||
CUSTOM_COMMAND=$1
|
||||
SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__$1 ${@:2}
|
||||
break
|
||||
;;
|
||||
|
||||
-v | --verbose ) VERBOSE=1 ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
-n | --namespace )
|
||||
echo "TODO: set namespace ('$2')" >&2
|
||||
USER_ARGS+=(--namespace $2); shift 1
|
||||
;;
|
||||
|
||||
-k | --context | --kube-context )
|
||||
echo "TODO: set context ('$2')" >&2
|
||||
[[ $CLI =~ ^helm$ ]] && USER_ARGS+=(--kube-context $2)
|
||||
[[ $CLI =~ ^kubectl$ ]] && USER_ARGS+=(--context $2)
|
||||
shift 1
|
||||
;;
|
||||
|
||||
-- ) shift 1; break ;;
|
||||
|
||||
* ) USER_ARGS+=($1) ;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
while [[ $# -gt 0 ]]; do USER_ARGS+=($1); shift 1; done
|
||||
|
||||
|
||||
CHECK_ERRORS --no-fail || return 1
|
||||
|
||||
[[ $HELP -eq 1 ]] && { USAGE; return 0; }
|
||||
|
||||
#####################################################################
|
||||
|
||||
local STRICT=$(_SCWRYPTS_KUBECTL_SETTINGS get strict || echo 1)
|
||||
|
||||
case $CUSTOM_COMMAND in
|
||||
0 )
|
||||
local CLI_ARGS=()
|
||||
|
||||
local CONTEXT=$(k meta get context)
|
||||
local NAMESPACE=$(k meta get namespace)
|
||||
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^helm$ ]] && CLI_ARGS+=(--kube-context $CONTEXT)
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^kubectl$ ]] && CLI_ARGS+=(--context $CONTEXT)
|
||||
|
||||
[[ $STRICT -eq 1 ]] && {
|
||||
[ $CONTEXT ] || ERROR "missing kubectl 'context'"
|
||||
[ $NAMESPACE ] || ERROR "missing kubectl 'namespace'"
|
||||
|
||||
CHECK_ERRORS --no-fail --no-usage || {
|
||||
ERROR "with 'strict' settings enabled, context and namespace must be set!"
|
||||
REMINDER "
|
||||
these values can be set directly with
|
||||
$(echo $CLI | head -c1) meta set (namespace|context)
|
||||
"
|
||||
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
[ $NAMESPACE ] && CLI_ARGS+=(--namespace $NAMESPACE)
|
||||
[[ $VERBOSE -eq 1 ]] && {
|
||||
INFO "
|
||||
context '$CONTEXT'
|
||||
namespace '$NAMESPACE'
|
||||
environment '$SCWRYPTS_ENV'
|
||||
subsession '$SUBSESSION'
|
||||
"
|
||||
STATUS "running $CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}"
|
||||
} || {
|
||||
[[ $(_SCWRYPTS_KUBECTL_SETTINGS get context) =~ ^show$ ]] && {
|
||||
INFO "$SCWRYPTS_ENV.$SUBSESSION : $CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}"
|
||||
}
|
||||
}
|
||||
$CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}
|
||||
;;
|
||||
* ) SCWRYPTS_KUBECTL_CUSTOM_COMMAND__$CUSTOM_COMMAND ${USER_ARGS[@]} ;;
|
||||
esac
|
||||
}
|
||||
|
||||
_SCWRYPTS_KUBECTL_SETTINGS() {
|
||||
# (get setting-name) or (set setting-name setting-value)
|
||||
REDIS h$1 ${SCWRYPTS_ENV}:kubectl:settings ${@:2} | grep .
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
source ${0:a:h}/kubectl.completion.zsh
|
||||
source ${0:a:h}/meta.zsh
|
140
plugins/kubectl/driver/meta.zsh
Normal file
140
plugins/kubectl/driver/meta.zsh
Normal file
@ -0,0 +1,140 @@
|
||||
SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__meta() {
|
||||
USAGE__usage+=" meta"
|
||||
USAGE__args="
|
||||
- get output value of meta variable
|
||||
- set interactively configure value of meta variable
|
||||
- clear clear current subsession variables
|
||||
|
||||
(settings args)
|
||||
- show output context for every command
|
||||
- hide (default) hide output context for every command
|
||||
|
||||
- strict (default) require context *and* namespace to be set
|
||||
- loose do not require context and namespace to be set
|
||||
"
|
||||
USAGE__options=''
|
||||
USAGE__description=$(SCWRYPTS_KUBECTL_CUSTOM_COMMAND_DESCRIPTION__meta)
|
||||
|
||||
META_SUBARGS="
|
||||
- namespace
|
||||
- context
|
||||
"
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
set )
|
||||
USAGE__usage+=" set"
|
||||
USAGE__args="set (namespace|context)"
|
||||
USAGE__description="interactively set a namespace or context for '$SCWRYPTS_ENV'"
|
||||
case $2 in
|
||||
namespace | context ) USER_ARGS+=($1 $2) ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
* ) ERROR "cannot set '$2'" ;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
|
||||
get )
|
||||
USAGE__usage+=" get"
|
||||
USAGE__args="get (namespace|context|all)"
|
||||
USAGE__description="output the current namespace or context for '$SCWRYPTS_ENV'"
|
||||
case $2 in
|
||||
namespace | context | all ) USER_ARGS+=($1 $2) ;;
|
||||
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
* ) ERROR "cannot get '$2'" ;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
|
||||
copy )
|
||||
USAGE__usage+=" copy"
|
||||
USAGE__args+="copy [0-9]"
|
||||
USAGE__description="copy current subsession ($SUBSESSION) to target subsession id"
|
||||
case $2 in
|
||||
[0-9] ) USER_ARGS+=($1 $2) ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
* ) ERROR "target session must be a number [0-9]" ;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
|
||||
clear | show | hide | strict | loose ) USER_ARGS+=($1) ;;
|
||||
|
||||
* ) ERROR "no meta command '$1'"
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
}
|
||||
|
||||
SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta() {
|
||||
case $1 in
|
||||
get )
|
||||
[[ $2 =~ ^all$ ]] && {
|
||||
local CONTEXT=$(REDIS get --prefix current:context | grep . || echo "\\033[1;31mnone set\\033[0m")
|
||||
local NAMESPACE=$(REDIS get --prefix current:namespace | grep . || echo "\\033[1;31mnone set\\033[0m")
|
||||
echo "
|
||||
environment : $SCWRYPTS_ENV
|
||||
context : $CONTEXT
|
||||
namespace : $NAMESPACE
|
||||
|
||||
CLI settings
|
||||
command context : $(_SCWRYPTS_KUBECTL_SETTINGS get context)
|
||||
strict mode : $([[ $STRICT -eq 1 ]] && echo "on" || echo "\\033[1;31moff\\033[0m")
|
||||
" | sed 's/^ \+//' >&2
|
||||
return 0
|
||||
}
|
||||
|
||||
REDIS get --prefix current:$2
|
||||
;;
|
||||
|
||||
set )
|
||||
scwrypts -n --name set-$2 --type zsh --group kubectl -- --subsession $SUBSESSION >/dev/null \
|
||||
&& SUCCESS "$2 set"
|
||||
;;
|
||||
|
||||
copy )
|
||||
: \
|
||||
&& STATUS "copying $1 to $2" \
|
||||
&& scwrypts -n --name set-context --type zsh --group kubectl -- --subsession $2 $(k meta get context | grep . || echo 'reset') \
|
||||
&& scwrypts -n --name set-namespace --type zsh --group kubectl -- --subsession $2 $(k meta get namespace | grep . || echo 'reset') \
|
||||
&& SUCCESS "subsession $1 copied to $2" \
|
||||
;
|
||||
;;
|
||||
|
||||
clear )
|
||||
scwrypts -n --name set-context --type zsh --group kubectl -- --subsession $SUBSESSION reset >/dev/null \
|
||||
&& SUCCESS "subsession $SUBSESSION reset to default"
|
||||
;;
|
||||
|
||||
show )
|
||||
_SCWRYPTS_KUBECTL_SETTINGS set context show >/dev/null \
|
||||
&& SUCCESS "now showing full command context"
|
||||
;;
|
||||
|
||||
hide )
|
||||
_SCWRYPTS_KUBECTL_SETTINGS set context hide >/dev/null \
|
||||
&& SUCCESS "now hiding command context"
|
||||
;;
|
||||
|
||||
loose )
|
||||
_SCWRYPTS_KUBECTL_SETTINGS set strict 0 >/dev/null \
|
||||
&& WARNING "now running in 'loose' mode"
|
||||
;;
|
||||
|
||||
strict )
|
||||
_SCWRYPTS_KUBECTL_SETTINGS set strict 1 >/dev/null \
|
||||
&& SUCCESS "now running in 'strict' mode"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
SCWRYPTS_KUBECTL_CUSTOM_COMMAND_DESCRIPTION__meta() {
|
||||
[ $CLI ] || CLI='kubectl'
|
||||
echo "operations for $CLI session variables and other CLI settings"
|
||||
}
|
@ -1,173 +0,0 @@
|
||||
[[ $SCWRYPTS_KUBECTL_DRIVER_READY -eq 1 ]] && return 0
|
||||
|
||||
k() { _SCWRYPTS_KUBECTL_DRIVER kubectl $@; }
|
||||
h() { _SCWRYPTS_KUBECTL_DRIVER helm $@; }
|
||||
|
||||
_SCWRYPTS_KUBECTL_DRIVER() {
|
||||
[ ! $SCWRYPTS_ENV ] && {
|
||||
ERROR "must set SCWRYPTS_ENV in order to use '$(echo $CLI | head -c1)'"
|
||||
return 1
|
||||
}
|
||||
|
||||
which REDIS >/dev/null 2>&1 \
|
||||
|| eval "$(scwrypts -n --name meta/get-static-redis-definition --type zsh --group kubectl)"
|
||||
|
||||
local CLI="$1"; shift 1
|
||||
|
||||
local SCWRYPTS_GROUP CUSTOM_COMMANDS=(meta)
|
||||
for SCWRYPTS_GROUP in ${SCWRYPTS_GROUPS[@]}
|
||||
do
|
||||
CUSTOM_COMMANDS+=($(eval echo '$SCWRYPTS_KUBECTL_CUSTOM_COMMANDS__'$SCWRYPTS_GROUP))
|
||||
done
|
||||
|
||||
##########################################
|
||||
|
||||
local USAGE_ARGS="$(for C in ${CUSTOM_COMMANDS[@]}; do echo " - $C"; done)"
|
||||
local USAGE_OPTIONS="
|
||||
-n, --namespace set the namespace for commands in '$SCWRYPTS_ENV'
|
||||
-k, --context set the context for commands in '$SCWRYPTS_ENV'
|
||||
"
|
||||
local DESCRIPTION="
|
||||
Provides 'k' (kubectl) and 'h' (helm) shorthands to the
|
||||
respective utility. These functions leverage redis and scwrypts
|
||||
environments to allow quick selection of contexts and namespaces
|
||||
usable across all active shell instances.
|
||||
|
||||
The scwrypts group 'kubectl' has simple selection executables
|
||||
for kubecontext and namespace, but also provides the library to
|
||||
enable enriched, use-case-sensitive setup of kubernetes context.
|
||||
"
|
||||
|
||||
local USAGE="
|
||||
usage: $(echo $CLI | head -c1) [...args...] [...options...] -- [...$CLI options...]
|
||||
|
||||
args: -
|
||||
|
||||
options: -
|
||||
-h, --help display this help dialogue
|
||||
-v, --verbose output debugging information
|
||||
|
||||
DESCRIPTION
|
||||
"
|
||||
|
||||
##########################################
|
||||
|
||||
local USER_ARGS=()
|
||||
|
||||
local CUSTOM_COMMAND=0
|
||||
PARAMETER_OVERRIDES+=
|
||||
local VERBOSE=0
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-v | --verbose ) VERBOSE=1 ;;
|
||||
-n | --namespace )
|
||||
echo "TODO: set namespace ('$2')" >&2
|
||||
USER_ARGS+=(--namespace $2); shift 1
|
||||
;;
|
||||
|
||||
-k | --context | --kube-context )
|
||||
echo "TODO: set context ('$2')" >&2
|
||||
[[ $CLI =~ ^helm$ ]] && USER_ARGS+=(--kube-context $2)
|
||||
[[ $CLI =~ ^kubectl$ ]] && USER_ARGS+=(--context $2)
|
||||
shift 1
|
||||
;;
|
||||
|
||||
meta )
|
||||
CUSTOM_COMMAND=meta
|
||||
USAGE_ARGS=" - get\n - set"
|
||||
USAGE_OPTIONS=''
|
||||
DESCRIPTION="perform meta-operations on $(echo $CLI | head -c1) for '$SCWRYPTS_ENV'"
|
||||
|
||||
case $2 in
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
set )
|
||||
USAGE_ARGS=" set (namespace|context)"
|
||||
DESCRIPTION="interactively set a namespace or context for '$SCWRYPTS_ENV'"
|
||||
case $3 in
|
||||
namespace | context ) USER_ARGS+=($2 $3) ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
* ) ERROR "cannot set '$3'" >&2 ;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
|
||||
get )
|
||||
USAGE_ARGS=" get (namespace|context)"
|
||||
DESCRIPTION="output the current namespace or context for '$SCWRYPTS_ENV'"
|
||||
case $3 in
|
||||
namespace | context ) USER_ARGS+=($2 $3) ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
|
||||
* ) ERROR "cannot get '$3'" >&2 ;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
;;
|
||||
|
||||
-h | --help ) HELP=1 ;;
|
||||
-- ) shift 1; break ;;
|
||||
* ) USER_ARGS+=($1) ;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
while [[ $# -gt 0 ]]; do USER_ARGS+=($1); shift 1; done
|
||||
|
||||
|
||||
CHECK_ERRORS --no-fail 2>&1 | sed 's/scwrypts -- //' >&2 || return 1
|
||||
|
||||
[[ $HELP -eq 1 ]] && {
|
||||
[[ ! $CUSTOM_COMMAND =~ ^0$ ]] \
|
||||
&& USAGE=$(echo $USAGE | sed "s/[[]\\.\\.\\.args\\.\\.\\.[]]/$CUSTOM_COMMAND &/")
|
||||
|
||||
USAGE=$(echo $USAGE | perl -pe "
|
||||
s/args: -/args:\n$USAGE_ARGS/;
|
||||
s^options: -^options:$USAGE_OPTIONS^;
|
||||
s/DESCRIPTION/$DESCRIPTION/;
|
||||
")
|
||||
|
||||
USAGE 2>&1 | sed 's/scwrypts -- //' >&2
|
||||
return 0
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
case $CUSTOM_COMMAND in
|
||||
0 )
|
||||
local CLI_ARGS=()
|
||||
|
||||
local CONTEXT=$(k meta get context)
|
||||
local NAMESPACE=$(k meta get namespace)
|
||||
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^helm$ ]] && CLI_ARGS+=(--kube-context $CONTEXT)
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^kubectl$ ]] && CLI_ARGS+=(--context $CONTEXT)
|
||||
|
||||
[ $NAMESPACE ] && CLI_ARGS+=(--namespace $NAMESPACE)
|
||||
[[ $VERBOSE -eq 1 ]] && {
|
||||
INFO "
|
||||
using context '$CONTEXT'
|
||||
using namespace '$NAMESPACE'
|
||||
"
|
||||
STATUS "running $CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}"
|
||||
}
|
||||
$CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}
|
||||
;;
|
||||
* ) eval 'SCWRYPTS_KUBECTL_CUSTOM_COMMAND__'$CUSTOM_COMMAND ${USER_ARGS[@]} ;;
|
||||
esac
|
||||
}
|
||||
|
||||
SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta() {
|
||||
case $1 in
|
||||
get ) REDIS get --prefix current:$2; return 0 ;;
|
||||
set ) scwrypts --name set-$2 --type zsh --group kubectl ;;
|
||||
esac
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
source ${0:a:h}/kubectl.driver.completion.zsh
|
@ -8,4 +8,4 @@ SCWRYPTS_STATIC_CONFIG__kubectl+=(
|
||||
"$SCWRYPTS_ROOT__kubectl/.config/static/redis.zsh"
|
||||
)
|
||||
|
||||
source "$SCWRYPTS_ROOT__kubectl/kubectl.driver.zsh"
|
||||
source "$SCWRYPTS_ROOT__kubectl/driver/kubectl.driver.zsh"
|
||||
|
@ -30,17 +30,17 @@ KUBECTL__SET_CONTEXT() {
|
||||
local CONTEXT=$1
|
||||
[ ! $CONTEXT ] && return 1
|
||||
|
||||
[[ $CONTEXT =~ default ]] && {
|
||||
[[ $CONTEXT =~ reset ]] && {
|
||||
: \
|
||||
&& REDIS del --prefix "current:context" \
|
||||
&& KUBECTL__SET_NAMESPACE default \
|
||||
&& KUBECTL__SET_NAMESPACE reset \
|
||||
;
|
||||
return $?
|
||||
}
|
||||
|
||||
: \
|
||||
&& REDIS set --prefix "current:context" "$CONTEXT" \
|
||||
&& KUBECTL__SET_NAMESPACE default \
|
||||
&& KUBECTL__SET_NAMESPACE reset \
|
||||
;
|
||||
}
|
||||
|
||||
@ -49,8 +49,17 @@ KUBECTL__SELECT_CONTEXT() {
|
||||
}
|
||||
|
||||
KUBECTL__LIST_CONTEXTS() {
|
||||
echo default
|
||||
KUBECTL config get-contexts -o name | sort
|
||||
echo reset
|
||||
local ALL_CONTEXTS=$(KUBECTL config get-contexts -o name | sort)
|
||||
|
||||
echo $ALL_CONTEXTS | grep -v '^arn:aws:eks'
|
||||
|
||||
[[ $AWS_ACCOUNT ]] && {
|
||||
echo $ALL_CONTEXTS | grep "^arn:aws:eks:.*:$AWS_ACCOUNT"
|
||||
true
|
||||
} || {
|
||||
echo $ALL_CONTEXTS | grep '^arn:aws:eks'
|
||||
}
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
@ -61,7 +70,7 @@ KUBECTL__SET_NAMESPACE() {
|
||||
local NAMESPACE=$1
|
||||
[ ! $NAMESPACE ] && return 1
|
||||
|
||||
[[ $NAMESPACE =~ default ]] && {
|
||||
[[ $NAMESPACE =~ reset ]] && {
|
||||
REDIS del --prefix "current:namespace"
|
||||
return $?
|
||||
}
|
||||
@ -74,6 +83,7 @@ KUBECTL__SELECT_NAMESPACE() {
|
||||
}
|
||||
|
||||
KUBECTL__LIST_NAMESPACES() {
|
||||
echo reset
|
||||
echo default
|
||||
KUBECTL get namespaces -o name | sed 's/^namespace\///' | sort
|
||||
}
|
||||
|
@ -19,22 +19,29 @@ REDIS() {
|
||||
usage: [...options...]
|
||||
|
||||
options:
|
||||
--subsession [0-9] use a particular subsession
|
||||
|
||||
-p, --prefix apply dynamic prefix to the next command line argument
|
||||
|
||||
--get-prefix output key prefix for current session
|
||||
--get-prefix output key prefix for current session+subsession
|
||||
--get-static-definition output the static ZSH function definition for REDIS
|
||||
|
||||
other arguments are passed through to redis-cli
|
||||
additional arguments and options are passed through to 'redis-cli'
|
||||
"
|
||||
local REDIS_ARGS=() USER_ARGS=()
|
||||
local REDIS_PREFIX=$(eval echo '$SCWRYPTS_KUBECTL_REDIS_KEY_PREFIX__'$SCWRYPTS_KUBECTL_REDIS)
|
||||
|
||||
local ECHO_STATIC_DEFINITION=0
|
||||
local REDIS_ARGS=() USER_ARGS=()
|
||||
|
||||
[ $SUBSESSION ] || local SUBSESSION=0
|
||||
|
||||
local REDIS_PREFIX=$(eval echo '$SCWRYPTS_KUBECTL_REDIS_KEY_PREFIX__'$SCWRYPTS_KUBECTL_REDIS)
|
||||
[ $REDIS_PREFIX ] && REDIS_PREFIX+=':'
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-p | --prefix ) USER_ARGS+=("${SCWRYPTS_ENV}:${REDIS_PREFIX}$2"); shift 1 ;;
|
||||
-p | --prefix ) USER_ARGS+=("${REDIS_PREFIX}${SCWRYPTS_ENV}:${SUBSESSION}:$2"); shift 1 ;;
|
||||
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
--get-prefix ) echo $REDIS_PREFIX; return 0 ;;
|
||||
--get-static-definition ) ECHO_STATIC_DEFINITION=1 ;;
|
||||
@ -57,10 +64,11 @@ REDIS() {
|
||||
[[ $ECHO_STATIC_DEFINITION -eq 1 ]] && {
|
||||
echo "REDIS() {\
|
||||
local USER_ARGS=(); \
|
||||
[ ! \$SUBSESSION ] && local SUBSESSION=0 ;\
|
||||
while [[ \$# -gt 0 ]]; \
|
||||
do \
|
||||
case \$1 in
|
||||
-p | --prefix ) USER_ARGS+=(\"\${SCWRYPTS_ENV}:${REDIS_PREFIX}\$2\"); shift 1 ;; \
|
||||
-p | --prefix ) USER_ARGS+=(\"${REDIS_PREFIX}\${SCWRYPTS_ENV}:\${SUBSESSION}:\$2\"); shift 1 ;; \
|
||||
* ) USER_ARGS+=(\$1) ;; \
|
||||
esac; \
|
||||
shift 1; \
|
||||
@ -73,7 +81,6 @@ REDIS() {
|
||||
redis-cli ${REDIS_ARGS[@]} ${USER_ARGS[@]}
|
||||
}
|
||||
|
||||
|
||||
REDIS ping | grep -qi pong || {
|
||||
RPID=$(docker ps -a | grep scwrypts-kubectl-redis | awk '{print $1;}')
|
||||
[ $RPID ] && STATUS 'refreshing redis instance' && docker rm -f $RPID
|
||||
|
@ -10,13 +10,31 @@ CHECK_ENVIRONMENT
|
||||
|
||||
MAIN() {
|
||||
local USAGE="
|
||||
usage: [context]
|
||||
usage: [context] [...options...]
|
||||
|
||||
args:
|
||||
context the full name of the kubeconfig context to set
|
||||
context (optional) the full name of the kubeconfig context to set
|
||||
|
||||
options:
|
||||
--subsession REDIS subsession (default 0)
|
||||
"
|
||||
local CONTEXT="$1"
|
||||
[ $CONTEXT ] || $(KUBECTL__SELECT_CONTEXT)
|
||||
local CONTEXT
|
||||
local SUBSESSION=0
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
* )
|
||||
[ $CONTEXT ] && ERROR "unexpected argument '$2'"
|
||||
CONTEXT=$1
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
[ $CONTEXT ] || CONTEXT=$(KUBECTL__SELECT_CONTEXT)
|
||||
[ $CONTEXT ] || ERROR 'must provide or select a valid kube context'
|
||||
|
||||
CHECK_ERRORS
|
||||
|
@ -10,13 +10,31 @@ CHECK_ENVIRONMENT
|
||||
|
||||
MAIN() {
|
||||
local USAGE="
|
||||
usage: [namespace]
|
||||
usage: [namespace] [...options...]
|
||||
|
||||
args:
|
||||
namespace the full name of the namespace to set
|
||||
namespace (optional) the full name of the namespace context to set
|
||||
|
||||
options:
|
||||
--subsession REDIS subsession (default 0)
|
||||
"
|
||||
local NAMESPACE="$1"
|
||||
[ $NAMESPACE ] || $(KUBECTL__SELECT_NAMESPACE)
|
||||
local NAMESPACE
|
||||
local SUBSESSION=0
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
* )
|
||||
[ $NAMESPACE ] && ERROR "unexpected argument '$2'"
|
||||
NAMESPACE=$1
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
[ $NAMESPACE ] || NAMESPACE=$(KUBECTL__SELECT_NAMESPACE)
|
||||
[ $NAMESPACE ] || ERROR 'must provide or select a valid namespace'
|
||||
|
||||
CHECK_ERRORS
|
||||
|
@ -45,4 +45,5 @@ source "$SCWRYPTS_ROOT/zsh/lib/config.group.zsh" \
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
__SCWRYPT=1 # arbitrary; indicates currently inside a scwrypt
|
||||
[ $NO_EXPORT_CONFIG ] || __SCWRYPT=1 # arbitrary; indicates currently inside a scwrypt
|
||||
true
|
||||
|
@ -55,37 +55,81 @@ ABORT() { FAIL 69 'user abort'; }
|
||||
|
||||
CHECK_ERRORS() {
|
||||
local FAIL_OUT=1
|
||||
local DISPLAY_USAGE=1
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-n | --no-fail ) FAIL_OUT=0 ;;
|
||||
--no-fail ) FAIL_OUT=0 ;;
|
||||
--no-usage ) DISPLAY_USAGE=0 ;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
[ ! $ERRORS ] && ERRORS=0
|
||||
[[ $ERRORS -ne 0 ]] && USAGE
|
||||
[[ $ERRORS -eq 0 ]] || {
|
||||
[[ $FAIL_OUT -eq 1 ]] \
|
||||
&& exit $ERRORS \
|
||||
|| return $ERRORS
|
||||
}
|
||||
[[ $ERRORS -eq 0 ]] && return 0
|
||||
|
||||
[[ $DISPLAY_USAGE -eq 1 ]] && USAGE
|
||||
|
||||
[[ $FAIL_OUT -eq 1 ]] && exit $ERRORS
|
||||
|
||||
return $ERRORS
|
||||
}
|
||||
|
||||
USAGE() {
|
||||
USAGE() { # formatter for USAGE variable
|
||||
[ ! $USAGE ] && return 0
|
||||
USAGE=$(echo $USAGE | sed "s/^\t\+//; s/\s\+$//")
|
||||
local USAGE_LINE=$(echo $USAGE | grep -i '^[ ]*usage *:' | sed 's/^[ ]*//')
|
||||
|
||||
local USAGE_LINE=$(\
|
||||
echo $USAGE \
|
||||
| grep -i '^ *usage *:' \
|
||||
| sed "s;^[^:]*:;& scwrypts $SCWRYPT_NAME --;" \
|
||||
| sed 's/ \{2,\}/ /g; s/scwrypts -- scwrypts/scwrypts/' \
|
||||
)
|
||||
local THE_REST=$(echo $USAGE | grep -vi '^ *usage *:' | sed 'N;/^\n$/D;P;D;')
|
||||
[ $USAGE__usage ] && echo $USAGE_LINE | grep -q 'usage: -' \
|
||||
&& USAGE_LINE=$(echo $USAGE_LINE | sed "s/usage: -/usage: $USAGE__usage/")
|
||||
|
||||
{ echo; printf "$__DARK_BLUE $USAGE_LINE$__COLOR_RESET\n"; echo $THE_REST; echo } >&2
|
||||
[ $__SCWRYPT ] \
|
||||
&& USAGE_LINE=$(
|
||||
echo $USAGE_LINE \
|
||||
| sed "s;^[^:]*:;& scwrypts $SCWRYPT_NAME --;" \
|
||||
| sed 's/ \{2,\}/ /g; s/scwrypts -- scwrypts/scwrypts/' \
|
||||
)
|
||||
|
||||
local THE_REST=$(echo $USAGE | grep -vi '^[ ]*usage *:' )
|
||||
|
||||
local DYNAMIC_USAGE_ELEMENT
|
||||
#
|
||||
# create dynamic usage elements (like 'args') by defining USAGE__<element>
|
||||
# then using the syntax "<element>: -" in your USAGE variable
|
||||
#
|
||||
# e.g.
|
||||
#
|
||||
# USAGE__args="
|
||||
# subcommand arg 1 arg 1 description
|
||||
# subcommand arg 2 some other description
|
||||
# "
|
||||
#
|
||||
# USAGE="
|
||||
# usage: some-command [...args...]
|
||||
#
|
||||
# args: -
|
||||
# -h, --help some arguments are applicable everywhere
|
||||
# "
|
||||
#
|
||||
for DYNAMIC_USAGE_ELEMENT in $(echo $THE_REST | sed -n 's/^\([^:]*\): -$/\1/p')
|
||||
do
|
||||
DYNAMIC_USAGE_ELEMENT_TEXT=$(eval echo '$USAGE__'$DYNAMIC_USAGE_ELEMENT)
|
||||
|
||||
[[ ! $DYNAMIC_USAGE_ELEMENT =~ ^description$ ]] \
|
||||
&& DYNAMIC_USAGE_ELEMENT_TEXT=$(echo $DYNAMIC_USAGE_ELEMENT_TEXT | sed 's/[^ ]/ &/')
|
||||
|
||||
THE_REST=$(echo $THE_REST | perl -pe "s/$DYNAMIC_USAGE_ELEMENT: -/$DYNAMIC_USAGE_ELEMENT:\n$DYNAMIC_USAGE_ELEMENT_TEXT\n\n/")
|
||||
done
|
||||
|
||||
# allow for dynamic 'description: -' but delete the 'description:' header line
|
||||
THE_REST=$(echo $THE_REST | sed '/^[ ]*description:$/d')
|
||||
|
||||
echo "$__DARK_BLUE$USAGE_LINE$__COLOR_RESET\n\n$THE_REST" \
|
||||
| sed "s/^\t\+//; s/\s\+$//; s/^\\s*$//;" \
|
||||
| sed '/./,$!d; :a; /^\n*$/{$d;N;ba;};' \
|
||||
| perl -p0e 's/\n{2,}/\n\n/g' \
|
||||
| perl -p0e 's/:\n{2,}/:\n/g' \
|
||||
>&2
|
||||
}
|
||||
|
||||
INPUT() {
|
||||
|
Loading…
Reference in New Issue
Block a user