Skip to content

Commit

Permalink
adding bash completion
Browse files Browse the repository at this point in the history
Signed-off-by: Anastas Dancha <[email protected]>
  • Loading branch information
anapsix committed Jan 18, 2021
1 parent cc66d09 commit ae97a83
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
70 changes: 70 additions & 0 deletions k8s-vault-completion.bash
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/k8s-vault.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ae97a83

Please sign in to comment.