Skip to content

Commit

Permalink
feat: add --kubeconfig flag support
Browse files Browse the repository at this point in the history
When using the `--kubeconfig` flag fish completions were ignoring this
and utilising the default kubeconfig file or the one specified in the
`KUBECONFIG` env variable.

This commit adds code that checks for the `--kubeconfig PATH` or
`--kubeconfig=PATH` flags and passes them through when the completions
use kubectl commands 👍🏼👍🏼.

Signed-off-by: Jonathan Parton <[email protected]>
  • Loading branch information
JonParton authored and evanlucas committed Aug 16, 2021
1 parent bbe3b83 commit ced6763
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
36 changes: 35 additions & 1 deletion completions/kubectl.fish
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ function __fish_kubectl
end
end

command kubectl $__fish_kubectl_timeout $context_args $argv
set -l kubeconfig_args

if set -l kubeconfig_args (__fish_kubectl_get_kubeconfig_flags | string split " ")
for c in $kubeconfig_args
set kubeconfig_args $kubeconfig_args $c
end
end

command kubectl $__fish_kubectl_timeout $context_args $kubeconfig_args $argv
end

function __fish_kubectl_get_commands
Expand Down Expand Up @@ -288,6 +296,32 @@ function __fish_kubectl_get_context_flags
return 1
end

function __fish_kubectl_get_kubeconfig_flags
set -l cmd (commandline -opc)
if [ (count $cmd) -eq 0 ]
return 1
end

set -l foundKubeconfig 0

for c in $cmd
test $foundKubeconfig -eq 1
set -l out "--kubeconfig" "$c"
and echo $out
and return 0

if string match -q -r -- "--kubeconfig=" "$c"
set -l out (string split -- "=" "$c" | string join " ")
and echo $out
and return 0
else if contains -- "$c" "--kubeconfig"
set foundKubeconfig 1
end
end

return 1
end

function __fish_kubectl_get_ns_flags
set -l cmd (commandline -opc)
if [ (count $cmd) -eq 0 ]
Expand Down
36 changes: 35 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ function __fish_kubectl
end
end
command kubectl $__fish_kubectl_timeout $context_args $argv
set -l kubeconfig_args
if set -l kubeconfig_args (__fish_kubectl_get_kubeconfig_flags | string split " ")
for c in $kubeconfig_args
set kubeconfig_args $kubeconfig_args $c
end
end
command kubectl $__fish_kubectl_timeout $context_args $kubeconfig_args $argv
end
function __fish_kubectl_get_commands
Expand Down Expand Up @@ -219,6 +227,32 @@ function __fish_kubectl_get_context_flags
return 1
end
function __fish_kubectl_get_kubeconfig_flags
set -l cmd (commandline -opc)
if [ (count $cmd) -eq 0 ]
return 1
end
set -l foundKubeconfig 0
for c in $cmd
test $foundKubeconfig -eq 1
set -l out "--kubeconfig" "$c"
and echo $out
and return 0
if string match -q -r -- "--kubeconfig=" "$c"
set -l out (string split -- "=" "$c" | string join " ")
and echo $out
and return 0
else if contains -- "$c" "--kubeconfig"
set foundKubeconfig 1
end
end
return 1
end
function __fish_kubectl_get_ns_flags
set -l cmd (commandline -opc)
if [ (count $cmd) -eq 0 ]
Expand Down

0 comments on commit ced6763

Please sign in to comment.