Skip to content

Commit 70c8ccc

Browse files
authored
Merge pull request #1272 from planetscale/add-list-tablets-filters
Add filter flags to `branch list-tablets`
2 parents 6f0302c + ec5eb0e commit 70c8ccc

4 files changed

Lines changed: 129 additions & 6 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/mattn/go-shellwords v1.0.12
2626
github.com/mitchellh/go-homedir v1.1.0
2727
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
28-
github.com/planetscale/planetscale-go v0.167.0
28+
github.com/planetscale/planetscale-go v0.168.0
2929
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
3030
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
3131
github.com/spf13/cobra v1.10.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
176176
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
177177
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
178178
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
179-
github.com/planetscale/planetscale-go v0.167.0 h1:TqXYCu7Pgcc3wAVD7cNBqZ1zH5+rXrxr6UGYNSV5V44=
180-
github.com/planetscale/planetscale-go v0.167.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
179+
github.com/planetscale/planetscale-go v0.168.0 h1:MZfhTUbZ4DNv2q7WSMF0VqYovGpdkvY40JstZTCPTW4=
180+
github.com/planetscale/planetscale-go v0.168.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
181181
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
182182
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
183183
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=

internal/cmd/branch/vtctld/list_tablets.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
)
1010

1111
func ListTabletsCmd(ch *cmdutil.Helper) *cobra.Command {
12+
var flags struct {
13+
keyspace string
14+
shard string
15+
tabletType string
16+
tabletAliases []string
17+
}
18+
1219
cmd := &cobra.Command{
1320
Use: "list-tablets <database> <branch>",
1421
Short: "List tablets for a branch, grouped by keyspace and shard",
@@ -28,9 +35,13 @@ func ListTabletsCmd(ch *cmdutil.Helper) *cobra.Command {
2835
defer end()
2936

3037
groups, err := client.Vtctld.ListTablets(ctx, &ps.ListBranchTabletsRequest{
31-
Organization: ch.Config.Organization,
32-
Database: database,
33-
Branch: branch,
38+
Organization: ch.Config.Organization,
39+
Database: database,
40+
Branch: branch,
41+
Keyspace: flags.keyspace,
42+
Shard: flags.shard,
43+
TabletType: flags.tabletType,
44+
TabletAliases: flags.tabletAliases,
3445
})
3546
if err != nil {
3647
return cmdutil.HandleError(err)
@@ -41,5 +52,11 @@ func ListTabletsCmd(ch *cmdutil.Helper) *cobra.Command {
4152
},
4253
}
4354

55+
cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Only list tablets in this keyspace")
56+
cmd.Flags().StringVar(&flags.shard, "shard", "", "Only list tablets in this shard (requires --keyspace)")
57+
cmd.Flags().StringVar(&flags.tabletType, "tablet-type", "", "Only list tablets of this type (e.g. primary, replica, rdonly)")
58+
cmd.Flags().StringSliceVar(&flags.tabletAliases, "tablet-alias", nil,
59+
"Only list the tablet(s) with these aliases, e.g. zone1-0000000100 (comma-separated; overrides the other filters)")
60+
4461
return cmd
4562
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package vtctld
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
qt "github.com/frankban/quicktest"
9+
10+
"github.com/planetscale/cli/internal/cmdutil"
11+
"github.com/planetscale/cli/internal/config"
12+
"github.com/planetscale/cli/internal/mock"
13+
"github.com/planetscale/cli/internal/printer"
14+
ps "github.com/planetscale/planetscale-go/planetscale"
15+
)
16+
17+
func TestListTablets(t *testing.T) {
18+
c := qt.New(t)
19+
20+
org := "my-org"
21+
db := "my-db"
22+
branch := "my-branch"
23+
24+
svc := &mock.VtctldService{
25+
ListTabletsFn: func(ctx context.Context, req *ps.ListBranchTabletsRequest) ([]*ps.TabletGroup, error) {
26+
c.Assert(req.Organization, qt.Equals, org)
27+
c.Assert(req.Database, qt.Equals, db)
28+
c.Assert(req.Branch, qt.Equals, branch)
29+
30+
// No filter flags set.
31+
c.Assert(req.Keyspace, qt.Equals, "")
32+
c.Assert(req.Shard, qt.Equals, "")
33+
c.Assert(req.TabletType, qt.Equals, "")
34+
c.Assert(req.TabletAliases, qt.HasLen, 0)
35+
36+
return []*ps.TabletGroup{}, nil
37+
},
38+
}
39+
40+
var buf bytes.Buffer
41+
format := printer.JSON
42+
p := printer.NewPrinter(&format)
43+
p.SetResourceOutput(&buf)
44+
45+
ch := &cmdutil.Helper{
46+
Printer: p,
47+
Config: &config.Config{Organization: org},
48+
Client: func() (*ps.Client, error) {
49+
return &ps.Client{
50+
Vtctld: svc,
51+
}, nil
52+
},
53+
}
54+
55+
cmd := ListTabletsCmd(ch)
56+
cmd.SetArgs([]string{db, branch})
57+
err := cmd.Execute()
58+
c.Assert(err, qt.IsNil)
59+
c.Assert(svc.ListTabletsFnInvoked, qt.IsTrue)
60+
}
61+
62+
func TestListTablets_Filters(t *testing.T) {
63+
c := qt.New(t)
64+
65+
org := "my-org"
66+
db := "my-db"
67+
branch := "my-branch"
68+
69+
svc := &mock.VtctldService{
70+
ListTabletsFn: func(ctx context.Context, req *ps.ListBranchTabletsRequest) ([]*ps.TabletGroup, error) {
71+
c.Assert(req.Keyspace, qt.Equals, "commerce")
72+
c.Assert(req.Shard, qt.Equals, "-80")
73+
c.Assert(req.TabletType, qt.Equals, "replica")
74+
c.Assert(req.TabletAliases, qt.DeepEquals, []string{"zone1-0000000100", "zone1-0000000101"})
75+
76+
return []*ps.TabletGroup{}, nil
77+
},
78+
}
79+
80+
var buf bytes.Buffer
81+
format := printer.JSON
82+
p := printer.NewPrinter(&format)
83+
p.SetResourceOutput(&buf)
84+
85+
ch := &cmdutil.Helper{
86+
Printer: p,
87+
Config: &config.Config{Organization: org},
88+
Client: func() (*ps.Client, error) {
89+
return &ps.Client{
90+
Vtctld: svc,
91+
}, nil
92+
},
93+
}
94+
95+
cmd := ListTabletsCmd(ch)
96+
cmd.SetArgs([]string{
97+
db, branch,
98+
"--keyspace", "commerce",
99+
"--shard=-80",
100+
"--tablet-type", "replica",
101+
"--tablet-alias", "zone1-0000000100,zone1-0000000101",
102+
})
103+
err := cmd.Execute()
104+
c.Assert(err, qt.IsNil)
105+
c.Assert(svc.ListTabletsFnInvoked, qt.IsTrue)
106+
}

0 commit comments

Comments
 (0)