diff --git a/src/bin/repo.rs b/src/bin/repo.rs index f0412644d..1c94c4ee3 100644 --- a/src/bin/repo.rs +++ b/src/bin/repo.rs @@ -6,11 +6,11 @@ use cookbook::cook::fetch::{FetchResult, fetch, fetch_offline}; use cookbook::cook::fs::{create_target_dir, run_command}; use cookbook::cook::ident; use cookbook::cook::package::{package, package_handle_push}; -use cookbook::cook::pty::{PtyOut, UnixSlavePty, flush_pty, setup_pty}; +use cookbook::cook::pty::{PtyOut, UnixSlavePty, flush_pty, setup_pty, write_to_pty}; use cookbook::cook::script::KILL_ALL_PID; use cookbook::cook::tree::{self, WalkTreeEntry}; use cookbook::recipe::{CookRecipe, recipes_flatten_package_names, recipes_mark_as_deps}; -use cookbook::{log_to_pty, staged_pkg}; +use cookbook::{Error, staged_pkg}; use pkg::{PackageName, PackageState}; use ratatui::Terminal; use ratatui::layout::{Constraint, Direction, Layout, Position, Rect}; @@ -359,7 +359,7 @@ fn repo_inner( let mut logger = Some((&mut stdout_writer, &mut stderr_writer)); let result = repo_inner_fn(&logger); if let Err(err_ctx) = &result { - log_to_pty!(&logger, "\n{:?}", err_ctx) + write_to_pty(&logger, &format!("\n{:?}", err_ctx)); } // successful fetch is not that useful to log if *command == CliCommand::Cook || result.is_err() { @@ -742,12 +742,11 @@ fn handle_cook( } /// delete stage artifacts upon nonstop failure to let repo_builder know -fn handle_nonstop_fail(recipe: &CookRecipe) -> anyhow::Result<()> { +fn handle_nonstop_fail(recipe: &CookRecipe) -> cookbook::Result<()> { let target_dir = recipe.target_dir(); let stage_dirs = get_stage_dirs(&recipe.recipe.optional_packages, &target_dir); for stage_dir in stage_dirs { - remove_stage_dir(&stage_dir) - .map_err(|err| anyhow!("failed to remove stage dir: {:?}", err))?; + remove_stage_dir(&stage_dir)?; } Ok(()) } @@ -958,6 +957,8 @@ impl ToString for JobType { } } +const PROMPT_WAIT: Duration = Duration::from_millis(101); + struct TuiApp { recipes: Vec<(CookRecipe, RecipeStatus)>, fetch_queue: VecDeque, @@ -1195,7 +1196,7 @@ fn run_tui_cook(config: CliConfig, recipes: Vec) -> Result) -> Result break 'again, - 1 => thread::sleep(Duration::from_millis(101)), + 1 => thread::sleep(PROMPT_WAIT), 2 => { cooker_prompting.swap(0, Ordering::SeqCst); break 'wait; @@ -1296,11 +1297,11 @@ fn run_tui_cook(config: CliConfig, recipes: Vec) -> Result) -> Result break 'again, - 1 => thread::sleep(Duration::from_millis(101)), + 1 => thread::sleep(PROMPT_WAIT), 2 => { fetcher_prompting.swap(0, Ordering::SeqCst); break 'wait; @@ -1368,17 +1369,11 @@ fn run_tui_cook(config: CliConfig, recipes: Vec) -> Result) -> Result Result<(), String> { +pub fn remove_stage_dir(stage_dir: &PathBuf) -> crate::Result<()> { if stage_dir.is_dir() { remove_all(&stage_dir)?; } diff --git a/src/cook/pty.rs b/src/cook/pty.rs index 373817e3b..4d1d466dc 100644 --- a/src/cook/pty.rs +++ b/src/cook/pty.rs @@ -16,7 +16,6 @@ pub use std::os::unix::io::RawFd; use crate::{Error, Result, wrap_io_err}; -#[macro_export] macro_rules! log_to_pty { ($logger:expr, $($arg:tt)+) => { if $logger.is_some() { @@ -30,6 +29,8 @@ macro_rules! log_to_pty { }; } +pub(crate) use log_to_pty; + pub type PtyOut<'a> = Option<(&'a mut UnixSlavePty, &'a mut PipeWriter)>; pub fn setup_pty() -> ( @@ -74,6 +75,10 @@ pub fn spawn_to_pipe(command: &mut Command, stdout_pipe: &PtyOut) -> Result Error { wrap_io_err!(context)(io::Error::last_os_error()) } + pub fn from_io_error(err: io::Error, context: &'static str) -> Error { + wrap_io_err!(context)(err) + } } impl Display for Error { @@ -194,10 +197,12 @@ impl From for Error { } } -pub(crate) type Result = std::result::Result; +pub type Result = std::result::Result; pub(crate) use wrap_io_err; pub(crate) use wrap_other_err; pub(crate) use bail_other_err; + +pub(crate) use cook::pty::log_to_pty;