Allow rc commands to control the exit code

This commit is contained in:
Kovid Goyal
2024-05-14 14:25:55 +05:30
parent f659342b48
commit 1a394d6a57
3 changed files with 14 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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.")

View File

@@ -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
}