From aa50f4620d454ade2df54ecef3f9f190340d3fd0 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 1 Nov 2024 03:55:44 +0200 Subject: [PATCH] Sort contexts before listing them (#1016) It was annoying to have a different order for the context every time I ran `kubecm switch` Signed-off-by: Yarden Shoham --- cmd/delete.go | 2 ++ cmd/export.go | 3 +++ cmd/rename.go | 2 ++ cmd/switch.go | 2 ++ cmd/utils.go | 4 ++++ 5 files changed, 13 insertions(+) diff --git a/cmd/delete.go b/cmd/delete.go index 4d8e6dc5..5af6abae 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "slices" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" @@ -112,6 +113,7 @@ func selectDeleteContext(config *clientcmdapi.Config) (string, string, error) { kubeItems = append([]Needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...) } } + slices.SortFunc(kubeItems, compareKubeItems) // exit option kubeItems, err := ExitOption(kubeItems) if err != nil { diff --git a/cmd/export.go b/cmd/export.go index 13168378..cc664acb 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -3,6 +3,8 @@ package cmd import ( "errors" "fmt" + "slices" + "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" @@ -93,6 +95,7 @@ func selectExportContext(config *clientcmdapi.Config) (string, string, error) { kubeItems = append([]Needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...) } } + slices.SortFunc(kubeItems, compareKubeItems) // exit option kubeItems, err := ExitOption(kubeItems) if err != nil { diff --git a/cmd/rename.go b/cmd/rename.go index 4eef247e..e493b3f7 100644 --- a/cmd/rename.go +++ b/cmd/rename.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "slices" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" @@ -42,6 +43,7 @@ func (rc *RenameCommand) runRename(command *cobra.Command, args []string) error kubeItems = append([]Needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...) } } + slices.SortFunc(kubeItems, compareKubeItems) var kubeName string var rename string // args option diff --git a/cmd/switch.go b/cmd/switch.go index f0e1a441..3bca37db 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "slices" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" @@ -81,6 +82,7 @@ func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error) kubeItems = append([]Needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...) } } + slices.SortFunc(kubeItems, compareKubeItems) // exit option kubeItems, err := ExitOption(kubeItems) if err != nil { diff --git a/cmd/utils.go b/cmd/utils.go index 959f32a8..f0299212 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -452,6 +452,10 @@ func CheckAndTransformFilePath(path string, autoCreate bool) (string, error) { return path, nil } +func compareKubeItems(a, b Needle) int { + return strings.Compare(a.Name, b.Name) +} + // CheckValidContext check and clean mismatched AuthInfo and Cluster func CheckValidContext(clear bool, config *clientcmdapi.Config) *clientcmdapi.Config { for key, obj := range config.Contexts {