Skip to content

Commit

Permalink
Add list cmd, default root cmd to config switching
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Oct 5, 2023
1 parent ab324dd commit 5ec4bc6
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 43 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.7 (UNRELEASED)
* Added list command to `kxd file` and `kxd ctx`.
* The root command now defaults to `kxd file switch` if no sub-commands are passed in.

## v0.0.6 (September 29, 2023)
* Allow for specifying multiple matchers as a comma seperated string for `KXD_MATCHER` environment variable.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ kxd ctx s
This command will display a list of available contexts in your current kubeconfig file. Select the one you want to switch to.


### Listing Current Kubeconfig or Kubernetes Context
### Getting Current Kubeconfig or Kubernetes Context

To list the currently set Kubeconfig or Kubernetes Context, use the following commands:
To get the currently set Kubeconfig or Kubernetes Context, use the following commands:

```bash
kxd f c
Expand Down
2 changes: 1 addition & 1 deletion docs/kxd.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Allows for switching kubeconfig files and contexts, as well as getting the curre
* [kxd file](kxd_file.md) - Kubeconfig file command
* [kxd version](kxd_version.md) - kxd version command

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ See each sub-command's help for details on how to use the generated script.
* [kxd completion powershell](kxd_completion_powershell.md) - Generate the autocompletion script for powershell
* [kxd completion zsh](kxd_completion_zsh.md) - Generate the autocompletion script for zsh

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ kxd completion bash

* [kxd completion](kxd_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ kxd completion fish [flags]

* [kxd completion](kxd_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ kxd completion powershell [flags]

* [kxd completion](kxd_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kxd completion zsh [flags]

* [kxd completion](kxd_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
3 changes: 2 additions & 1 deletion docs/kxd_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is the default context command.

* [kxd](kxd.md) - kxd - switch between Kubeconfigs and contexts.
* [kxd context current](kxd_context_current.md) - Shows currently set kubeconfig context
* [kxd context list](kxd_context_list.md) - List kubeconfig contexts
* [kxd context switch](kxd_context_switch.md) - Switch kubeconfig contexts

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_context_current.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kxd context current [flags]

* [kxd context](kxd_context.md) - Kubeconfig context command

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
23 changes: 23 additions & 0 deletions docs/kxd_context_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## kxd context list

List kubeconfig contexts

### Synopsis

This displays a simple list of your kubeconfig contexts.

```
kxd context list [flags]
```

### Options

```
-h, --help help for list
```

### SEE ALSO

* [kxd context](kxd_context.md) - Kubeconfig context command

###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_context_switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kxd context switch [flags]

* [kxd context](kxd_context.md) - Kubeconfig context command

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
3 changes: 2 additions & 1 deletion docs/kxd_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is the default file command.

* [kxd](kxd.md) - kxd - switch between Kubeconfigs and contexts.
* [kxd file current](kxd_file_current.md) - Shows currently set kubeconfig
* [kxd file list](kxd_file_list.md) - List kubeconfigs
* [kxd file switch](kxd_file_switch.md) - Switch kubeconfig

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_file_current.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kxd file current [flags]

* [kxd file](kxd_file.md) - Kubeconfig file command

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
23 changes: 23 additions & 0 deletions docs/kxd_file_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## kxd file list

List kubeconfigs

### Synopsis

This displays a simple list of your kubeconfigs.

```
kxd file list [flags]
```

### Options

```
-h, --help help for list
```

### SEE ALSO

* [kxd file](kxd_file.md) - Kubeconfig file command

###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_file_switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kxd file switch [flags]

* [kxd file](kxd_file.md) - Kubeconfig file command

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
2 changes: 1 addition & 1 deletion docs/kxd_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kxd version [flags]

* [kxd](kxd.md) - kxd - switch between Kubeconfigs and contexts.

###### Auto generated by spf13/cobra on 25-Sep-2023
###### Auto generated by spf13/cobra on 5-Oct-2023
31 changes: 24 additions & 7 deletions src/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
"log"
"os"
"path/filepath"
)

var contextCmd = &cobra.Command{
Expand Down Expand Up @@ -43,15 +42,26 @@ var switchContextCmd = &cobra.Command{
},
}

var listContextsCmd = &cobra.Command{
Use: "list",
Short: "List kubeconfig contexts",
Aliases: []string{"l"},
Long: "This displays a simple list of your kubeconfig contexts.",
Run: func(cmd *cobra.Command, args []string) {
err := runContextLister()
if err != nil {
log.Fatal(err)
}
},
}

func init() {
contextCmd.AddCommand(switchContextCmd)
contextCmd.AddCommand(currentContextCmd)
contextCmd.AddCommand(switchContextCmd, currentContextCmd, listContextsCmd)
rootCmd.AddCommand(contextCmd)
}

func runContextSwitcher() error {
homeDir := utils.GetHomeDir()
kubeconfigPath := utils.GetEnv("KUBECONFIG", filepath.Join(homeDir, ".kube/config"))
kubeconfigPath := utils.GetCurrentConfigFile()
config, err := utils.InitializeKubeconfig(kubeconfigPath)
if err != nil {
log.Fatalf("Error initializing kubeconfig: %v\n", err)
Expand Down Expand Up @@ -94,8 +104,7 @@ func runContextSwitcher() error {
}

func runListContext() error {
homeDir := utils.GetHomeDir()
kubeconfigPath := utils.GetEnv("KUBECONFIG", filepath.Join(homeDir, ".kube/config"))
kubeconfigPath := utils.GetCurrentConfigFile()
config, err := utils.InitializeKubeconfig(kubeconfigPath)
if err != nil {
log.Fatalf("Error initializing kubeconfig: %v\n", err)
Expand All @@ -108,3 +117,11 @@ func runListContext() error {
fmt.Printf(currentContext + "\n")
return nil
}

func runContextLister() error {
contexts := utils.ListContexts(utils.GetCurrentConfigFile())
for _, c := range contexts {
fmt.Println(c)
}
return nil
}
61 changes: 41 additions & 20 deletions src/cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cmd

import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/radiusmethod/kxd/src/utils"
"github.com/spf13/cobra"
"log"
"os"
"path/filepath"
"sort"
"strings"

"github.com/manifoldco/promptui"
"github.com/radiusmethod/kxd/src/utils"
"github.com/spf13/cobra"
)

var fileCmd = &cobra.Command{
Expand Down Expand Up @@ -45,22 +46,30 @@ var switchFileCmd = &cobra.Command{
},
}

var listFileCmd = &cobra.Command{
Use: "list",
Short: "List kubeconfigs",
Aliases: []string{"l"},
Long: "This displays a simple list of your kubeconfigs.",
Run: func(cmd *cobra.Command, args []string) {
err := runConfigLister()
if err != nil {
log.Fatal(err)
}
},
}

func init() {
fileCmd.AddCommand(switchFileCmd)
fileCmd.AddCommand(currentFileCmd)
fileCmd.AddCommand(switchFileCmd, currentFileCmd, listFileCmd)
rootCmd.AddCommand(fileCmd)
}

func runConfigSwitcher() error {
homeDir := utils.GetHomeDir()
configFileLocation := fmt.Sprintf("%s/.kube", homeDir)
configs := getConfigs(configFileLocation, homeDir)

err := utils.TouchFile(fmt.Sprintf("%s/.kxd", homeDir))
configs := getConfigs()
err := utils.TouchFile(filepath.Join(utils.GetHomeDir(), ".kxd"))
if err != nil {
log.Fatal(err)
}

fmt.Printf(utils.NoticeColor, "Kubeconfig Switcher\n")
prompt := promptui.Select{
Label: fmt.Sprintf(utils.PromptColor, "Choose a config"),
Expand Down Expand Up @@ -91,25 +100,27 @@ func runConfigSwitcher() error {
if result == "default" {
result = "config"
}
utils.WriteFile(result, homeDir)
utils.WriteFile(result, utils.GetHomeDir())

return nil
}

func runGetCurrentConfig() error {
homeDir := utils.GetHomeDir()
kubeconfigPath := utils.GetEnv("KUBECONFIG", filepath.Join(homeDir, ".kube/config"))
if _, err := os.Stat(kubeconfigPath); err == nil {
fmt.Println(kubeconfigPath)
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("No current kubeconfig found.")
os.Exit(1)
fmt.Println(kubeconfigPath)
}
return nil
}

func getConfigs(configFileLocation string, homeDir string) []string {
func getConfigs() []string {
var files []string
configFileLocation := utils.GetConfigFileLocation()

fileExts := strings.Split(utils.GetEnv("KXD_MATCHER", ".conf"), ",")
err := filepath.Walk(configFileLocation, func(path string, f os.FileInfo, _ error) error {
for _, value := range fileExts {
Expand All @@ -120,14 +131,24 @@ func getConfigs(configFileLocation string, homeDir string) []string {
}
return nil
})

if err != nil {
log.Fatal(err)
}

if _, err := os.Stat(fmt.Sprintf("%s/.kube/config", homeDir)); err == nil {
defaultConfigPath := filepath.Join(utils.GetHomeDir(), ".kube/config")
if _, err := os.Stat(defaultConfigPath); err == nil {
files = append(files, "default")
}
files = append(files, "unset")
sort.Strings(files)
return files
}

func runConfigLister() error {
configs := getConfigs()
for _, c := range configs {
fmt.Println(c)
}
return nil
}
7 changes: 7 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import (
"fmt"
"github.com/spf13/cobra"
"os"
"log"
)

var rootCmd = &cobra.Command{
Use: "kxd",
Short: "kxd - switch between Kubeconfigs and contexts.",
Long: "Allows for switching kubeconfig files and contexts, as well as getting the current set ones.",
Run: func(cmd *cobra.Command, args []string) {
err := runConfigSwitcher()
if err != nil {
log.Fatal(err)
}
},
}

func Execute() {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

var version string = "v0.0.6"
var version string = "v0.0.7"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
22 changes: 22 additions & 0 deletions src/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
)

func TouchFile(name string) error {
Expand Down Expand Up @@ -51,3 +52,24 @@ func GetHomeDir() string {
}
return homeDir
}

func GetConfigFileLocation() string {
configFileLocation := filepath.Join(GetHomeDir(), ".kube")
if IsDirectoryExists(configFileLocation) {
return filepath.Join(configFileLocation)
}
log.Fatalf("~/.kube directory does not exist!")
return ""
}

func GetCurrentConfigFile() string {
return GetEnv("KUBECONFIG", filepath.Join(GetHomeDir(), ".kube/config"))
}

func IsDirectoryExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
Loading

0 comments on commit 5ec4bc6

Please sign in to comment.