This commit is contained in:
Sulabh Chaturvedi 2025-01-23 12:07:06 -05:00 committed by GitHub
commit 1680b370dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 1 deletions

View File

@ -41,6 +41,7 @@ type InteractiveDeleteOp struct {
func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
// parse kubeconfig just to see if it can be loaded
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
printer.Warning(stderr, "kubeconfig file not found")
@ -48,6 +49,12 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
}
return errors.Wrap(err, "kubeconfig error")
}
ctxs := kc.ContextNames()
if ctxs == nil {
err := printer.Warning(stderr, "No kubectl context found")
return errors.Wrap(err, "kubeconfig error")
}
kc.Close()
cmd := exec.Command("fzf", "--ansi", "--no-preview")

View File

@ -41,6 +41,10 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error {
}
ctxs := kc.ContextNames()
if ctxs == nil {
err := printer.Warning(stderr, "No kubectl context found")
return errors.Wrap(err, "kubeconfig error")
}
natsort.Sort(ctxs)
cur := kc.GetCurrentContext()

View File

@ -214,7 +214,7 @@ load common
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "" ]]
[[ "$output" = "warning: No kubectl context found" ]]
}
@test "delete several contexts including a non existent one" {
@ -242,3 +242,11 @@ load common
run ${COMMAND} -c
[ "$status" -ne 0 ]
}
@test "display msg on empty context" {
use_config config3
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "warning: No kubectl context found" ]]
}

15
test/testdata/config3 vendored Normal file
View File

@ -0,0 +1,15 @@
# config with no context
apiVersion: v1
clusters:
- cluster:
server: ""
name: cluster1
contexts:
- context:
current-context: ""
kind: Config
preferences: {}
users:
- name: user1
user: {}