-
Notifications
You must be signed in to change notification settings - Fork 725
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: verify pd address #528
Changes from 4 commits
c0ec6b8
57bf536
afa7d3e
907cf38
6dba825
066ecc5
bb88fe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,9 @@ import ( | |
var ( | ||
pdClient pd.Client | ||
dailClient = &http.Client{} | ||
|
||
pingPrifix = "pd/ping" | ||
errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it") | ||
) | ||
|
||
func doRequest(cmd *cobra.Command, prefix string, method string) (string, error) { | ||
|
@@ -74,6 +77,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 | ||
|
@@ -111,6 +118,27 @@ 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 | ||
} | ||
defer reps.Body.Close() | ||
if reps.StatusCode != http.StatusOK { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. must read all response body and close |
||
ioutil.ReadAll(reps.Body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to read it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If not read, the connection FD will not close even you call body close later. I remember this is a pitfall before, but don't know go improves it or not. Btw, We should call |
||
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}} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
package pdctl | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pingcap/pd/pdctl/command" | ||
"github.com/spf13/cobra" | ||
) | ||
|
@@ -44,14 +47,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) | ||
command.InitPDClient(rootCmd) | ||
err := command.InitPDClient(rootCmd) | ||
if err != nil { | ||
fmt.Println(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should exit here directly? |
||
os.Exit(1) | ||
} | ||
rootCmd.SetUsageTemplate(command.UsageTemplate) | ||
if err := rootCmd.Execute(); err != nil { | ||
return rootCmd.UsageString(), err | ||
fmt.Println(rootCmd.UsageString()) | ||
} | ||
return "", nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split it to another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
InitPDClient
meet error, it will return error directly without execute the command. so I moveexit
to here.