Skip to content
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

Merged
merged 7 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/pd-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func loop() {
}
continue
}

if line == "exit" {
Copy link
Contributor

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

Copy link
Contributor Author

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 move exit to here.

os.Exit(0)
}
args := strings.Split(strings.TrimSpace(line), " ")
args = append(args, "-u", url)
usage, err := pdctl.Start(args)
Expand Down
30 changes: 29 additions & 1 deletion pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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}}
Expand Down
5 changes: 4 additions & 1 deletion pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return err.Error() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this string will print.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down