summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/packages.rs5
-rw-r--r--src/packages/apt.rs58
2 files changed, 63 insertions, 0 deletions
diff --git a/src/packages.rs b/src/packages.rs
index 8efc928..3ff997d 100644
--- a/src/packages.rs
+++ b/src/packages.rs
@@ -1,5 +1,6 @@
1use std::{fs::File, io::Read}; 1use std::{fs::File, io::Read};
2 2
3use apt::Apt;
3use dnf::Dnf; 4use dnf::Dnf;
4use pacman::Pacman; 5use pacman::Pacman;
5use portage::Portage; 6use portage::Portage;
@@ -7,6 +8,7 @@ use serde::{Deserialize, Serialize};
7 8
8use crate::error::{Error, Result}; 9use crate::error::{Error, Result};
9 10
11mod apt;
10mod dnf; 12mod dnf;
11mod pacman; 13mod pacman;
12mod portage; 14mod portage;
@@ -34,6 +36,7 @@ pub struct Package {
34 36
35#[derive(Debug, Clone, clap::ValueEnum, Serialize, Deserialize)] 37#[derive(Debug, Clone, clap::ValueEnum, Serialize, Deserialize)]
36pub enum Manager { 38pub enum Manager {
39 Apt,
37 Dnf, 40 Dnf,
38 Pacman, 41 Pacman,
39 Portage, 42 Portage,
@@ -65,6 +68,7 @@ impl Manager {
65 68
66 fn from_str(value: &str) -> Result<Self> { 69 fn from_str(value: &str) -> Result<Self> {
67 Ok(match value { 70 Ok(match value {
71 "debian" | "ubuntu" |"linuxmint" => Self::Apt,
68 "fedora" => Self::Dnf, 72 "fedora" => Self::Dnf,
69 "arch" => Self::Pacman, 73 "arch" => Self::Pacman,
70 "gentoo" => Self::Portage, 74 "gentoo" => Self::Portage,
@@ -74,6 +78,7 @@ impl Manager {
74 78
75 pub fn to_package_manager(&self) -> Box<dyn PackageManager> { 79 pub fn to_package_manager(&self) -> Box<dyn PackageManager> {
76 match self { 80 match self {
81 Self::Apt => Box::new(Apt),
77 Self::Dnf => Box::new(Dnf), 82 Self::Dnf => Box::new(Dnf),
78 Self::Pacman => Box::new(Pacman), 83 Self::Pacman => Box::new(Pacman),
79 Self::Portage => Box::new(Portage), 84 Self::Portage => Box::new(Portage),
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}