Skip to content

Commit

Permalink
remove external deps for 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 19, 2021
1 parent a7d1cc1 commit e84b4be
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.1
0.2.2
15 changes: 5 additions & 10 deletions k8s-vault-completion.bash
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#!/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
}
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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/k8s-vault.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "colorize"
require "kce"
require "./k8s-vault/*"

Expand Down
19 changes: 17 additions & 2 deletions src/k8s-vault/helpers.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "socket"
require "./configreader"
require "./log"

module K8sVault
Expand All @@ -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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/k8s-vault/log.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "colorize"

module K8sVault
class Log
# Controls debug logging
Expand Down

0 comments on commit e84b4be

Please sign in to comment.