From 10cef1621092639e3fee48810fb66a5caca6b52f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Aug 2022 10:35:32 +0530 Subject: [PATCH] DRYer --- tools/cmd/at/main.go | 16 +++++++++------- tools/cmd/at/template.go | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/cmd/at/main.go b/tools/cmd/at/main.go index 99b971a05..86a141b25 100644 --- a/tools/cmd/at/main.go +++ b/tools/cmd/at/main.go @@ -222,7 +222,11 @@ func get_response(rc *utils.RemoteControlCmd, timeout float64) (ans *Response, e return } -func send_rc_command(rc *utils.RemoteControlCmd, timeout float64, string_response_is_err bool) (err error) { +func send_rc_command(cmd *cobra.Command, rc *utils.RemoteControlCmd, timeout float64, string_response_is_err bool) (err error) { + err = setup_global_options(cmd) + if err != nil { + return err + } response, err := get_response(rc, timeout) if err != nil || response == nil { return err @@ -320,7 +324,7 @@ func add_global_options(fs *pflag.FlagSet) { } -func setup_global_options(cmd *cobra.Command, args []string) (err error) { +func setup_global_options(cmd *cobra.Command) (err error) { var v = cli.FlagValGetter{Flags: cmd.Flags()} to := v.String("to") password := v.String("password") @@ -350,10 +354,9 @@ func setup_global_options(cmd *cobra.Command, args []string) (err error) { func EntryPoint(tool_root *cobra.Command) *cobra.Command { at_root_command := cli.CreateCommand(&cobra.Command{ - Use: "@ [global options] command [command options] [command args]", - Short: "Control kitty remotely", - Long: "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` or use a password, for this to work.", - PersistentPreRunE: setup_global_options, + Use: "@ [global options] command [command options] [command args]", + Short: "Control kitty remotely", + Long: "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` or use a password, for this to work.", }) at_root_command.Annotations["options_title"] = "Global options" add_global_options(at_root_command.PersistentFlags()) @@ -366,7 +369,6 @@ func EntryPoint(tool_root *cobra.Command) *cobra.Command { alias.Use = "@" + alias.Use alias.Hidden = true add_global_options(alias.Flags()) - alias.PersistentPreRunE = setup_global_options tool_root.AddCommand(&alias) } return at_root_command diff --git a/tools/cmd/at/template.go b/tools/cmd/at/template.go index 3104d31aa..4be47275b 100644 --- a/tools/cmd/at/template.go +++ b/tools/cmd/at/template.go @@ -59,7 +59,7 @@ func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) { if err == nil { timeout = rt } - err = send_rc_command(rc, timeout, STRING_RESPONSE_IS_ERROR) + err = send_rc_command(cmd, rc, timeout, STRING_RESPONSE_IS_ERROR) return }