Skip to content

Commit 3ab4346

Browse files
committed
Set render table for list context
1 parent 5582a55 commit 3ab4346

3 files changed

Lines changed: 81 additions & 14 deletions

File tree

src/features/context.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88

99
"github.com/AlecAivazis/survey/v2"
10+
"github.com/olekukonko/tablewriter"
1011
"k8s.io/client-go/kubernetes"
1112
"k8s.io/client-go/tools/clientcmd"
1213
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
@@ -122,6 +123,45 @@ func ListContexts(kc *KubeConfig) error {
122123
return nil
123124
}
124125

126+
func ShowDetailList(config *clientcmdapi.Config) error {
127+
contextsMap := config.Contexts
128+
129+
// Create a slice of context information
130+
var contextInfo []struct {
131+
ContextName string
132+
ClusterName string
133+
}
134+
135+
// Iterate through each context and extract the cluster and user information
136+
for contextName, contextConfig := range contextsMap {
137+
clusterName := contextConfig.Cluster
138+
clusterConfig, found := config.Clusters[clusterName]
139+
if !found {
140+
return fmt.Errorf("cluster %s not found in config", clusterName)
141+
}
142+
contextInfo = append(contextInfo, struct {
143+
ContextName string
144+
ClusterName string
145+
}{
146+
ContextName: contextName,
147+
ClusterName: clusterConfig.Server,
148+
})
149+
}
150+
151+
// Print the list of context names
152+
fmt.Println("Available Kubernetes contexts:")
153+
154+
// Print the table of context information
155+
table := tablewriter.NewWriter(os.Stdout)
156+
table.SetHeader([]string{"Context Name", "Cluster Name"})
157+
for _, info := range contextInfo {
158+
table.Append([]string{info.ContextName, info.ClusterName})
159+
}
160+
table.Render()
161+
162+
return nil
163+
}
164+
125165
func ShowContext(kc *KubeConfig) error {
126166
if err := kc.Load(); err != nil {
127167
return err

src/features/menus.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ func GetCommands() []*cobra.Command {
6565
if err != nil {
6666
return err
6767
}
68-
contextsMap := config.Contexts
69-
70-
// Print the list of context names
71-
fmt.Println("Available Kubernetes contexts:")
72-
for contextName := range contextsMap {
73-
fmt.Println(contextName)
74-
}
68+
ShowDetailList(config)
7569
}
7670
return nil
7771
},

src/singleton/main.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
14151448
func ShowContext(kc *KubeConfig) error {
14161449
if err := kc.Load(); err != nil {
14171450
return err

0 commit comments

Comments
 (0)