From c0ec6b85af677fa71ed6616361578b5f30799638 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 17 Feb 2017 15:43:29 +0800 Subject: [PATCH 1/6] pdctl: verify pd address --- cmd/pd-ctl/main.go | 4 +++- pdctl/command/global.go | 30 +++++++++++++++++++++++++++++- pdctl/ctl.go | 5 ++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/pd-ctl/main.go b/cmd/pd-ctl/main.go index 878f097ca1a..b9cc760205b 100644 --- a/cmd/pd-ctl/main.go +++ b/cmd/pd-ctl/main.go @@ -87,7 +87,9 @@ func loop() { } continue } - + if line == "exit" { + os.Exit(0) + } args := strings.Split(strings.TrimSpace(line), " ") args = append(args, "-u", url) usage, err := pdctl.Start(args) diff --git a/pdctl/command/global.go b/pdctl/command/global.go index c2315917c5b..51eb05bc084 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -26,7 +26,12 @@ import ( "github.com/spf13/cobra" ) -var pdClient pd.Client +var ( + pdClient pd.Client + + pingPrifix = "pd/ping" + errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it") +) // InitPDClient initialize pd client from cmd func InitPDClient(cmd *cobra.Command) error { @@ -38,6 +43,10 @@ func InitPDClient(cmd *cobra.Command) error { if pdClient != nil { return nil } + err = validPDAddr(addr) + if err != nil { + return err + } pdClient, err = pd.NewClient([]string{addr}) if err != nil { return err @@ -75,6 +84,25 @@ func printResponseError(r *http.Response) { io.Copy(os.Stdout, r.Body) } +func validPDAddr(pd string) error { + u, err := url.Parse(pd) + if err != nil { + return err + } + if u.Scheme == "" { + u.Scheme = "http" + } + addr := u.String() + reps, err := http.Get(fmt.Sprintf("%s/%s", addr, pingPrifix)) + if err != nil { + return err + } + if reps.StatusCode != http.StatusOK { + return errInvalidAddr + } + return nil +} + // UsageTemplate will used to generate a help information const UsageTemplate = `Usage:{{if .Runnable}} {{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine ""}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}} diff --git a/pdctl/ctl.go b/pdctl/ctl.go index d92ac496e86..b60e0b76d07 100644 --- a/pdctl/ctl.go +++ b/pdctl/ctl.go @@ -48,7 +48,10 @@ func Start(args []string) (string, error) { rootCmd.SetArgs(args) rootCmd.SilenceErrors = true rootCmd.ParseFlags(args) - command.InitPDClient(rootCmd) + err := command.InitPDClient(rootCmd) + if err != nil { + return err.Error(), err + } rootCmd.SetUsageTemplate(command.UsageTemplate) if err := rootCmd.Execute(); err != nil { return rootCmd.UsageString(), err From 57bf536fd42fba36207abab8c282c2cb307e0ab2 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 17 Feb 2017 16:30:46 +0800 Subject: [PATCH 2/6] pdctl: address comment --- pdctl/command/global.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pdctl/command/global.go b/pdctl/command/global.go index 51eb05bc084..4b162f76cce 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -16,6 +16,7 @@ package command import ( "fmt" "io" + "io/ioutil" "net/http" "net/url" "os" @@ -97,7 +98,9 @@ func validPDAddr(pd string) error { if err != nil { return err } + defer reps.Body.Close() if reps.StatusCode != http.StatusOK { + ioutil.ReadAll(reps.Body) return errInvalidAddr } return nil From afa7d3ee671da20fee45147d3bf90eae27ee4d5b Mon Sep 17 00:00:00 2001 From: nolouch Date: Sat, 18 Feb 2017 11:49:09 +0800 Subject: [PATCH 3/6] pdctl: add label command --- cmd/pd-ctl/main.go | 5 +---- pdctl/ctl.go | 10 ++++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/pd-ctl/main.go b/cmd/pd-ctl/main.go index b9cc760205b..e713ceebac0 100644 --- a/cmd/pd-ctl/main.go +++ b/cmd/pd-ctl/main.go @@ -92,9 +92,6 @@ func loop() { } args := strings.Split(strings.TrimSpace(line), " ") args = append(args, "-u", url) - usage, err := pdctl.Start(args) - if err != nil { - fmt.Println(usage) - } + pdctl.Start(args) } } diff --git a/pdctl/ctl.go b/pdctl/ctl.go index b60e0b76d07..055f2482377 100644 --- a/pdctl/ctl.go +++ b/pdctl/ctl.go @@ -14,6 +14,8 @@ package pdctl import ( + "fmt" + "github.com/pingcap/pd/pdctl/command" "github.com/spf13/cobra" ) @@ -44,17 +46,17 @@ func init() { } // Start run Command -func Start(args []string) (string, error) { +func Start(args []string) { rootCmd.SetArgs(args) rootCmd.SilenceErrors = true rootCmd.ParseFlags(args) err := command.InitPDClient(rootCmd) if err != nil { - return err.Error(), err + fmt.Println(err) + return } rootCmd.SetUsageTemplate(command.UsageTemplate) if err := rootCmd.Execute(); err != nil { - return rootCmd.UsageString(), err + fmt.Println(rootCmd.UsageString()) } - return "", nil } From 6dba82508d80fc6f5e78897b934626a578570536 Mon Sep 17 00:00:00 2001 From: nolouch Date: Tue, 21 Feb 2017 15:41:31 +0800 Subject: [PATCH 4/6] pdctl: address comment --- pdctl/command/global.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdctl/command/global.go b/pdctl/command/global.go index 965efdb2ae8..5ec733634e8 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -132,8 +132,8 @@ func validPDAddr(pd string) error { return err } defer reps.Body.Close() + ioutil.ReadAll(reps.Body) if reps.StatusCode != http.StatusOK { - ioutil.ReadAll(reps.Body) return errInvalidAddr } return nil From 066ecc5daf7de0929fa79a69b19024f3ff449398 Mon Sep 17 00:00:00 2001 From: nolouch Date: Tue, 21 Feb 2017 15:49:15 +0800 Subject: [PATCH 5/6] typo --- pdctl/command/global.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdctl/command/global.go b/pdctl/command/global.go index 5ec733634e8..d31e07ebdc8 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -31,7 +31,7 @@ var ( pdClient pd.Client dailClient = &http.Client{} - pingPrifix = "pd/ping" + pingPrefix = "pd/ping" errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it") ) From bb88fe37abd92bc17b6159aac090139628a1424a Mon Sep 17 00:00:00 2001 From: nolouch Date: Tue, 21 Feb 2017 15:55:53 +0800 Subject: [PATCH 6/6] fix ci --- pdctl/command/global.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdctl/command/global.go b/pdctl/command/global.go index d31e07ebdc8..f65aee3ea64 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -127,7 +127,7 @@ func validPDAddr(pd string) error { u.Scheme = "http" } addr := u.String() - reps, err := http.Get(fmt.Sprintf("%s/%s", addr, pingPrifix)) + reps, err := http.Get(fmt.Sprintf("%s/%s", addr, pingPrefix)) if err != nil { return err }