Skip to content

Commit

Permalink
dmctl/:Clear workers cache before we run a command (pingcap#365)
Browse files Browse the repository at this point in the history
* clear workers and new rootCmd every time before we start a subCommand
  • Loading branch information
lichunzhu authored Nov 20, 2019
1 parent e37b039 commit 788c878
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions dm/ctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,32 @@ import (

var (
commandMasterFlags = CommandMasterFlags{}
rootCmd = &cobra.Command{
Use: "dmctl",
Short: "DM control",
}
rootCmd *cobra.Command
)

// CommandMasterFlags are flags that used in all commands for dm-master
type CommandMasterFlags struct {
workers []string // specify workers to control on these dm-workers
}

// Reset clears cache of CommandMasterFlags
func (c CommandMasterFlags) Reset() {
c.workers = c.workers[:0]
}

func init() {
rootCmd = NewRootCmd()
}

// NewRootCmd generates a new rootCmd
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dmctl",
Short: "DM control",
}
// --worker worker1 -w worker2 --worker=worker3,worker4 -w=worker5,worker6
rootCmd.PersistentFlags().StringSliceVarP(&commandMasterFlags.workers, "worker", "w", []string{}, "DM-worker ID")
rootCmd.AddCommand(
cmd.PersistentFlags().StringSliceVarP(&commandMasterFlags.workers, "worker", "w", []string{}, "DM-worker ID")
cmd.AddCommand(
master.NewStartTaskCmd(),
master.NewStopTaskCmd(),
master.NewPauseTaskCmd(),
Expand All @@ -66,6 +77,7 @@ func init() {
master.NewPurgeRelayCmd(),
master.NewMigrateRelayCmd(),
)
return cmd
}

// Init initializes dm-control
Expand Down Expand Up @@ -116,6 +128,8 @@ func PrintHelp(args []string) {

// Start starts running a command
func Start(args []string) {
commandMasterFlags.Reset()
rootCmd = NewRootCmd()
rootCmd.SetArgs(args)
if err := rootCmd.Execute(); err != nil {
fmt.Println(rootCmd.UsageString())
Expand Down

0 comments on commit 788c878

Please sign in to comment.