refactor(#1474): group journalctl read args into a journalquery struct
This commit is contained in:
parent
98660d134a
commit
f751c4495f
7 changed files with 77 additions and 116 deletions
|
|
@ -21,7 +21,7 @@ use std::path::{Path, PathBuf};
|
|||
|
||||
use anyhow::{Context as _, Result, bail};
|
||||
use hive_sh4re::priv_proto::{
|
||||
AGENT_PREFIX, AGENT_STATE_ROOT, BindMount, JournalOutput, MANAGER_NAME, META_DIR,
|
||||
AGENT_PREFIX, AGENT_STATE_ROOT, BindMount, JournalQuery, MANAGER_NAME, META_DIR,
|
||||
NetworkIsolation, PRIV_SOCK, PrivEvent, PrivRequest, PrivResponse, PrivStream, PrivStreamLine,
|
||||
SIBLING_CONTAINERS,
|
||||
};
|
||||
|
|
@ -222,28 +222,10 @@ async fn exec(req: PrivRequest, writer: &mut OwnedWriteHalf) -> Result<(String,
|
|||
|
||||
PrivRequest::ReadContainerJournal {
|
||||
ref container,
|
||||
lines,
|
||||
boot,
|
||||
output,
|
||||
ref unit,
|
||||
ref priority,
|
||||
ref grep,
|
||||
ref since,
|
||||
ref until,
|
||||
ref query,
|
||||
} => {
|
||||
validate_container_system_name(container)?;
|
||||
read_container_journal(
|
||||
container,
|
||||
lines,
|
||||
boot,
|
||||
output,
|
||||
unit.as_deref(),
|
||||
priority.as_deref(),
|
||||
grep.as_deref(),
|
||||
since.as_deref(),
|
||||
until.as_deref(),
|
||||
)
|
||||
.await
|
||||
read_container_journal(container, query).await
|
||||
}
|
||||
|
||||
PrivRequest::WriteNspawnFlags {
|
||||
|
|
@ -606,46 +588,35 @@ async fn container_run_streaming(
|
|||
/// hard error — journalctl's own diagnostic (folded into `stderr` with
|
||||
/// the exit status) is what the caller surfaces to the operator, so the
|
||||
/// helper never bails.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn read_container_journal(
|
||||
container: &str,
|
||||
lines: u32,
|
||||
boot: bool,
|
||||
output: JournalOutput,
|
||||
unit: Option<&str>,
|
||||
priority: Option<&str>,
|
||||
grep: Option<&str>,
|
||||
since: Option<&str>,
|
||||
until: Option<&str>,
|
||||
) -> Result<(String, String)> {
|
||||
async fn read_container_journal(container: &str, query: &JournalQuery) -> Result<(String, String)> {
|
||||
let mut args: Vec<String> = vec![
|
||||
"-M".to_owned(),
|
||||
container.to_owned(),
|
||||
"--no-pager".to_owned(),
|
||||
format!("--output={}", output.as_journalctl()),
|
||||
format!("--output={}", query.output.as_journalctl()),
|
||||
"-n".to_owned(),
|
||||
lines.to_string(),
|
||||
query.lines.to_string(),
|
||||
];
|
||||
if boot {
|
||||
if query.boot {
|
||||
args.push("-b".to_owned());
|
||||
}
|
||||
if let Some(u) = unit {
|
||||
if let Some(u) = &query.unit {
|
||||
args.push("-u".to_owned());
|
||||
args.push(u.to_owned());
|
||||
args.push(u.clone());
|
||||
}
|
||||
if let Some(p) = priority {
|
||||
if let Some(p) = &query.priority {
|
||||
args.push("-p".to_owned());
|
||||
args.push(p.to_owned());
|
||||
args.push(p.clone());
|
||||
}
|
||||
// `--grep=`/`--since=`/`--until=` use the `=`-joined form so a value
|
||||
// can never be parsed as a separate journalctl flag.
|
||||
if let Some(g) = grep {
|
||||
if let Some(g) = &query.grep {
|
||||
args.push(format!("--grep={g}"));
|
||||
}
|
||||
if let Some(s) = since {
|
||||
if let Some(s) = &query.since {
|
||||
args.push(format!("--since={s}"));
|
||||
}
|
||||
if let Some(u) = until {
|
||||
if let Some(u) = &query.until {
|
||||
args.push(format!("--until={u}"));
|
||||
}
|
||||
let out = Command::new("journalctl")
|
||||
|
|
|
|||
Loading…
Reference in a new issue