Skip to content

Commit

Permalink
default file current to .kxd file (#23)
Browse files Browse the repository at this point in the history
* default file current to .kxd file

* fallback to KUBECONFIG var then ~/.kube/config
  • Loading branch information
pjaudiomv authored Oct 26, 2023
1 parent 914f768 commit 8933936
Show file tree
Hide file tree
Showing 3 changed files with 33 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
35 changes: 28 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,34 @@ 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")

// 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
}
}
}

if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Fatal("Kubeconfig not found")
}

fmt.Println(configPath)
return nil
}

Expand Down

0 comments on commit 8933936

Please sign in to comment.