summaryrefslogtreecommitdiff
path: root/src/packages/apt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/packages/apt.rs')
-rw-r--r--src/packages/apt.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/packages/apt.rs b/src/packages/apt.rs
new file mode 100644
index 0000000..2036559
--- /dev/null
+++ b/src/packages/apt.rs
@@ -0,0 +1,58 @@
1use std::process::{Command, Stdio};
2
3use super::{Package, PackageList, PackageManager};
4
5use crate::error::{Error, Result};
6
7pub struct Apt;
8
9impl PackageManager for Apt {
10 fn get_installed(&self) -> Result<PackageList> {
11 let list = Command::new("apt").args(["list", "--installed"]).output().unwrap();
12
13 let mut pkgs: Vec<Package> = Vec::new();
14
15 let list_str = String::from_utf8(list.stdout).unwrap();
16 let list_lines: Vec<&str> = list_str.split('\n').collect();
17 // Pop first info line
18 let list_lines = &list_lines[1..list_lines.len()];
19 for pkg in list_lines {
20 if pkg.is_empty() {
21 continue;
22 };
23
24 let split: Vec<&str> = pkg.split_whitespace().collect();
25 if split.len() != 4 {
26 return Err(Error::UnknownOutput);
27 };
28
29 let Some(pkg_id) = split[0].split_once('/') else {
30 return Err(Error::UnknownOutput);
31 };
32
33 let explicit = split[3] == "[installed]";
34
35 pkgs.push(Package {
36 id: pkg_id.0.to_string(),
37 version: split[1].to_string(),
38 explicit
39 })
40 }
41
42 Ok(PackageList { packages: pkgs, manager: super::Manager::Apt })
43 }
44
45 fn install(&self, pkgs: Vec<super::Package>) -> Result<()> {
46 let mut args = vec!["apt".to_string(), "install".to_string(), "--yes".to_string()];
47
48 for pkg in pkgs {
49 args.push(pkg.id);
50 }
51 Command::new("sudo")
52 .stdout(Stdio::inherit())
53 .args(args)
54 .spawn()?
55 .wait_with_output()?;
56 Ok(())
57 }
58}