Skip to content

Commit bd49a55

Browse files
committed
add table view for list cmd
Signed-off-by: sh2 <[email protected]>
1 parent 17e4e6d commit bd49a55

File tree

2 files changed

+67
-24
lines changed

2 files changed

+67
-24
lines changed

pkg/cmd/gtctl/cluster/list/list.go

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,92 @@ package list
1717
import (
1818
"context"
1919
"fmt"
20+
"os"
2021

2122
greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
23+
"github.com/olekukonko/tablewriter"
2224
"github.com/spf13/cobra"
2325
"k8s.io/apimachinery/pkg/api/errors"
2426

27+
"github.com/GreptimeTeam/gtctl/pkg/deployer"
2528
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
2629
"github.com/GreptimeTeam/gtctl/pkg/logger"
2730
)
2831

2932
func NewListClustersCommand(l logger.Logger) *cobra.Command {
33+
table := tablewriter.NewWriter(os.Stdout)
34+
configClustersTableView(table)
35+
3036
cmd := &cobra.Command{
3137
Use: "list",
3238
Short: "List all GreptimeDB clusters",
3339
Long: `List all GreptimeDB clusters`,
3440
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()
3942

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 {
4344
return err
4445
}
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-
}
5946

6047
return nil
6148
},
6249
}
6350

6451
return cmd
6552
}
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+
}

pkg/deployer/k8s/deployer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ func (d *deployer) CreateEtcdCluster(ctx context.Context, name string, options *
175175

176176
manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.EtcdBitnamiOCIRegistry, helm.DefaultEtcdChartVersion, *options)
177177
if err != nil {
178-
return err
178+
return fmt.Errorf("error while loading helm chart: %v", err)
179179
}
180180

181181
if d.dryRun {
182182
d.logger.V(0).Info(string(manifests))
183183
return nil
184184
}
185185

186-
if err := d.client.Apply(ctx, manifests); err != nil {
187-
return err
186+
if err = d.client.Apply(ctx, manifests); err != nil {
187+
return fmt.Errorf("error while applying helm chart: %v", err)
188188
}
189189

190190
return d.client.WaitForEtcdReady(ctx, resourceName, resourceNamespace, d.timeout)

0 commit comments

Comments
 (0)