From e84b4be65c4f16aa2592d76d19c69475c5f79487 Mon Sep 17 00:00:00 2001 From: Anastas Dancha Date: Tue, 19 Jan 2021 18:51:47 +0300 Subject: [PATCH] remove external deps for bash completion Signed-off-by: Anastas Dancha --- VERSION | 2 +- k8s-vault-completion.bash | 15 +++++---------- shard.yml | 2 +- src/cli.cr | 3 +++ src/k8s-vault.cr | 1 - src/k8s-vault/helpers.cr | 19 +++++++++++++++++-- src/k8s-vault/log.cr | 2 ++ 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/VERSION b/VERSION index 0c62199..ee1372d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.1 +0.2.2 diff --git a/k8s-vault-completion.bash b/k8s-vault-completion.bash index 48cfbcf..058094d 100644 --- a/k8s-vault-completion.bash +++ b/k8s-vault-completion.bash @@ -1,5 +1,4 @@ #!/usr/bin/env bash -KUBECONFIG=${KUBECONFIG:-~/.kube/config} if [[ -z "${K8SVAULT_CONFIG_DIR:-}" ]]; then K8SVAULT_CONFIG_DIR="${HOME}/.kube" fi @@ -7,13 +6,10 @@ 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 -} +if ! command -v k8s-vault >/dev/null; then + echo >&2 "ERROR: k8s-vault is missing in PATH" + return 1 +fi for dep in ${DEPS[*]}; do check_dep $dep done @@ -24,8 +20,7 @@ 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 + if contexts=$(k8s-vault list-enabled-contexts); then COMPREPLY+=( $(compgen -W "${contexts[*]}" -- "${_word_last}") ) fi } diff --git a/shard.yml b/shard.yml index f478463..faf54d9 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: k8s-vault -version: 0.2.1 +version: 0.2.2 description: | `k8s-vault` makes it easy to reach K8s API via jumphost, using SSH port forwarding. diff --git a/src/cli.cr b/src/cli.cr index 9e3ffd2..bef9f95 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -26,6 +26,9 @@ while cli_opts.size > 0 when "example-config" K8sVault.example_config exit 0 + when "list-enabled-contexts" + list_enabled_contexts.each {|c| puts c} rescue nil + exit 0 when "completion" K8sVault.completion exit 0 diff --git a/src/k8s-vault.cr b/src/k8s-vault.cr index 15f27fd..c488ea1 100644 --- a/src/k8s-vault.cr +++ b/src/k8s-vault.cr @@ -1,4 +1,3 @@ -require "colorize" require "kce" require "./k8s-vault/*" diff --git a/src/k8s-vault/helpers.cr b/src/k8s-vault/helpers.cr index d57d21a..c810034 100644 --- a/src/k8s-vault/helpers.cr +++ b/src/k8s-vault/helpers.cr @@ -1,4 +1,5 @@ require "socket" +require "./configreader" require "./log" module K8sVault @@ -10,8 +11,8 @@ module K8sVault # Example: # ``` # unless wait_for_connection(host: "localhost", port: 8080, timeout: 10, sleep_cycle: 1) - # STDERR.puts "failed to establish connection within 10 seconds" - # exit 1 + # STDERR.puts "failed to establish connection within 10 seconds" + # exit 1 # end # ``` def wait_for_connection(host : String = "localhost", port : Int32 = 80, timeout : Int32 = 5, sleep_cycle : Float32 = 0.25) : Bool @@ -43,5 +44,19 @@ module K8sVault end return true end + + def list_enabled_contexts(file : String? = nil) + file ||= K8sVault::K8SVAULT_CONFIG + if File.readable?(file) + begin + config = K8sVault::ConfigReader.config(file) + rescue YAML::ParseException + raise K8sVault::ConfigParseError.new("unable to parse config \"#{file}\"") + end + else + raise K8sVault::NoFileAccessError.new("\"#{file}\" is not readable") + end + config.clusters.map { |c| c.name if c.enabled == true }.compact + end end end diff --git a/src/k8s-vault/log.cr b/src/k8s-vault/log.cr index 087ac6c..f6e3a67 100644 --- a/src/k8s-vault/log.cr +++ b/src/k8s-vault/log.cr @@ -1,3 +1,5 @@ +require "colorize" + module K8sVault class Log # Controls debug logging