Return the exit status of session's main process with --return option

Closes #599
This commit is contained in:
Marcin Kulik
2025-05-29 13:49:53 +02:00
parent 136f0f4b20
commit 9f18c457ba
3 changed files with 37 additions and 14 deletions

View File

@@ -90,6 +90,10 @@ pub struct Record {
#[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)]
pub window_size: Option<(Option<u16>, Option<u16>)>,
/// Return session exit status
#[arg(long)]
pub return_: bool,
#[arg(long, hide = true)]
pub cols: Option<u16>,
@@ -161,6 +165,10 @@ pub struct Stream {
#[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)]
pub window_size: Option<(Option<u16>, Option<u16>)>,
/// Return session exit status
#[arg(long)]
pub return_: bool,
/// Log file path
#[arg(long, value_name = "PATH")]
pub log_file: Option<PathBuf>,
@@ -224,6 +232,10 @@ pub struct Session {
#[arg(long, value_name = "COLSxROWS", value_parser = parse_window_size)]
pub window_size: Option<(Option<u16>, Option<u16>)>,
/// Return session exit status
#[arg(long)]
pub return_: bool,
/// Log file path
#[arg(long, value_name = "PATH")]
pub log_file: Option<PathBuf>,

View File

@@ -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<ExitCode> {
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<N: Notifier + 'static>(

View File

@@ -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(),
}
}