use std::process::Command;
#[derive(Debug, thiserror::Error)]
pub enum LogsError {
#[error("az command failed: {0}")]
AzFailed(String),
#[error("io: {0}")]
Io(#[from] std::io::Error),
}
pub fn tail(
container_app_name: &str,
resource_group: &str,
tail: usize,
follow: bool,
since: Option<&str>,
) -> Result<(), LogsError> {
let tail_str = tail.to_string();
let mut args = vec![
"containerapp",
"logs",
"show",
"--name",
container_app_name,
"--resource-group",
resource_group,
"--tail",
&tail_str,
];
if follow {
args.push("--follow");
}
let since_owned;
if let Some(s) = since {
since_owned = s.to_string();
args.push("--since");
args.push(&since_owned);
}
let status = Command::new("az").args(&args).status()?;
if !status.success() {
return Err(LogsError::AzFailed(format!(
"logs command failed for Container App '{container_app_name}'"
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn logs_error_formats_correctly() {
let err = LogsError::AzFailed("app not found".to_string());
assert!(err.to_string().contains("az command failed"));
assert!(err.to_string().contains("app not found"));
}
}