From 248487cdad56e595d0c5c517800e5d6624b8dfb5 Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Wed, 25 Oct 2023 11:22:58 -0400 Subject: [PATCH 1/2] default file current to .kxd file --- CHANGELOG.md | 4 ++++ README.md | 2 +- src/cmd/file.go | 29 ++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 482824b..2a4beef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v0.0.8 (October 25, 2023) +* Fix for setting default as argument. +* Changed the way kxd file current works to check `~/.kxd` file then default to `~/.kube/config`. + ## v0.0.7 (October 23, 2023) * Added support for setting config names as argument. * Added list command to `kxd file` and `kxd ctx`. diff --git a/README.md b/README.md index 79d7e47..8c75c22 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ kxd version To persist the set config when you open new terminal windows, you can add the following to your bash profile or zshrc. ```bash -export KUBECONFIG=$(cat ~/.kxd) +export KUBECONFIG=$(kxd file current) ``` ### Show your set kubeconfig in your shell prompt diff --git a/src/cmd/file.go b/src/cmd/file.go index b1541d3..6862ec1 100644 --- a/src/cmd/file.go +++ b/src/cmd/file.go @@ -8,6 +8,7 @@ import ( "log" "os" "path/filepath" + "strings" ) var fileCmd = &cobra.Command{ @@ -103,14 +104,28 @@ func runConfigSwitcher() error { } func runGetCurrentConfig() error { - kubeconfigPath := utils.GetEnv("KUBECONFIG", filepath.Join(utils.GetHomeDir(), ".kube/config")) - if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) { - log.Fatal("No current kubeconfig found.") - } else if err != nil { - log.Fatal(err) - } else { - fmt.Println(kubeconfigPath) + homeDir := utils.GetHomeDir() + kxdPath := filepath.Join(homeDir, ".kxd") + + defaultKubeConfigPath := filepath.Join(homeDir, ".kube", "config") + configPath := defaultKubeConfigPath + + if _, err := os.Stat(kxdPath); !os.IsNotExist(err) { + content, _ := os.ReadFile(kxdPath) + trimmedContent := strings.TrimSpace(string(content)) + if trimmedContent != "" { + specifiedConfigPath := filepath.Join(homeDir, ".kube", trimmedContent) + if _, err := os.Stat(specifiedConfigPath); !os.IsNotExist(err) { + configPath = specifiedConfigPath + } + } } + + if _, err := os.Stat(configPath); os.IsNotExist(err) { + log.Fatal("Kubeconfig not found") + } + + fmt.Println(configPath) return nil } From 55d9298d7812413a2bd11f86ea88e7bfa6e0dcbe Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Wed, 25 Oct 2023 11:55:46 -0400 Subject: [PATCH 2/2] fallback to KUBECONFIG var then ~/.kube/config --- src/cmd/file.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/file.go b/src/cmd/file.go index 6862ec1..c565e0b 100644 --- a/src/cmd/file.go +++ b/src/cmd/file.go @@ -107,14 +107,20 @@ func runGetCurrentConfig() error { homeDir := utils.GetHomeDir() kxdPath := filepath.Join(homeDir, ".kxd") - defaultKubeConfigPath := filepath.Join(homeDir, ".kube", "config") + // Default to KUBECONFIG env var then ~/.kube/config if .kxd config doesn't exist + defaultKubeConfigPath := utils.GetEnv("KUBECONFIG", filepath.Join(homeDir, ".kube", "config")) configPath := defaultKubeConfigPath + // Check if .kxd file exists. if _, err := os.Stat(kxdPath); !os.IsNotExist(err) { content, _ := os.ReadFile(kxdPath) trimmedContent := strings.TrimSpace(string(content)) + + // If .kxd file is not empty, determine the specified kubeconfig path. if trimmedContent != "" { specifiedConfigPath := filepath.Join(homeDir, ".kube", trimmedContent) + + // If the specified kubeconfig exists, update the configPath. if _, err := os.Stat(specifiedConfigPath); !os.IsNotExist(err) { configPath = specifiedConfigPath }