1use as_result::*;
5use async_stream::stream;
6use futures::stream::Stream;
7use std::{io, pin::Pin};
8use tokio::io::{AsyncBufReadExt, BufReader};
9use tokio::process::{Child, ChildStdout, Command};
10
11#[derive(AsMut, Deref, DerefMut)]
12#[as_mut(forward)]
13pub struct Dpkg(Command);
14
15impl Dpkg {
16 #[allow(clippy::new_without_default)]
17 pub fn new() -> Self {
18 let mut cmd = Command::new("dpkg");
19 cmd.env("LANG", "C");
20 Self(cmd)
21 }
22
23 pub fn force_confdef(mut self) -> Self {
24 self.arg("--force-confdef");
25 self
26 }
27
28 pub fn force_confold(mut self) -> Self {
29 self.arg("--force-confold");
30 self
31 }
32
33 pub fn configure_all(mut self) -> Self {
34 self.args(["--configure", "-a"]);
35 self
36 }
37
38 pub async fn status(mut self) -> io::Result<()> {
39 self.0.status().await?.into_result()
40 }
41}
42
43pub type InstalledEvent = Pin<Box<dyn Stream<Item = String>>>;
44
45#[derive(AsMut, Deref, DerefMut)]
46#[as_mut(forward)]
47pub struct DpkgQuery(Command);
48
49impl DpkgQuery {
50 #[allow(clippy::new_without_default)]
51 pub fn new() -> Self {
52 let mut cmd = Command::new("dpkg-query");
53 cmd.env("LANG", "C");
54 Self(cmd)
55 }
56
57 pub async fn show_installed<I, S>(mut self, packages: I) -> io::Result<(Child, InstalledEvent)>
58 where
59 I: IntoIterator<Item = S>,
60 S: AsRef<std::ffi::OsStr>,
61 {
62 self.args(["--show", "--showformat=${Package} ${db:Status-Status}\n"]);
63 self.args(packages);
64
65 let (child, stdout) = self.spawn_with_stdout().await?;
66
67 let mut stdout = BufReader::new(stdout).lines();
68
69 let stream = stream! {
70 while let Ok(Some(line)) = stdout.next_line().await {
71 let mut fields = line.split(' ');
72 let package = fields.next().unwrap();
73 if fields.next().unwrap() == "installed" {
74 yield package.into();
75 }
76 }
77 };
78
79 Ok((child, Box::pin(stream)))
80 }
81
82 pub async fn status(mut self) -> io::Result<()> {
83 self.0.status().await?.into_result()
84 }
85
86 pub async fn spawn_with_stdout(self) -> io::Result<(Child, ChildStdout)> {
87 crate::utils::spawn_with_stdout(self.0).await
88 }
89}