diff --git a/src/lib.rs b/src/lib.rs index e0403714..bb4363de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,6 @@ use std::hash::{Hash, Hasher}; use std::sync::{atomic::AtomicUsize, Arc, RwLock}; use std::time::{self, Duration}; use std::{fmt, io}; -use tokio::fs::File; use crate::config::{GooseConfiguration, GooseDefaults}; use crate::controller::{ControllerProtocol, ControllerRequest}; @@ -1723,16 +1722,8 @@ impl GooseAttack { goose_attack_run_state.throttle_threads_tx = throttle_threads_tx; goose_attack_run_state.parent_to_throttle_tx = parent_to_throttle_tx; - // If enabled, try to create the report file to confirm access. - for file in &self.configuration.report_file { - let _ = File::create(&file) - .await - .map_err(|err| GooseError::InvalidOption { - option: "--report-file".to_string(), - value: file.clone(), - detail: format!("Failed to create report file: {err}"), - })?; - } + // Try to create the requested report files, to confirm access. + self.create_reports().await?; // Record when the GooseAttack officially started. self.started = Some(time::Instant::now()); diff --git a/src/metrics.rs b/src/metrics.rs index b06ecd07..fe8313a5 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -3003,8 +3003,11 @@ impl GooseAttack { }; } - /// Write all requested reports. - pub(crate) async fn write_reports(&self) -> Result<(), GooseError> { + /// Process all requested reports. + /// + /// If `write` is true, then also write the data. Otherwise just create the files to ensure + /// we have access. + async fn process_reports(&self, write: bool) -> Result<(), GooseError> { let create = |path: PathBuf| async move { File::create(&path) .await @@ -3019,13 +3022,22 @@ impl GooseAttack { let path = PathBuf::from(report); match path.extension().map(OsStr::to_string_lossy).as_deref() { Some("html" | "htm") => { - self.write_html_report(create(path).await?, report).await?; + let file = create(path).await?; + if write { + self.write_html_report(file, report).await?; + } } Some("json") => { - self.write_json_report(create(path).await?).await?; + let file = create(path).await?; + if write { + self.write_json_report(file).await?; + } } Some("md") => { - self.write_markdown_report(create(path).await?).await?; + let file = create(path).await?; + if write { + self.write_markdown_report(file).await?; + } } None => { return Err(GooseError::InvalidOption { @@ -3047,6 +3059,16 @@ impl GooseAttack { Ok(()) } + /// Create all requested reports, to ensure we have access. + pub(crate) async fn create_reports(&self) -> Result<(), GooseError> { + self.process_reports(false).await + } + + /// Write all requested reports. + pub(crate) async fn write_reports(&self) -> Result<(), GooseError> { + self.process_reports(true).await + } + /// Write a JSON report. pub(crate) async fn write_json_report(&self, report_file: File) -> Result<(), GooseError> { let data = common::prepare_data(