Skip to content

Commit

Permalink
use a right way to decode key
Browse files Browse the repository at this point in the history
  • Loading branch information
rleungx committed Oct 26, 2018
1 parent 9c3ed7d commit eb52a3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 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
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

0 comments on commit eb52a3a

Please sign in to comment.