-
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 1 commit
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 |
---|---|---|
|
@@ -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 { | ||
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 |
||
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 |
---|---|---|
|
@@ -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 | ||
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. why return 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. this string will print. 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. why not check error and print the error directly? |
||
} | ||
rootCmd.SetUsageTemplate(command.UsageTemplate) | ||
if err := rootCmd.Execute(); err != nil { | ||
return rootCmd.UsageString(), err | ||
|
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.