From 1a394d6a57de6883b6b3f16343248314b8e5bf9d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 14 May 2024 14:25:55 +0530 Subject: [PATCH] Allow rc commands to control the exit code --- tools/cmd/at/main.go | 10 ++++++++++ tools/cmd/at/shell.go | 1 + tools/cmd/at/template.go | 3 +++ 3 files changed, 14 insertions(+) diff --git a/tools/cmd/at/main.go b/tools/cmd/at/main.go index 4201b1fd8..cc684bfd6 100644 --- a/tools/cmd/at/main.go +++ b/tools/cmd/at/main.go @@ -247,6 +247,16 @@ func get_response(do_io func(io_data *rc_io_data) ([]byte, error), io_data *rc_i return } +var running_shell = false + +type exit_error struct { + exit_code int +} + +func (m *exit_error) Error() string { + return fmt.Sprintf("Subprocess exit with code: %d", m.exit_code) +} + func send_rc_command(io_data *rc_io_data) (err error) { err = setup_global_options(io_data.cmd) if err != nil { diff --git a/tools/cmd/at/shell.go b/tools/cmd/at/shell.go index e31a89b64..aeb8bda4c 100644 --- a/tools/cmd/at/shell.go +++ b/tools/cmd/at/shell.go @@ -203,6 +203,7 @@ func shell_main(cmd *cli.Command, args []string) (int, error) { if err != nil { return 1, err } + running_shell = true formatter = markup.New(true) fmt.Println("Welcome to the kitty shell!") fmt.Println("Use", formatter.Green("help"), "for assistance or", formatter.Green("exit"), "to quit.") diff --git a/tools/cmd/at/template.go b/tools/cmd/at/template.go index 2327c1304..5605393ea 100644 --- a/tools/cmd/at/template.go +++ b/tools/cmd/at/template.go @@ -90,6 +90,9 @@ func run_CMD_NAME(cmd *cli.Command, args []string) (return_code int, err error) } err = send_rc_command(&io_data) + if ee, ok := err.(*exit_error); ok && !running_shell { + return ee.exit_code, nil + } return }