Skip to content

Commit

Permalink
Refine the code
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Feb 5, 2024
1 parent 829db7e commit e1c5dc0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tools/pd-ctl/pdctl/command/cluster_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewClusterCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "show the cluster information",
Run: showClusterCommandFunc,
PersistentPreRunE: requirePDClient,
Run: showClusterCommandFunc,
}
cmd.AddCommand(NewClusterStatusCommand())
return cmd
Expand Down
37 changes: 27 additions & 10 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

const (
pdControlCallerID = "pd-ctl"
pingPrefix = "pd/api/v1/ping"
clusterPrefix = "pd/api/v1/cluster"
)

Expand Down Expand Up @@ -70,9 +69,9 @@ func requirePDClient(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
return InitNewPDClientWithTLS(cmd, caPath, certPath, keyPath)
return initNewPDClientWithTLS(cmd, caPath, certPath, keyPath)
}
return InitNewPDClient(cmd)
return initNewPDClient(cmd)
}

// shouldInitPDClient checks whether we should create a new PD client according to the cluster information.
Expand Down Expand Up @@ -101,8 +100,7 @@ func shouldInitPDClient(cmd *cobra.Command) (bool, error) {
return currentClusterInfo.GetId() == 0 || newClusterInfo.GetId() != currentClusterInfo.GetId(), nil
}

// InitNewPDClient creates a PD HTTP client with the given PD addresses.
func InitNewPDClient(cmd *cobra.Command, opts ...pd.ClientOption) error {
func initNewPDClient(cmd *cobra.Command, opts ...pd.ClientOption) error {
if should, err := shouldInitPDClient(cmd); !should || err != nil {
return err
}
Expand All @@ -113,13 +111,12 @@ func InitNewPDClient(cmd *cobra.Command, opts ...pd.ClientOption) error {
return nil
}

// InitNewPDClientWithTLS creates a PD HTTP client with the given PD addresses and TLS config.
func InitNewPDClientWithTLS(cmd *cobra.Command, caPath, certPath, keyPath string) error {
func initNewPDClientWithTLS(cmd *cobra.Command, caPath, certPath, keyPath string) error {
tlsConfig, err := initTLSConfig(caPath, certPath, keyPath)
if err != nil {
return err
}
InitNewPDClient(cmd, pd.WithTLSConfig(tlsConfig))
initNewPDClient(cmd, pd.WithTLSConfig(tlsConfig))
return nil
}

Expand All @@ -128,8 +125,28 @@ var dialClient = &http.Client{
Transport: apiutil.NewCallerIDRoundTripper(http.DefaultTransport, pdControlCallerID),
}

// InitHTTPSClient creates https client with ca file
func InitHTTPSClient(caPath, certPath, keyPath string) error {
// RequireHTTPSClient creates a HTTPS client if the related flags are set
func RequireHTTPSClient(cmd *cobra.Command, args []string) error {
caPath, err := cmd.Flags().GetString("cacert")
if err == nil && len(caPath) != 0 {
certPath, err := cmd.Flags().GetString("cert")
if err != nil {
return err
}
keyPath, err := cmd.Flags().GetString("key")
if err != nil {
return err
}
err = initHTTPSClient(caPath, certPath, keyPath)
if err != nil {
cmd.Println(err)
return err
}
}
return nil
}

func initHTTPSClient(caPath, certPath, keyPath string) error {
tlsConfig, err := initTLSConfig(caPath, certPath, keyPath)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions tools/pd-ctl/pdctl/command/ping_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/spf13/cobra"
)

const pingPrefix = "pd/api/v1/ping"

// NewPingCommand return a ping subcommand of rootCmd
func NewPingCommand() *cobra.Command {
m := &cobra.Command{
Expand Down
31 changes: 6 additions & 25 deletions tools/pd-ctl/pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ func init() {
// GetRootCmd is exposed for integration tests. But it can be embedded into another suite, too.
func GetRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "pd-ctl",
Short: "Placement Driver control",
Use: "pd-ctl",
Short: "Placement Driver control",
PersistentPreRunE: command.RequireHTTPSClient,
SilenceErrors: true,
}

rootCmd.PersistentFlags().StringP("pd", "u", "http://127.0.0.1:2379", "address of PD")
rootCmd.PersistentFlags().String("cacert", "", "path of file that contains list of trusted SSL CAs")
rootCmd.PersistentFlags().String("cert", "", "path of file that contains X509 certificate in PEM format")
rootCmd.PersistentFlags().String("key", "", "path of file that contains X509 key in PEM format")

rootCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true

rootCmd.AddCommand(
command.NewConfigCommand(),
command.NewRegionCommand(),
Expand All @@ -70,29 +74,6 @@ func GetRootCmd() *cobra.Command {
command.NewResourceManagerCommand(),
)

rootCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true
rootCmd.SilenceErrors = true

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
caPath, err := cmd.Flags().GetString("cacert")
if err == nil && len(caPath) != 0 {
certPath, err := cmd.Flags().GetString("cert")
if err != nil {
return err
}
keyPath, err := cmd.Flags().GetString("key")
if err != nil {
return err
}
err = command.InitHTTPSClient(caPath, certPath, keyPath)
if err != nil {
rootCmd.Println(err)
return err
}
}
return nil
}

return rootCmd
}

Expand Down

0 comments on commit e1c5dc0

Please sign in to comment.