Skip to content

Commit

Permalink
pd-ctl: try to fix interactive mode (#3782) (#3784)
Browse files Browse the repository at this point in the history
* pd-ctl: fix interactive mode

Signed-off-by: xhe <xw897002528@gmail.com>

* pd-ctl: remove unnecessary modifications

Signed-off-by: xhe <xw897002528@gmail.com>

* pd-ctl: hidden unnecessary flags

Signed-off-by: xhe <xw897002528@gmail.com>

* pd-ctl: revert exit on error

Signed-off-by: xhe <xw897002528@gmail.com>

* pd-ctl: remove unused

Signed-off-by: xhe <xw897002528@gmail.com>

Co-authored-by: xhe <xw897002528@gmail.com>
Co-authored-by: ShuNing <nolouch@gmail.com>
  • Loading branch information
3 people authored Jun 18, 2021
1 parent 2258e38 commit 8bc9675
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions tools/pd-ctl/pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import (
"github.com/chzyer/readline"
"github.com/mattn/go-shellwords"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tikv/pd/server"
"github.com/tikv/pd/tools/pd-ctl/pdctl/command"
)

var (
readlineCompleter *readline.PrefixCompleter
)

func init() {
cobra.EnablePrefixMatching = true
}
Expand Down Expand Up @@ -109,23 +106,22 @@ func MainStart(args []string) {
return
}
if v, err := cmd.Flags().GetBool("interact"); err == nil && v {
loop()
readlineCompleter := readline.NewPrefixCompleter(genCompleter(cmd)...)
loop(cmd.PersistentFlags(), readlineCompleter)
}
}

rootCmd.SetArgs(args)
rootCmd.ParseFlags(args)
rootCmd.SetOutput(os.Stdout)

readlineCompleter = readline.NewPrefixCompleter(genCompleter(rootCmd)...)

if err := rootCmd.Execute(); err != nil {
rootCmd.Println(err)
os.Exit(1)
}
}

func loop() {
func loop(persistentFlags *pflag.FlagSet, readlineCompleter readline.AutoCompleter) {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
Expand All @@ -139,8 +135,20 @@ func loop() {
}
defer l.Close()

rootCmd := GetRootCmd()
rootCmd.SetOutput(os.Stdout)
getREPLCmd := func() *cobra.Command {
rootCmd := GetRootCmd()
persistentFlags.VisitAll(func(flag *pflag.Flag) {
if flag.Changed {
rootCmd.PersistentFlags().Set(flag.Name, flag.Value.String())
}
})
rootCmd.LocalFlags().MarkHidden("pd")
rootCmd.LocalFlags().MarkHidden("cacert")
rootCmd.LocalFlags().MarkHidden("cert")
rootCmd.LocalFlags().MarkHidden("key")
rootCmd.SetOutput(os.Stdout)
return rootCmd
}

for {
line, err := l.Readline()
Expand All @@ -161,12 +169,11 @@ func loop() {
continue
}

rootCmd := getREPLCmd()
rootCmd.SetArgs(args)
rootCmd.ParseFlags(args)

if err := rootCmd.Execute(); err != nil {
rootCmd.Println(err)
os.Exit(1)
}
}
}
Expand Down

0 comments on commit 8bc9675

Please sign in to comment.