@@ -810,13 +810,7 @@ func GetCommands() []*cobra.Command {
810810 if err != nil {
811811 return err
812812 }
813- contextsMap := config .Contexts
814-
815- // Print the list of context names
816- fmt .Println ("Available Kubernetes contexts:" )
817- for contextName := range contextsMap {
818- fmt .Println (contextName )
819- }
813+ ShowDetailList (config )
820814 }
821815 return nil
822816 },
@@ -1412,6 +1406,45 @@ func ListContexts(kc *KubeConfig) error {
14121406 return nil
14131407}
14141408
1409+ func ShowDetailList (config * clientcmdapi.Config ) error {
1410+ contextsMap := config .Contexts
1411+
1412+ // Create a slice of context information
1413+ var contextInfo []struct {
1414+ ContextName string
1415+ ClusterName string
1416+ }
1417+
1418+ // Iterate through each context and extract the cluster and user information
1419+ for contextName , contextConfig := range contextsMap {
1420+ clusterName := contextConfig .Cluster
1421+ clusterConfig , found := config .Clusters [clusterName ]
1422+ if ! found {
1423+ return fmt .Errorf ("cluster %s not found in config" , clusterName )
1424+ }
1425+ contextInfo = append (contextInfo , struct {
1426+ ContextName string
1427+ ClusterName string
1428+ }{
1429+ ContextName : contextName ,
1430+ ClusterName : clusterConfig .Server ,
1431+ })
1432+ }
1433+
1434+ // Print the list of context names
1435+ fmt .Println ("Available Kubernetes contexts:" )
1436+
1437+ // Print the table of context information
1438+ table := tablewriter .NewWriter (os .Stdout )
1439+ table .SetHeader ([]string {"Context Name" , "Cluster Name" })
1440+ for _ , info := range contextInfo {
1441+ table .Append ([]string {info .ContextName , info .ClusterName })
1442+ }
1443+ table .Render ()
1444+
1445+ return nil
1446+ }
1447+
14151448func ShowContext (kc * KubeConfig ) error {
14161449 if err := kc .Load (); err != nil {
14171450 return err
0 commit comments