use super::{Context, INTERRUPTED_EXIT, cancellation_listener, print_json};
use std::{
io::{self, Write},
path::PathBuf,
};
use artifactize_core::project::selection::Selection;
use super::{Follow, PolicyArgs, outcome_code};
pub(super) struct VerifyLimits {
pub modes: artifactize_core::runs::RunModes,
pub jobs: Option<std::num::NonZeroU32>,
pub max_executions: Option<u64>,
pub follow: Follow,
pub request_key: Option<artifactize_core::types::RequestKey>,
pub comment: Option<artifactize_core::types::RunComment>,
pub reuse_only: Vec<artifactize_core::config::ProfileKind>,
}
pub(super) async fn verify(
context: Context,
selection: Selection,
policy: PolicyArgs,
limits: VerifyLimits,
) -> Result<u8, String> {
let VerifyLimits {
modes,
jobs,
max_executions,
follow,
request_key,
comment,
reuse_only,
} = limits;
let options = artifactize_core::project::VerifyOptions {
modes,
jobs: jobs.map(|limit| limit.get() as usize),
max_executions,
reuse_only: reuse_only.into_iter().collect(),
comment,
..policy.options()
};
let repo = context.repo.unwrap_or_else(|| PathBuf::from("."));
let runs = artifactize_core::runs::Context::new(
&repo,
context.state_dir.as_deref(),
&artifactize_core::platform::current_executable().map_err(|e| e.to_string())?,
)?;
let observation =
artifactize_core::runs::start(&runs, &selection, &options, request_key).await?;
match follow {
Follow::Watch => {
return super::run::watch_run(&runs, observation.run).await;
}
Follow::Submit => {
if context.json {
print_json(
&serde_json::json!({"run": observation.run, "state": observation.state}),
)?;
} else {
writeln!(io::stdout().lock(), "{}", observation.run).map_err(|e| e.to_string())?;
}
return Ok(0);
}
Follow::Wait => {}
}
let (cancellation, listener) = cancellation_listener()?;
writeln!(
io::stderr().lock(),
"Started {id} (follow: artifactize run watch {id})",
id = observation.run
)
.map_err(|e| e.to_string())?;
let result =
artifactize_core::runs::wait(&runs, &observation.run, None, cancellation.clone()).await;
listener.abort();
if cancellation.is_cancelled() {
return Ok(INTERRUPTED_EXIT);
}
result?;
let view = artifactize_core::runs::view(&runs, &observation.run).await?;
let _ = io::stderr()
.lock()
.write_all(runs.worker_diagnostics(&view.run.id).as_bytes());
if let Some(error) = view.run.error() {
let _ = writeln!(io::stderr().lock(), "{error}");
}
let observed_at = time::OffsetDateTime::now_utc();
super::render::verify(&view, context.json, observed_at)?;
Ok(outcome_code(&view.run))
}