From 8bc9675a923f81f79d8a566e208c8afdcf4ea3f3 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 18 Jun 2021 14:30:38 +0800 Subject: [PATCH] pd-ctl: try to fix interactive mode (#3782) (#3784) * pd-ctl: fix interactive mode Signed-off-by: xhe * pd-ctl: remove unnecessary modifications Signed-off-by: xhe * pd-ctl: hidden unnecessary flags Signed-off-by: xhe * pd-ctl: revert exit on error Signed-off-by: xhe * pd-ctl: remove unused Signed-off-by: xhe Co-authored-by: xhe Co-authored-by: ShuNing --- tools/pd-ctl/pdctl/ctl.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/tools/pd-ctl/pdctl/ctl.go b/tools/pd-ctl/pdctl/ctl.go index 88bbf318519..821e1937181 100644 --- a/tools/pd-ctl/pdctl/ctl.go +++ b/tools/pd-ctl/pdctl/ctl.go @@ -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 } @@ -109,7 +106,8 @@ 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) } } @@ -117,15 +115,13 @@ func MainStart(args []string) { 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", @@ -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() @@ -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) } } }