blob: 203655987f28dcad6413bd7ee698066a0767ad07 (
plain) (
blame)
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
|
use std::process::{Command, Stdio};
use super::{Package, PackageList, PackageManager};
use crate::error::{Error, Result};
pub struct Apt;
impl PackageManager for Apt {
fn get_installed(&self) -> Result<PackageList> {
let list = Command::new("apt").args(["list", "--installed"]).output().unwrap();
let mut pkgs: Vec<Package> = Vec::new();
let list_str = String::from_utf8(list.stdout).unwrap();
let list_lines: Vec<&str> = list_str.split('\n').collect();
// Pop first info line
let list_lines = &list_lines[1..list_lines.len()];
for pkg in list_lines {
if pkg.is_empty() {
continue;
};
let split: Vec<&str> = pkg.split_whitespace().collect();
if split.len() != 4 {
return Err(Error::UnknownOutput);
};
let Some(pkg_id) = split[0].split_once('/') else {
return Err(Error::UnknownOutput);
};
let explicit = split[3] == "[installed]";
pkgs.push(Package {
id: pkg_id.0.to_string(),
version: split[1].to_string(),
explicit
})
}
Ok(PackageList { packages: pkgs, manager: super::Manager::Apt })
}
fn install(&self, pkgs: Vec<super::Package>) -> Result<()> {
let mut args = vec!["apt".to_string(), "install".to_string(), "--yes".to_string()];
for pkg in pkgs {
args.push(pkg.id);
}
Command::new("sudo")
.stdout(Stdio::inherit())
.args(args)
.spawn()?
.wait_with_output()?;
Ok(())
}
}
|