diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-18 09:07:22 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-18 09:07:22 +0800 |
| commit | b3aa0a08848ccd01cd7a4ecef6ff5bf46719af85 (patch) | |
| tree | cf079a8714ecde10339701e9f77fb5f886ea9d2c /mingling_ci/src/cmd | |
| parent | 9591f192004547a4532e000c0f4534eb48bcb1bb (diff) | |
feat(ci-new): add report-clean command to remove collected logs and
report
Diffstat (limited to 'mingling_ci/src/cmd')
| -rw-r--r-- | mingling_ci/src/cmd/cmd_report_clean.rs | 54 | ||||
| -rw-r--r-- | mingling_ci/src/cmd/cmd_report_collect.rs | 5 |
2 files changed, 56 insertions, 3 deletions
diff --git a/mingling_ci/src/cmd/cmd_report_clean.rs b/mingling_ci/src/cmd/cmd_report_clean.rs new file mode 100644 index 0000000..976851e --- /dev/null +++ b/mingling_ci/src/cmd/cmd_report_clean.rs @@ -0,0 +1,54 @@ +use std::path::PathBuf; + +use mingling::{ + Grouped, RenderResult, Routable, + macros::{buffer, command, r_println, renderer}, +}; + +use crate::Next; +use crate::reporter::{COLLECT_DIR, REPORT_PATH}; +use crate::res::{CargoError, MessagePrinter}; + +/// Removes collected logs and the generated report. +#[command(node = "report-clean")] +pub fn report_clean() -> Next { + let mut removed = Vec::new(); + for path in [PathBuf::from(COLLECT_DIR), PathBuf::from(REPORT_PATH)] { + match std::fs::remove_dir_all(&path).or_else(|_| std::fs::remove_file(&path)) { + Ok(()) => removed.push(path), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => { + return ErrorReportClean(format!("failed to remove {}: {e}", path.display())) + .to_chain(); + } + } + } + ResultReportClean { removed }.to_chain() +} + +/// Paths removed by `report-clean`. +#[derive(Grouped)] +pub struct ResultReportClean { + pub removed: Vec<PathBuf>, +} + +#[derive(Grouped, Default)] +pub struct ErrorReportClean(pub String); + +#[renderer(buffer)] +pub fn render_report_clean(r: ResultReportClean) { + if r.removed.is_empty() { + r_println!("Report data already clean"); + } else { + for path in r.removed { + r_println!("Removed {}", path.display()); + } + } +} + +#[renderer] +pub fn render_error_report_clean(e: ErrorReportClean, error: &CargoError) -> RenderResult { + let render_result = RenderResult::new(); + error.println(vec![format!("Report: {}", e.0)]); + render_result +} diff --git a/mingling_ci/src/cmd/cmd_report_collect.rs b/mingling_ci/src/cmd/cmd_report_collect.rs index bb944b3..3cebaef 100644 --- a/mingling_ci/src/cmd/cmd_report_collect.rs +++ b/mingling_ci/src/cmd/cmd_report_collect.rs @@ -8,10 +8,9 @@ use mingling::{ }; use crate::Next; -use crate::reporter::COLLECT_DIR; +use crate::reporter::{COLLECT_DIR, REPORT_PATH}; use crate::res::{CargoError, Manifests, MessagePrinter, ResCollectLogs}; -const OUTPUT_PATH: &str = "./.temp/reports/result.md"; const REPORT_TEMPLATE: &str = include_str!("../../tmpls/report.md"); const TASK_SECTION_TEMPLATE: &str = include_str!("../../tmpls/task_section.md"); @@ -93,7 +92,7 @@ pub fn report_collect(manifests: &Manifests, logs: &ResCollectLogs) -> Next { *template.add_impl("task_sections".to_string()) = sections; let expanded = template.expand().unwrap_or_default(); - let output = PathBuf::from(OUTPUT_PATH); + let output = PathBuf::from(REPORT_PATH); let parent = output.parent().expect("output path has a parent"); if let Err(e) = std::fs::create_dir_all(parent).and_then(|()| std::fs::write(&output, expanded)) |
