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 }