diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index c0b228f3..62b8a093 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -7,6 +7,7 @@ mod show_language_files; mod show_language_mutations; mod show_single_mutation; mod step_init_workspace; +mod step_reset_workspace; mod step_tend_state; mod step_tend_workspaces; @@ -91,17 +92,7 @@ fn main() { } config::Step::ResetWorkspace { 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_reset(&cli.config, None) - .expect("reset workspace"); - Box::new(render::ResetWorkspace { - workspace_id: wid, - outcome, - }) + step_reset_workspace::StepResetWorkspace::run(cli.config.clone(), workspace_id) } config::Step::ApplyMutation { diff --git a/crates/bough-cli/src/step_reset_workspace.rs b/crates/bough-cli/src/step_reset_workspace.rs new file mode 100644 index 00000000..5c5e7094 --- /dev/null +++ b/crates/bough-cli/src/step_reset_workspace.rs @@ -0,0 +1,72 @@ +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 StepResetWorkspace { + pub workspace_id: bough_core::WorkspaceId, + pub outcome: bough_core::PhaseOutcome, +} + +impl StepResetWorkspace { + 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_reset(&config, None) + .expect("reset workspace"); + Box::new(Self { + workspace_id: wid, + outcome, + }) + } +} + +impl Render for StepResetWorkspace { + 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!( + "# Reset 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}Reset 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), + ) + } +}