From ae97a832b2594046e989b80c483fe964506f9979 Mon Sep 17 00:00:00 2001 From: Anastas Dancha Date: Tue, 19 Jan 2021 00:03:25 +0300 Subject: [PATCH] adding bash completion Signed-off-by: Anastas Dancha --- Dockerfile | 1 + k8s-vault-completion.bash | 70 +++++++++++++++++++++++++++++++++++++++ src/cli.cr | 2 +- src/k8s-vault.cr | 4 +++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 k8s-vault-completion.bash diff --git a/Dockerfile b/Dockerfile index 21b266c..dae4909 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /tmp COPY VERSION . COPY shard.yml . COPY k8s-vault_example.yaml . +COPY k8s-vault-completion.bash . COPY ./src ./src RUN \ shards install && \ diff --git a/k8s-vault-completion.bash b/k8s-vault-completion.bash new file mode 100644 index 0000000..48cfbcf --- /dev/null +++ b/k8s-vault-completion.bash @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +KUBECONFIG=${KUBECONFIG:-~/.kube/config} +if [[ -z "${K8SVAULT_CONFIG_DIR:-}" ]]; then + K8SVAULT_CONFIG_DIR="${HOME}/.kube" +fi +if [[ -z "${K8SVAULT_CONFIG:-}" ]]; then + K8SVAULT_CONFIG="${K8SVAULT_CONFIG_DIR}/k8s-vault.yaml" +fi +COMPREPLY=() +DEPS=( jq oq ) +check_dep() { + if ! which $1 2>&1 >/dev/null; then + echo >&2 "ERROR: dependency missing - \"${1}\"" + return 1 + fi +} +for dep in ${DEPS[*]}; do + check_dep $dep +done +if [ ! -r "${K8SVAULT_CONFIG}" ]; then + echo >&2 "ERROR: unable to read K8SVAULT_CONFIG at \"${K8SVAULT_CONFIG}\"" + return 1 +fi +_k8svault_get_contexts() +{ + local contexts + # if contexts=$(yq r -j ${KUBECONFIG} contexts[*].name); then + if contexts=$(oq -i yaml '.clusters| .[] | select(.enabled != false) | .name' "${K8SVAULT_CONFIG}"); then + COMPREPLY+=( $(compgen -W "${contexts[*]}" -- "${_word_last}") ) + fi +} +_k8svault_completion() +{ + local _word_index=$[${COMP_CWORD}-1] + local _word="${COMP_WORDS[$_word_index]}" + local _word_last="${COMP_WORDS[-1]}" + + case $_word in + k8s-vault) + COMPREPLY+=( $(compgen -W "--debug exec completion" -- "${_word_last}") ) + return + ;; + --debug) + COMPREPLY=( $(compgen -W "exec completion" -- "${_word_last}") ) + return + ;; + completion) + return + ;; + exec) + _k8svault_get_contexts + return + ;; + -*) + return + ;; + \>*) + return + ;; + *) + COMPREPLY=( -s -- ) + return + ;; + esac +} +if [[ $(type -t compopt) = "builtin" ]]; then + complete -o default -F _k8svault_completion k8s-vault +else + complete -o default -o nospace -F _k8svault_completion k8s-vault +fi diff --git a/src/cli.cr b/src/cli.cr index aaa0459..9e3ffd2 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -27,7 +27,7 @@ while cli_opts.size > 0 K8sVault.example_config exit 0 when "completion" - K8sVault::Log.info "not implemented yet" + K8sVault.completion exit 0 when "exec" cli_opts.shift diff --git a/src/k8s-vault.cr b/src/k8s-vault.cr index b17334c..e8de699 100644 --- a/src/k8s-vault.cr +++ b/src/k8s-vault.cr @@ -66,6 +66,10 @@ module K8sVault puts {{ read_file("#{__DIR__}/../k8s-vault_example.yaml") }} end + def self.completion + puts {{ read_file("#{__DIR__}/../k8s-vault-completion.bash") }} + end + def self.usage puts <<-USAGE k8s-vault makes it easy to reach K8s API via jumphost, using SSH port forwarding.