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

Sort contexts before listing them #1016

Merged
merged 1 commit into from
Nov 1, 2024
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
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
Loading