1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use as_result::*;
use async_process::{Child, ChildStdout, Command};
use async_stream::stream;
use futures::io::BufReader;
use futures::prelude::*;
use futures::stream::StreamExt;
use futures_util::pin_mut;
use std::{io, pin::Pin};

#[derive(AsMut, Deref, DerefMut)]
#[as_mut(forward)]
pub struct Dpkg(Command);

impl Dpkg {
    pub fn new() -> Self {
        let mut cmd = Command::new("dpkg");
        cmd.env("LANG", "C");
        Self(cmd)
    }

    pub fn configure_all(mut self) -> Self {
        self.args(&["--configure", "-a"]);
        self
    }

    pub async fn status(mut self) -> io::Result<()> {
        self.0.status().await?.into_result()
    }
}

pub type InstalledEvent = Pin<Box<dyn Stream<Item = String>>>;

#[derive(AsMut, Deref, DerefMut)]
#[as_mut(forward)]
pub struct DpkgQuery(Command);

impl DpkgQuery {
    pub fn new() -> Self {
        let mut cmd = Command::new("dpkg-query");
        cmd.env("LANG", "C");
        Self(cmd)
    }

    pub async fn show_installed<I, S>(mut self, packages: I) -> io::Result<(Child, InstalledEvent)>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<std::ffi::OsStr>,
    {
        self.args(&["--show", "--showformat=${Package} ${db:Status-Status}\n"]);
        self.args(packages);

        let (child, stdout) = self.spawn_with_stdout().await?;

        let stdout = BufReader::new(stdout).lines();

        let stream = stream! {
            pin_mut!(stdout);
            while let Some(Ok(line)) = stdout.next().await {
                let mut fields = line.split(' ');
                let package = fields.next().unwrap();
                if fields.next().unwrap() == "installed" {
                    yield package.into();
                }
            }
        };

        Ok((child, Box::pin(stream)))
    }

    pub async fn status(mut self) -> io::Result<()> {
        self.0.status().await?.into_result()
    }

    pub async fn spawn_with_stdout(self) -> io::Result<(Child, ChildStdout)> {
        crate::utils::spawn_with_stdout(self.0).await
    }
}