|
| 1 | +package vtctld |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/planetscale/cli/internal/cmdutil" |
| 7 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +// RefreshStateByShardCmd reloads tablet records for all tablets in a shard via vtctld. |
| 12 | +func RefreshStateByShardCmd(ch *cmdutil.Helper) *cobra.Command { |
| 13 | + var flags struct { |
| 14 | + keyspace string |
| 15 | + shard string |
| 16 | + cells []string |
| 17 | + } |
| 18 | + |
| 19 | + cmd := &cobra.Command{ |
| 20 | + Use: "refresh-state-by-shard <database> <branch>", |
| 21 | + Short: "Reload tablet records for all tablets in a shard", |
| 22 | + Long: "Reload the tablet record for all tablets in a shard via vtctld, " + |
| 23 | + "optionally limited to the specified cells.", |
| 24 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 25 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 26 | + ctx := cmd.Context() |
| 27 | + database, branch := args[0], args[1] |
| 28 | + |
| 29 | + if flags.keyspace == "" { |
| 30 | + return fmt.Errorf("keyspace is required") |
| 31 | + } |
| 32 | + if flags.shard == "" { |
| 33 | + return fmt.Errorf("shard is required") |
| 34 | + } |
| 35 | + |
| 36 | + client, err := ch.Client() |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + end := ch.Printer.PrintProgress( |
| 42 | + fmt.Sprintf("Refreshing tablet state for %s/%s on %s…", |
| 43 | + flags.keyspace, flags.shard, |
| 44 | + progressTarget(ch.Config.Organization, database, branch))) |
| 45 | + defer end() |
| 46 | + |
| 47 | + data, err := client.Vtctld.RefreshStateByShard(ctx, &ps.VtctldRefreshStateByShardRequest{ |
| 48 | + Organization: ch.Config.Organization, |
| 49 | + Database: database, |
| 50 | + Branch: branch, |
| 51 | + Keyspace: flags.keyspace, |
| 52 | + Shard: flags.shard, |
| 53 | + Cells: flags.cells, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + return cmdutil.HandleError(err) |
| 57 | + } |
| 58 | + |
| 59 | + end() |
| 60 | + return ch.Printer.PrettyPrintJSON(data) |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name") |
| 65 | + cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard name (e.g. \"-\" for unsharded)") |
| 66 | + cmd.Flags().StringSliceVar(&flags.cells, "cells", nil, "Cells to refresh (comma-separated)") |
| 67 | + cmd.MarkFlagRequired("keyspace") |
| 68 | + cmd.MarkFlagRequired("shard") |
| 69 | + |
| 70 | + return cmd |
| 71 | +} |
0 commit comments