Skip to content

Commit

Permalink
fix region key in detach mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rleungx committed Oct 26, 2018
1 parent 942a729 commit c8176cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
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
50 changes: 35 additions & 15 deletions tools/pd-ctl/pdctl/command/region_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os/exec"
"strconv"

Expand Down Expand Up @@ -200,7 +201,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 +224,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 @@ -233,18 +234,16 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("Error: unknown format")
return
}
// TODO: Deal with path escaped
prefix := regionKeyPrefix + "/" + key
prefix := regionKeyPrefix + "/" + url.QueryEscape(key)
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get region: %s\n", err)
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 All @@ -303,8 +324,7 @@ func showRegionsFromStartKeyCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("Error: unknown format")
return
}
// TODO: Deal with path escaped
prefix := regionKeyPrefix + "/" + key
prefix := regionKeyPrefix + "/" + url.QueryEscape(key)
if len(args) == 2 {
if _, err = strconv.Atoi(args[1]); err != nil {
fmt.Println("limit should be a number")
Expand Down

0 comments on commit c8176cf

Please sign in to comment.