diff --git a/src/cli.rs b/src/cli.rs index 4d4a9fe..1613c7a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -90,6 +90,10 @@ pub struct Record { #[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)] pub window_size: Option<(Option, Option)>, + /// Return session exit status + #[arg(long)] + pub return_: bool, + #[arg(long, hide = true)] pub cols: Option, @@ -161,6 +165,10 @@ pub struct Stream { #[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)] pub window_size: Option<(Option, Option)>, + /// Return session exit status + #[arg(long)] + pub return_: bool, + /// Log file path #[arg(long, value_name = "PATH")] pub log_file: Option, @@ -224,6 +232,10 @@ pub struct Session { #[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)] pub window_size: Option<(Option, Option)>, + /// Return session exit status + #[arg(long)] + pub return_: bool, + /// Log file path #[arg(long, value_name = "PATH")] pub log_file: Option, diff --git a/src/cmd/session.rs b/src/cmd/session.rs index dbd6787..0a0d041 100644 --- a/src/cmd/session.rs +++ b/src/cmd/session.rs @@ -4,6 +4,7 @@ use std::fs::{self, OpenOptions}; use std::io::LineWriter; use std::net::TcpListener; use std::path::Path; +use std::process::ExitCode; use std::time::Duration; use anyhow::{anyhow, bail, Context, Result}; @@ -33,7 +34,7 @@ use crate::stream::Stream; use crate::tty::{DevTty, FixedSizeTty, NullTty, Tty}; impl cli::Session { - pub fn run(mut self) -> Result<()> { + pub fn run(mut self) -> Result { locale::check_utf8_locale()?; let config = Config::new(self.server_url.clone())?; @@ -160,11 +161,11 @@ impl cli::Session { let exec_command = build_exec_command(command.as_ref().cloned()); let exec_extra_env = build_exec_extra_env(relay_id.as_ref()); - { + let (exit_status, _) = { let starter = SessionStarter::new(outputs, record_input, keys, notifier); let mut tty = self.get_tty(true)?; - pty::exec(&exec_command, &exec_extra_env, &mut tty, starter)?; - } + pty::exec(&exec_command, &exec_extra_env, &mut tty, starter)? + }; runtime.block_on(async { debug!("session shutting down..."); @@ -185,7 +186,13 @@ impl cli::Session { status::info!("asciinema session ended"); - Ok(()) + if !self.return_ || exit_status == 0 { + Ok(ExitCode::from(0)) + } else if exit_status > 0 { + Ok(ExitCode::from(exit_status as u8)) + } else { + Ok(ExitCode::from(1)) + } } fn get_file_writer( diff --git a/src/main.rs b/src/main.rs index 3a4597e..ee9a16b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,11 +22,13 @@ mod stream; mod tty; mod util; +use std::process::{ExitCode, Termination}; + use clap::Parser; use self::cli::{Cli, Commands, Session}; -fn main() -> anyhow::Result<()> { +fn main() -> ExitCode { let cli = Cli::parse(); if cli.quiet { @@ -51,11 +53,12 @@ fn main() -> anyhow::Result<()> { window_size: cmd.window_size, stream_local: None, stream_remote: None, + return_: cmd.return_, log_file: None, server_url: None, }; - cmd.run() + cmd.run().report() } Commands::Stream(stream) => { @@ -73,18 +76,19 @@ fn main() -> anyhow::Result<()> { window_size: stream.window_size, stream_local: stream.local, stream_remote: stream.remote, + return_: stream.return_, log_file: stream.log_file, server_url: stream.server_url, }; - cmd.run() + cmd.run().report() } - Commands::Session(cmd) => cmd.run(), - Commands::Play(cmd) => cmd.run(), - Commands::Cat(cmd) => cmd.run(), - Commands::Convert(cmd) => cmd.run(), - Commands::Upload(cmd) => cmd.run(), - Commands::Auth(cmd) => cmd.run(), + Commands::Session(cmd) => cmd.run().report(), + Commands::Play(cmd) => cmd.run().report(), + Commands::Cat(cmd) => cmd.run().report(), + Commands::Convert(cmd) => cmd.run().report(), + Commands::Upload(cmd) => cmd.run().report(), + Commands::Auth(cmd) => cmd.run().report(), } }