Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdctl: fix region key problems in detach mode #1298

Merged
merged 3 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/pd-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
if pdAddr != "" {
os.Args = append(os.Args, "-u", pdAddr)
}
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.Parse()

if version {
Expand Down
43 changes: 32 additions & 11 deletions tools/pd-ctl/pdctl/command/region_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func showRegionTopSizeCommandFunc(cmd *cobra.Command, args []string) {
// NewRegionWithKeyCommand return a region with key subcommand of regionCmd
func NewRegionWithKeyCommand() *cobra.Command {
r := &cobra.Command{
Use: "key [--format=raw|pb|proto|protobuf] <key>",
Use: "key [--format=raw|encode] <key>",
Short: "show the region with key",
Run: showRegionWithTableCommandFunc,
}
Expand All @@ -223,8 +223,8 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
switch format {
case "raw":
key = args[0]
case "pb", "proto", "protobuf":
key, err = decodeProtobufText(args[0])
case "encode":
key, err = decodeKey(args[0])
if err != nil {
fmt.Println("Error: ", err)
return
Expand All @@ -241,10 +241,9 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
return
}
fmt.Println(r)

}

func decodeProtobufText(text string) (string, error) {
func decodeKey(text string) (string, error) {
var buf []byte
r := bytes.NewBuffer([]byte(text))
for {
Expand All @@ -255,21 +254,43 @@ func decodeProtobufText(text string) (string, error) {
}
break
}
if c == '\\' {
_, err := fmt.Sscanf(string(r.Next(3)), "%03o", &c)
if c != '\\' {
buf = append(buf, c)
continue
}
n := r.Next(1)
switch n[0] {
case '"':
buf = append(buf, '"')
case '\'':
buf = append(buf, '\'')
case '\\':
buf = append(buf, '\\')
case 'n':
buf = append(buf, '\n')
case 't':
buf = append(buf, '\t')
case 'r':
buf = append(buf, '\r')
case 'x':
fmt.Sscanf(string(r.Next(2)), "%02x", &c)
buf = append(buf, c)
default:
n = append(n, r.Next(2)...)
_, err := fmt.Sscanf(string(n), "%03o", &c)
if err != nil {
return "", err
}
buf = append(buf, c)
}
buf = append(buf, c)
}
return string(buf), nil
}

// NewRegionsWithStartKeyCommand returns regions from startkey subcommand of regionCmd.
func NewRegionsWithStartKeyCommand() *cobra.Command {
r := &cobra.Command{
Use: "startkey [--format=raw|pb|proto|protobuf] <key> <limit>",
Use: "startkey [--format=raw|encode] <key> <limit>",
Short: "show regions from start key",
Run: showRegionsFromStartKeyCommandFunc,
}
Expand All @@ -293,8 +314,8 @@ func showRegionsFromStartKeyCommandFunc(cmd *cobra.Command, args []string) {
switch format {
case "raw":
key = args[0]
case "pb", "proto", "protobuf":
key, err = decodeProtobufText(args[0])
case "encode":
key, err = decodeKey(args[0])
if err != nil {
fmt.Println("Error: ", err)
return
Expand Down