mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-08 03:48:57 +00:00
do not fail on non-existing kubeconfig files
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/mattn/go-isatty"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/env"
|
||||
"github.com/ahmetb/kubectx/internal/kubeconfig"
|
||||
"github.com/ahmetb/kubectx/internal/printer"
|
||||
)
|
||||
|
||||
@@ -21,6 +22,17 @@ type InteractiveSwitchOp struct {
|
||||
}
|
||||
|
||||
func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
|
||||
// parse kubeconfig just to see if it can be loaded
|
||||
kc := new(kubeconfig.Kubeconfig).WithLoader(defaultLoader)
|
||||
if err := kc.Parse(); err != nil {
|
||||
if isENOENT(err) {
|
||||
printer.Warning(stderr, "kubeconfig file not found")
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "kubeconfig error")
|
||||
}
|
||||
kc.Close()
|
||||
|
||||
cmd := exec.Command("fzf", "--ansi", "--no-preview")
|
||||
var out bytes.Buffer
|
||||
cmd.Stdin = os.Stdin
|
||||
|
@@ -33,7 +33,7 @@ func (*StandardKubeconfigLoader) Load() (kubeconfig.ReadWriteResetCloser, error)
|
||||
f, err := os.OpenFile(cfgPath, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, errors.Errorf("kubeconfig file not found at %s", cfgPath)
|
||||
return nil, errors.Wrap(err, "kubeconfig file not found")
|
||||
}
|
||||
return nil, errors.Wrap(err, "failed to open file")
|
||||
}
|
||||
|
@@ -6,8 +6,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/kubeconfig"
|
||||
)
|
||||
|
||||
func withTestVar(key, value string) func() {
|
||||
// TODO(ahmetb) this method is currently duplicated
|
||||
@@ -131,6 +132,18 @@ func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStandardKubeconfigLoader_returnsNotFoundErr(t *testing.T) {
|
||||
defer withTestVar("KUBECONFIG", "foo")()
|
||||
kc := new(kubeconfig.Kubeconfig).WithLoader(defaultLoader)
|
||||
err := kc.Parse()
|
||||
if err == nil {
|
||||
t.Fatal("expected err")
|
||||
}
|
||||
if !isENOENT(err) {
|
||||
t.Fatalf("expected ENOENT error; got=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testfile(t *testing.T, contents string) (path string, cleanup func()) {
|
||||
t.Helper()
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"facette.io/natsort"
|
||||
"github.com/fatih/color"
|
||||
@@ -15,10 +16,14 @@ import (
|
||||
// ListOp describes listing contexts.
|
||||
type ListOp struct{}
|
||||
|
||||
func (_ ListOp) Run(stdout, _ io.Writer) error {
|
||||
func (_ ListOp) Run(stdout, stderr io.Writer) error {
|
||||
kc := new(kubeconfig.Kubeconfig).WithLoader(defaultLoader)
|
||||
defer kc.Close()
|
||||
if err := kc.Parse(); err != nil {
|
||||
if isENOENT(err) {
|
||||
printer.Warning(stderr, "kubeconfig file not found")
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "kubeconfig error")
|
||||
}
|
||||
|
||||
@@ -45,3 +50,14 @@ func (_ ListOp) Run(stdout, _ io.Writer) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isENOENT determines if the underlying error is os.IsNotExist. Right now
|
||||
// errors from github.com/pkg/errors doesn't work with os.IsNotExist.
|
||||
func isENOENT(err error) bool {
|
||||
for e := err; e != nil; e = errors.Unwrap(e) {
|
||||
if os.IsNotExist(e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user