@@ -17,49 +17,92 @@ package list
17
17
import (
18
18
"context"
19
19
"fmt"
20
+ "os"
20
21
21
22
greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
23
+ "github.com/olekukonko/tablewriter"
22
24
"github.com/spf13/cobra"
23
25
"k8s.io/apimachinery/pkg/api/errors"
24
26
27
+ "github.com/GreptimeTeam/gtctl/pkg/deployer"
25
28
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
26
29
"github.com/GreptimeTeam/gtctl/pkg/logger"
27
30
)
28
31
29
32
func NewListClustersCommand (l logger.Logger ) * cobra.Command {
33
+ table := tablewriter .NewWriter (os .Stdout )
34
+ configClustersTableView (table )
35
+
30
36
cmd := & cobra.Command {
31
37
Use : "list" ,
32
38
Short : "List all GreptimeDB clusters" ,
33
39
Long : `List all GreptimeDB clusters` ,
34
40
RunE : func (cmd * cobra.Command , args []string ) error {
35
- k8sDeployer , err := k8s .NewDeployer (l )
36
- if err != nil {
37
- return err
38
- }
41
+ ctx := context .Background ()
39
42
40
- ctx := context .TODO ()
41
- clusters , err := k8sDeployer .ListGreptimeDBClusters (ctx , nil )
42
- if err != nil && ! errors .IsNotFound (err ) {
43
+ if err := listClustersFromKubernetes (ctx , l , table ); err != nil {
43
44
return err
44
45
}
45
- if errors .IsNotFound (err ) || (clusters != nil && len (clusters ) == 0 ) {
46
- l .Error ("clusters not found\n " )
47
- return nil
48
- }
49
-
50
- // TODO(zyy17): more human friendly output format.
51
- for _ , cluster := range clusters {
52
- rawCluster , ok := cluster .Raw .(* greptimedbclusterv1alpha1.GreptimeDBCluster )
53
- if ! ok {
54
- return fmt .Errorf ("invalid cluster type" )
55
- }
56
- l .V (0 ).Infof ("Cluster '%s' in '%s' namespace is running, create at %s\n " ,
57
- rawCluster .Name , rawCluster .Namespace , rawCluster .CreationTimestamp )
58
- }
59
46
60
47
return nil
61
48
},
62
49
}
63
50
64
51
return cmd
65
52
}
53
+
54
+ func listClustersFromKubernetes (ctx context.Context , l logger.Logger , table * tablewriter.Table ) error {
55
+ k8sDeployer , err := k8s .NewDeployer (l )
56
+ if err != nil {
57
+ return err
58
+ }
59
+
60
+ clusters , err := k8sDeployer .ListGreptimeDBClusters (ctx , nil )
61
+ if err != nil && ! errors .IsNotFound (err ) {
62
+ return err
63
+ }
64
+ if errors .IsNotFound (err ) || (clusters != nil && len (clusters ) == 0 ) {
65
+ l .Error ("clusters not found\n " )
66
+ return nil
67
+ }
68
+
69
+ if err := renderClustersTableView (table , clusters ); err != nil {
70
+ return err
71
+ }
72
+
73
+ return nil
74
+ }
75
+
76
+ func configClustersTableView (table * tablewriter.Table ) {
77
+ table .SetAutoWrapText (false )
78
+ table .SetAutoFormatHeaders (true )
79
+ table .SetHeaderAlignment (tablewriter .ALIGN_LEFT )
80
+ table .SetAlignment (tablewriter .ALIGN_LEFT )
81
+ table .SetCenterSeparator ("" )
82
+ table .SetColumnSeparator ("" )
83
+ table .SetRowSeparator ("" )
84
+ table .SetHeaderLine (false )
85
+ table .SetBorder (false )
86
+ table .SetTablePadding ("\t " )
87
+ table .SetNoWhiteSpace (true )
88
+ }
89
+
90
+ func renderClustersTableView (table * tablewriter.Table , clusters []* deployer.GreptimeDBCluster ) error {
91
+ table .SetHeader ([]string {"Name" , "Namespace" , "Creation Date" })
92
+
93
+ for _ , cluster := range clusters {
94
+ rawCluster , ok := cluster .Raw .(* greptimedbclusterv1alpha1.GreptimeDBCluster )
95
+ if ! ok {
96
+ return fmt .Errorf ("invalid cluster type" )
97
+ }
98
+ table .Append ([]string {
99
+ rawCluster .Name ,
100
+ rawCluster .Namespace ,
101
+ rawCluster .CreationTimestamp .String (),
102
+ })
103
+ }
104
+
105
+ table .Render ()
106
+
107
+ return nil
108
+ }
0 commit comments