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 21dce03
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 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
36 changes: 27 additions & 9 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,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 +101,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 +112,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 +126,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
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 21dce03

Please sign in to comment.