From c8176cf4e6bdea9fcca045574cb2a920d7191135 Mon Sep 17 00:00:00 2001 From: rleungx Date: Fri, 26 Oct 2018 13:07:09 +0800 Subject: [PATCH 1/2] fix region key in detach mode --- tools/pd-ctl/main.go | 1 + tools/pd-ctl/pdctl/command/region_command.go | 50 ++++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/tools/pd-ctl/main.go b/tools/pd-ctl/main.go index df3ed756c27..6b0a8d54259 100644 --- a/tools/pd-ctl/main.go +++ b/tools/pd-ctl/main.go @@ -52,6 +52,7 @@ func main() { if pdAddr != "" { os.Args = append(os.Args, "-u", pdAddr) } + flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true flag.Parse() if version { diff --git a/tools/pd-ctl/pdctl/command/region_command.go b/tools/pd-ctl/pdctl/command/region_command.go index 12e2ddd6774..014dda153a8 100644 --- a/tools/pd-ctl/pdctl/command/region_command.go +++ b/tools/pd-ctl/pdctl/command/region_command.go @@ -18,6 +18,7 @@ import ( "fmt" "io" "net/http" + "net/url" "os/exec" "strconv" @@ -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] ", + Use: "key [--format=raw|encode] ", Short: "show the region with key", Run: showRegionWithTableCommandFunc, } @@ -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 @@ -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 { @@ -255,13 +254,35 @@ 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 } @@ -269,7 +290,7 @@ func decodeProtobufText(text string) (string, error) { // NewRegionsWithStartKeyCommand returns regions from startkey subcommand of regionCmd. func NewRegionsWithStartKeyCommand() *cobra.Command { r := &cobra.Command{ - Use: "startkey [--format=raw|pb|proto|protobuf] ", + Use: "startkey [--format=raw|encode] ", Short: "show regions from start key", Run: showRegionsFromStartKeyCommandFunc, } @@ -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 @@ -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") From ee9261ba85cf11cd77f6f52022fae8715720afac Mon Sep 17 00:00:00 2001 From: rleungx Date: Fri, 26 Oct 2018 15:01:47 +0800 Subject: [PATCH 2/2] remove escape key --- tools/pd-ctl/pdctl/command/region_command.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/pd-ctl/pdctl/command/region_command.go b/tools/pd-ctl/pdctl/command/region_command.go index 014dda153a8..54858876ccf 100644 --- a/tools/pd-ctl/pdctl/command/region_command.go +++ b/tools/pd-ctl/pdctl/command/region_command.go @@ -18,7 +18,6 @@ import ( "fmt" "io" "net/http" - "net/url" "os/exec" "strconv" @@ -234,7 +233,8 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) { fmt.Println("Error: unknown format") return } - prefix := regionKeyPrefix + "/" + url.QueryEscape(key) + // TODO: Deal with path escaped + prefix := regionKeyPrefix + "/" + key r, err := doRequest(cmd, prefix, http.MethodGet) if err != nil { fmt.Printf("Failed to get region: %s\n", err) @@ -324,7 +324,8 @@ func showRegionsFromStartKeyCommandFunc(cmd *cobra.Command, args []string) { fmt.Println("Error: unknown format") return } - prefix := regionKeyPrefix + "/" + url.QueryEscape(key) + // TODO: Deal with path escaped + prefix := regionKeyPrefix + "/" + key if len(args) == 2 { if _, err = strconv.Atoi(args[1]); err != nil { fmt.Println("limit should be a number")