Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default file current to .kxd file #23

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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