diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 4b7cec7c..c0b228f3 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -6,6 +6,7 @@ mod show_file_mutations; mod show_language_files; mod show_language_mutations; mod show_single_mutation; +mod step_init_workspace; mod step_tend_state; mod step_tend_workspaces; @@ -86,17 +87,7 @@ fn main() { } 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 workspace = session.bind_workspace(&wid).expect("bind workspace"); - let outcome = workspace - .run_init(&cli.config, None) - .expect("init workspace"); - Box::new(render::InitWorkspace { - workspace_id: wid, - outcome, - }) + step_init_workspace::StepInitWorkspace::run(cli.config.clone(), workspace_id) } config::Step::ResetWorkspace { workspace_id } => { diff --git a/crates/bough-cli/src/step_init_workspace.rs b/crates/bough-cli/src/step_init_workspace.rs new file mode 100644 index 00000000..56a255df --- /dev/null +++ b/crates/bough-cli/src/step_init_workspace.rs @@ -0,0 +1,70 @@ +use bough_core::Session; + +use crate::config::Config; +use crate::render::{ + HASH, RESET, TITLE, Render, fmt_phase_outcome_json, fmt_phase_outcome_terse, + fmt_phase_outcome_verbose, +}; + +pub struct StepInitWorkspace { + pub workspace_id: bough_core::WorkspaceId, + pub outcome: bough_core::PhaseOutcome, +} + +impl StepInitWorkspace { + pub fn run(config: Config, workspace_id: &str) -> Box { + let session = Session::new(config.clone()).expect("session creation"); + let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); + let workspace = session.bind_workspace(&wid).expect("bind workspace"); + let outcome = workspace.run_init(&config, None).expect("init workspace"); + Box::new(Self { + workspace_id: wid, + outcome, + }) + } +} + +impl Render for StepInitWorkspace { + 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), + ) + } +}