diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f9dd28..498d88b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 544410a..a7b4e1d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/kxd.md b/docs/kxd.md index 97f665a..dfa2759 100644 --- a/docs/kxd.md +++ b/docs/kxd.md @@ -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 diff --git a/docs/kxd_completion.md b/docs/kxd_completion.md index 5f68039..e634d63 100644 --- a/docs/kxd_completion.md +++ b/docs/kxd_completion.md @@ -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 diff --git a/docs/kxd_completion_bash.md b/docs/kxd_completion_bash.md index 8381838..2d4ad71 100644 --- a/docs/kxd_completion_bash.md +++ b/docs/kxd_completion_bash.md @@ -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 diff --git a/docs/kxd_completion_fish.md b/docs/kxd_completion_fish.md index 2b9952a..b844569 100644 --- a/docs/kxd_completion_fish.md +++ b/docs/kxd_completion_fish.md @@ -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 diff --git a/docs/kxd_completion_powershell.md b/docs/kxd_completion_powershell.md index 75c3196..ecb2644 100644 --- a/docs/kxd_completion_powershell.md +++ b/docs/kxd_completion_powershell.md @@ -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 diff --git a/docs/kxd_completion_zsh.md b/docs/kxd_completion_zsh.md index 0e7d258..0190728 100644 --- a/docs/kxd_completion_zsh.md +++ b/docs/kxd_completion_zsh.md @@ -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 diff --git a/docs/kxd_context.md b/docs/kxd_context.md index a155ec6..26277d4 100644 --- a/docs/kxd_context.md +++ b/docs/kxd_context.md @@ -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 diff --git a/docs/kxd_context_current.md b/docs/kxd_context_current.md index 591a9dc..7b25e02 100644 --- a/docs/kxd_context_current.md +++ b/docs/kxd_context_current.md @@ -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 diff --git a/docs/kxd_context_list.md b/docs/kxd_context_list.md new file mode 100644 index 0000000..54d7303 --- /dev/null +++ b/docs/kxd_context_list.md @@ -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 diff --git a/docs/kxd_context_switch.md b/docs/kxd_context_switch.md index 560a490..73ffc7f 100644 --- a/docs/kxd_context_switch.md +++ b/docs/kxd_context_switch.md @@ -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 diff --git a/docs/kxd_file.md b/docs/kxd_file.md index bcc4792..1f02639 100644 --- a/docs/kxd_file.md +++ b/docs/kxd_file.md @@ -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 diff --git a/docs/kxd_file_current.md b/docs/kxd_file_current.md index 074db21..80179c3 100644 --- a/docs/kxd_file_current.md +++ b/docs/kxd_file_current.md @@ -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 diff --git a/docs/kxd_file_list.md b/docs/kxd_file_list.md new file mode 100644 index 0000000..3f28e3f --- /dev/null +++ b/docs/kxd_file_list.md @@ -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 diff --git a/docs/kxd_file_switch.md b/docs/kxd_file_switch.md index 8ab0644..1dc9809 100644 --- a/docs/kxd_file_switch.md +++ b/docs/kxd_file_switch.md @@ -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 diff --git a/docs/kxd_version.md b/docs/kxd_version.md index 92fbd34..b8bda8b 100644 --- a/docs/kxd_version.md +++ b/docs/kxd_version.md @@ -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 diff --git a/src/cmd/context.go b/src/cmd/context.go index 8839281..8ba4530 100644 --- a/src/cmd/context.go +++ b/src/cmd/context.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" "log" "os" - "path/filepath" ) var contextCmd = &cobra.Command{ @@ -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) @@ -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) @@ -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 +} diff --git a/src/cmd/file.go b/src/cmd/file.go index f0b8bc3..f0b0f20 100644 --- a/src/cmd/file.go +++ b/src/cmd/file.go @@ -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{ @@ -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"), @@ -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 { @@ -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 +} diff --git a/src/cmd/root.go b/src/cmd/root.go index 134d46b..9108d45 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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() { diff --git a/src/cmd/version.go b/src/cmd/version.go index 0b1dc8b..4a4aade 100644 --- a/src/cmd/version.go +++ b/src/cmd/version.go @@ -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", diff --git a/src/utils/common.go b/src/utils/common.go index 7eb2760..4a9b997 100644 --- a/src/utils/common.go +++ b/src/utils/common.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" ) func TouchFile(name string) error { @@ -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() +} diff --git a/src/utils/kube.go b/src/utils/kube.go index d1d876b..39412a2 100644 --- a/src/utils/kube.go +++ b/src/utils/kube.go @@ -4,6 +4,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" "log" + "sort" ) func InitializeKubeconfig(kubeconfigPath string) (*api.Config, error) { @@ -25,5 +26,6 @@ func ListContexts(kubeconfigPath string) []string { for contextName := range config.Contexts { contexts = append(contexts, contextName) } + sort.Strings(contexts) return contexts }