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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::request::{Request, RequestError};
use crate::AptUpgradeEvent;
use as_result::*;
use async_process::{Child, ChildStdout, Command, ExitStatus};
use async_stream::stream;
use futures::io::BufReader;
use futures::prelude::*;
use futures::stream::StreamExt;
use futures_util::pin_mut;
use std::{collections::HashSet, io, pin::Pin};
#[derive(Debug)]
pub enum UpdateEvent {
BadPPA(BadPPA),
ExitStatus(io::Result<ExitStatus>),
}
#[derive(Debug)]
pub struct BadPPA {
pub url: String,
pub pocket: String,
}
pub type UpgradeEvents = Pin<Box<dyn Stream<Item = AptUpgradeEvent>>>;
#[derive(AsMut, Deref, DerefMut)]
#[as_mut(forward)]
pub struct AptGet(Command);
impl AptGet {
pub fn new() -> Self {
let mut cmd = Command::new("apt-get");
cmd.env("LANG", "C");
Self(cmd)
}
pub fn allow_downgrades(mut self) -> Self {
self.arg("--allow-downgrades");
return self;
}
pub fn autoremove(mut self) -> Self {
self.arg("autoremove");
self
}
pub fn fix_broken(mut self) -> Self {
self.args(&["install", "-f"]);
self
}
pub fn force(mut self) -> Self {
self.arg("-y");
return self;
}
pub async fn install<I, S>(mut self, packages: I) -> io::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
self.arg("install");
self.args(packages);
self.status().await
}
pub fn noninteractive(mut self) -> Self {
self.env("DEBIAN_FRONTEND", "noninteractive");
return self;
}
pub async fn update(mut self) -> io::Result<()> {
self.arg("update");
self.status().await
}
pub fn simulate(mut self) -> Self {
self.arg("-s");
self
}
pub async fn upgrade(mut self) -> io::Result<()> {
self.arg("full-upgrade");
self.status().await
}
pub async fn stream_upgrade(mut self) -> io::Result<(Child, UpgradeEvents)> {
self.args(&["--show-progress", "full-upgrade"]);
let (child, stdout) = self.spawn_with_stdout().await?;
let stream = stream! {
let stdout = BufReader::new(stdout).lines();
pin_mut!(stdout);
while let Some(Ok(line)) = stdout.next().await {
if let Ok(event) = line.parse::<AptUpgradeEvent>() {
yield event;
}
}
};
Ok((child, Box::pin(stream)))
}
pub async fn remove<I, S>(mut self, packages: I) -> io::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
self.arg("remove");
self.args(packages);
self.status().await
}
pub async fn fetch_uris(
mut self,
command: &[&str],
) -> io::Result<Result<HashSet<Request>, RequestError>> {
self.arg("--print-uris");
self.args(command);
let (mut child, stdout) = self.spawn_with_stdout().await?;
let stdout = BufReader::new(stdout).lines();
pin_mut!(stdout);
let mut packages = HashSet::new();
while let Some(Ok(line)) = stdout.next().await {
if !line.starts_with('\'') {
continue;
}
let package = match line.parse::<Request>() {
Ok(package) => package,
Err(why) => return Ok(Err(why)),
};
packages.insert(package);
}
child.status().await.map_result()?;
Ok(Ok(packages))
}
pub async fn stream_update(mut self) -> io::Result<Pin<Box<dyn Stream<Item = UpdateEvent>>>> {
self.arg("update");
let (mut 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 {
if line.starts_with("Err") {
let mut fields = line.split_ascii_whitespace();
let _ = fields.next();
let url = fields.next().unwrap();
let pocket = fields.next().unwrap();
yield UpdateEvent::BadPPA(BadPPA {
url: url.into(),
pocket: pocket.into(),
});
}
}
yield UpdateEvent::ExitStatus(child.status().await);
};
Ok(Box::pin(stream))
}
pub async fn spawn_with_stdout(self) -> io::Result<(Child, ChildStdout)> {
crate::utils::spawn_with_stdout(self.0).await
}
pub async fn status(mut self) -> io::Result<()> {
self.0.status().await?.into_result()
}
}