diff options
author | Christian Segundo | 2023-05-21 15:15:49 +0200 |
---|---|---|
committer | Christian Segundo | 2023-05-21 21:34:21 +0200 |
commit | 0d927a66dd793e15cd24294ab9f0a7c8b9953e9c (patch) | |
tree | 7ce1ecb067d0b51eb237ffed2acd9b2e939d32a8 | |
parent | ada36c86a256275fab8400f155ded2815db3836b (diff) | |
download | kontext-0d927a66dd793e15cd24294ab9f0a7c8b9953e9c.tar.gz |
Add kontext1.0.0
-rw-r--r-- | .gitignore | 11 | ||||
-rw-r--r-- | Makefile.am | 8 | ||||
-rw-r--r-- | completions/zsh/_kontext | 17 | ||||
-rw-r--r-- | configure.ac | 40 | ||||
-rw-r--r-- | kontext.1 | 54 | ||||
-rwxr-xr-x | kontext.in | 51 |
6 files changed, 181 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61f6544 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +configure~ +depcomp +install-sh +Makefile +Makefile.in +config.log +config.status +aclocal.m4 +missing +autom4te* +kontext diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..33502b7 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,8 @@ +bin_SCRIPTS = kontext +man1_MANS = kontext.1 +CLEANFILES = $(bin_SCRIPTS) + +if ENABLE_ZSH_COMPLETION +zshcompletiondir = $(ZSH_COMPLETION_DIR) +dist_zshcompletion_DATA = completions/zsh/_kontext +endif diff --git a/completions/zsh/_kontext b/completions/zsh/_kontext new file mode 100644 index 0000000..1f2ab78 --- /dev/null +++ b/completions/zsh/_kontext @@ -0,0 +1,17 @@ +#compdef kontext + +__kubectl_context_list() { + local -a ctx_list_disp ctx_list + local expl + IFS=$'\n' ctx_list=($(kubectl config get-contexts -o name)) + _wanted ctx expl 'kubernetes context' compadd "$@" "$ctx_list_disp[@]" - "${(@)ctx_list}" +} + +__kontext() { + _arguments \ + ':ctx arg:__kubectl_context_list' \ + - set1 '-h[help]' \ + - set2 '-n[namespace]' +} + +compdef __kontext kontext diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..d217716 --- /dev/null +++ b/configure.ac @@ -0,0 +1,40 @@ +AC_INIT([kontext], [1.0.0], [christian@segundo.io]) +AM_INIT_AUTOMAKE([foreign no-dependencies]) +AC_CONFIG_FILES([Makefile]) + +AC_SUBST([CC]) + +AC_PATH_PROG([ENV_ABSOLUTE_PATH], [env]) +AS_IF([test x"$ENV_ABSOLUTE_PATH" = x""], [AC_MSG_ERROR([env is not available])]) +AC_SUBST([ENV_ABSOLUTE_PATH]) + +AC_CHECK_PROG(BASH_CHECK,bash,yes) +AC_CHECK_PROG(KUBECTL_CHECK,kubectl,yes) +AC_CHECK_PROG(MKTEMP_CHECK,mktemp,yes) +AS_IF([test x"$BASH_CHECK" != x"yes"], [AC_MSG_ERROR([bash is not available])]) +AS_IF([test x"$KUBECTL_CHECK" != x"yes"], [AC_MSG_ERROR([kubectl is not available])]) +AS_IF([test x"$MKTEMP_CHECK" != x"yes"], [AC_MSG_ERROR([mktemp is not available])]) + +AC_CACHE_CHECK([for GNU getopt], [ac_cv_path_gnu_getopt], + [AC_PATH_PROGS_FEATURE_CHECK([gnu_getopt], [getopt], + [[output=`$ac_path_gnu_getopt -V | grep util-linux` + test -n "$output" \ + && ac_cv_path_gnu_getopt=$ac_path_gnu_getopt]], + [AC_MSG_ERROR([could not find getopt from util-linux])])]) +AC_SUBST([GNU_GETOPT], [$ac_cv_path_gnu_getopt]) + +AC_ARG_WITH([zsh-completion-dir], + AS_HELP_STRING([--with-zsh-completion-dir[=PATH]], + [Install the zsh auto-completion script in this directory. @<:@default=yes@:>@]), + [], + [with_zsh_completion_dir=yes]) +AS_IF([test "x$with_zsh_completion_dir" = "xyes"], + [ZSH_COMPLETION_DIR="$datadir/zsh/site-functions"], + [ZSH_COMPLETION_DIR="$with_zsh_completion_dir"]) + +AC_SUBST([ZSH_COMPLETION_DIR]) +AM_CONDITIONAL([ENABLE_ZSH_COMPLETION], [test "x$with_zsh_completion_dir" != "xno"]) + +AC_CONFIG_FILES([kontext], [chmod +x kontext]) + +AC_OUTPUT diff --git a/kontext.1 b/kontext.1 new file mode 100644 index 0000000..2b1bc40 --- /dev/null +++ b/kontext.1 @@ -0,0 +1,54 @@ +.Dd May 21, 2023 +.Dt KONTEXT 1 +.Os +. +.Sh NAME +.Nm kontext +.Nd per shell Kubernetes contexts +. +.Sh SYNOPSIS +.Nm +.Op Fl h +.Op Fl n Ar namespace +.Ar context +. +.Sh DESCRIPTION + +The +.Nm +program sets per shell Kubernetes contexts +to allow power users to work on multiple +clusters at the same time. +.Pp +Note that +.Nm +is distributed as a shell fragment +that must be sourced. In order to +use it, add the following alias +to your shell: +.Bl -tag -width Ds +.It kontext="source kontext" +.El +. +.Pp +The arguments are as follows: +.Bl -tag -width Ds +.It Fl n | -namespace Ar namespace +Switch to +.Ar namespace . The default +namespace is 'default'. +.It Fl h | -help +Shows a short help message. +.It Ar context +The context name to use. +.El +. +.Sh EXAMPLES +.Bd -literal -offset indent +$ kontext -n kube-system foo +$ kontext foo +$ kontext -n kube-system +.Ed +. +.Sh AUTHORS +.An Christian Segundo Aq Mt christian@segundo.io diff --git a/kontext.in b/kontext.in new file mode 100755 index 0000000..a16798a --- /dev/null +++ b/kontext.in @@ -0,0 +1,51 @@ +#!@ENV_ABSOLUTE_PATH@ bash + +[[ "${BASH_SOURCE[0]}" != "${0}" ]] || { >&2 cat <<EOF +Warning: kontext is not being sourced, add this alias to your shell: + alias kontext="source kontext" +EOF +exit 1; } + +kontext_func () { + local TEMP + TEMP=$(@GNU_GETOPT@ -o hn: --long help,namespace: -n 'kontext' -- "$@") + eval set -- "$TEMP" + + local HELP=false + local NAMESPACE="" + local CTX="" + + while true; do + case "$1" in + -h | --help ) HELP=true; shift ;; + -n | --namespace ) NAMESPACE="$2"; shift 2 ;; + -- ) CTX="$2"; shift; break ;; + * ) break ;; + esac + done + + if [ -z "$CTX" ] && [ -n "$NAMESPACE" ]; then + CTX=$(kubectl config current-context) + fi + + if [ -z "$CTX" ] || [ "$HELP" = true ]; then + cat <<-EOF +Usage: + kontext [-h|--help]" + Show this text. + kontext [-n|--namespace <namespace>] [<context>]" + Switch to the given namespace and context. + +More information may be found in the kontext(1) man page. +EOF + return 1 + else + TMP_KUBECONFIG=$(mktemp -t kubeconfig-XXXXXX) + kubectl config view --raw > "$TMP_KUBECONFIG" + export KUBECONFIG="$TMP_KUBECONFIG" + kubectl config use-context "$CTX" && \ + kubectl config set-context --current --namespace="$NAMESPACE" + fi +} + +kontext_func "$@" |