Skip to content

Commit

Permalink
default file list to .kxd file
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Oct 25, 2023
1 parent 914f768 commit 1e7c9e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 22 additions & 7 deletions src/cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
)

var fileCmd = &cobra.Command{
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 1e7c9e9

Please sign in to comment.