From 7e4fd427064240b8fd3bce73e15ddaa12cbc3ec9 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sun, 8 Mar 2026 22:33:42 +0000 Subject: [PATCH] Implement InitWorkspace step execution and rendering --- crates/bough-cli/src/main.rs | 14 ++++- crates/bough-cli/src/render.rs | 90 ++++++++++++++++++++++++++++++++ crates/bough-core/src/lib.rs | 1 + crates/bough-core/src/session.rs | 2 +- 4 files changed, 105 insertions(+), 2 deletions(-) diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 7ba18299..bae37bcb 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -173,7 +173,19 @@ fn main() { Box::new(render::TendWorkspaces { workspace_ids }) } - config::Step::InitWorkspace { workspace_id } => todo!(), + config::Step::InitWorkspace { workspace_id } => { + let session = + Session::new(cli.config.clone()).expect("session creation"); + let wid = bough_core::WorkspaceId::parse(workspace_id) + .expect("invalid workspace id"); + let outcome = session + .run_init_in_workspace(&wid, None) + .expect("init workspace"); + Box::new(render::InitWorkspace { + workspace_id: wid, + outcome, + }) + } config::Step::ResetWorkspace { workspace_id } => todo!(), diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index 5b7be57d..e7d62141 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -438,6 +438,96 @@ impl Render for TendWorkspaces { } } +pub struct InitWorkspace { + pub workspace_id: bough_core::WorkspaceId, + pub outcome: bough_core::PhaseOutcome, +} + +fn fmt_phase_outcome_terse(outcome: &bough_core::PhaseOutcome) -> String { + format!( + "exit={} duration={:.2}s{}", + outcome.exit_code(), + outcome.duration().as_secs_f64(), + if outcome.timed_out() { " TIMED_OUT" } else { "" }, + ) +} + +fn fmt_phase_outcome_verbose(outcome: &bough_core::PhaseOutcome) -> String { + let stdout = String::from_utf8_lossy(outcome.stdout()); + let stderr = String::from_utf8_lossy(outcome.stderr()); + let mut out = format!( + "Exit: {}\nDuration: {:.2}s\nTimed out: {}", + outcome.exit_code(), + outcome.duration().as_secs_f64(), + outcome.timed_out(), + ); + if !stdout.is_empty() { + out.push_str(&format!("\n\n{TITLE}stdout{RESET}\n{stdout}")); + } + if !stderr.is_empty() { + out.push_str(&format!("\n\n{TITLE}stderr{RESET}\n{stderr}")); + } + out +} + +fn fmt_phase_outcome_json(outcome: &bough_core::PhaseOutcome) -> String { + let stdout = String::from_utf8_lossy(outcome.stdout()); + let stderr = String::from_utf8_lossy(outcome.stderr()); + format!( + r#"{{"exit_code":{},"duration_secs":{:.3},"timed_out":{},"stdout":"{}","stderr":"{}"}}"#, + outcome.exit_code(), + outcome.duration().as_secs_f64(), + outcome.timed_out(), + stdout.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n"), + stderr.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n"), + ) +} + +impl Render for InitWorkspace { + fn markdown(&self) -> String { + let stdout = String::from_utf8_lossy(self.outcome.stdout()); + let stderr = String::from_utf8_lossy(self.outcome.stderr()); + let mut out = format!( + "# Init Workspace `{}`\n\n- Exit: {}\n- Duration: {:.2}s\n- Timed out: {}", + self.workspace_id, + self.outcome.exit_code(), + self.outcome.duration().as_secs_f64(), + self.outcome.timed_out(), + ); + if !stdout.is_empty() { + out.push_str(&format!("\n\n## stdout\n\n```\n{stdout}\n```")); + } + if !stderr.is_empty() { + out.push_str(&format!("\n\n## stderr\n\n```\n{stderr}\n```")); + } + out + } + + fn terse(&self) -> String { + format!( + "{HASH}{}{RESET} {}", + self.workspace_id, + fmt_phase_outcome_terse(&self.outcome), + ) + } + + fn verbose(&self) -> String { + format!( + "{TITLE}Init Workspace{RESET} {HASH}{}{RESET}\n\n{}", + self.workspace_id, + fmt_phase_outcome_verbose(&self.outcome), + ) + } + + fn json(&self) -> String { + format!( + r#"{{"workspace_id":"{}","outcome":{}}}"#, + self.workspace_id, + fmt_phase_outcome_json(&self.outcome), + ) + } +} + impl Render for Config { fn markdown(&self) -> String { format!( diff --git a/crates/bough-core/src/lib.rs b/crates/bough-core/src/lib.rs index 7c694783..2da73523 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -40,4 +40,5 @@ pub use mutation::MutationHash; pub use session::Config; pub use session::Session; pub use state::State; +pub use phase::PhaseOutcome; pub use workspace::WorkspaceId; diff --git a/crates/bough-core/src/session.rs b/crates/bough-core/src/session.rs index f4cc646c..ebb169c5 100644 --- a/crates/bough-core/src/session.rs +++ b/crates/bough-core/src/session.rs @@ -1156,7 +1156,7 @@ mod tests { .outcome_at() .unwrap(); - std::thread::sleep(std::time::Duration::from_millis(100)); + std::thread::sleep(std::time::Duration::from_millis(500)); session .set_state(&mutation, crate::state::Status::Caught) .unwrap(); -- knotmirror 0.0.1