From 03df8e3afce823078edf8c7a55c8d2f81c07374d Mon Sep 17 00:00:00 2001 From: ebarped Date: Wed, 5 Jan 2022 12:49:15 +0100 Subject: [PATCH] Add "none" option --- cmd/root.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index e0e060f..f3aeedf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -75,6 +75,18 @@ func rootFunc(cmd *cobra.Command, args []string) { name := args[0] path := kcRootDir + name log.Debug().Str("name", name).Str("path", path).Msgf("loading kubeconfig...") + + // "deselect" kubeconfig + if name == "none" { + err := deleteDefaultKubeconfig() + if err != nil { + log.Error().Str("name", "none").Str("path", path).Str("error", err.Error()).Msg("error setting kubeconfig to none...") + retcode = 1 + } + retcode = 0 + return + } + kc, err := kubeconfig.New(name, path) if err != nil { log.Error().Str("name", name).Str("path", path).Str("error", err.Error()).Msg("error loading kubeconfig") @@ -164,6 +176,21 @@ func rootFunc(cmd *cobra.Command, args []string) { } } +func deleteDefaultKubeconfig() error { + path := kcRootDir + "config" + + _, err := os.Stat(path) + if err != nil { + // .kube/config does not exist, just exit + return nil + } + err = os.Remove(path) + if err != nil { + return err + } + return nil +} + func useKubeconfig(kc *kubeconfig.Kubeconfig) error { _, err := copy(kc.Path, kcRootDir+"config") if err != nil {