Skip to content

Commit

Permalink
Sort contexts before listing them (#1016)
Browse files Browse the repository at this point in the history
It was annoying to have a different order for the context every time I ran `kubecm switch`

Signed-off-by: Yarden Shoham <[email protected]>
  • Loading branch information
yardenshoham authored Nov 1, 2024
1 parent 21244ba commit aa50f46
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"slices"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"slices"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"slices"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit aa50f46

Please sign in to comment.