From ec30fbab6ea45dcdec7d242fba2ff5555fea8e02 Mon Sep 17 00:00:00 2001 From: fx Date: Mon, 23 Sep 2024 18:56:39 +0200 Subject: add dnf package manager --- src/packages/dnf.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/packages/dnf.rs (limited to 'src/packages') diff --git a/src/packages/dnf.rs b/src/packages/dnf.rs new file mode 100644 index 0000000..aad9c8f --- /dev/null +++ b/src/packages/dnf.rs @@ -0,0 +1,50 @@ +use std::process::Command; + +use super::{Package, PackageList, PackageManager}; + +use crate::error::{Error, Result}; + +pub struct Dnf; + +impl PackageManager for Dnf { + fn get_installed(&self) -> Result { + let list = Command::new("dnf").args(["list", "installed"]).output().unwrap(); + let explicit_list = Command::new("dnf").args(["repoquery", "--userinstalled"]).output().unwrap(); + + let list_str = String::from_utf8(list.stdout).unwrap(); + let ex_list_str = String::from_utf8(explicit_list.stdout).unwrap(); + + + let mut pkgs: Vec = Vec::new(); + 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() != 3 { + return Err(Error::UnknownOutput); + }; + + let explicit = ex_list_str.contains(pkg); + + let Some(pkg_id) = split[0].split_once('.') else { + return Err(Error::UnknownOutput); + }; + pkgs.push(Package { + id: pkg_id.0.to_string(), + version: split[1].to_string(), + explicit, + }); + } + + Ok(PackageList { + packages: pkgs, + manager: super::Manager::Dnf, + }) + } + + fn install(&self, _pkgs: Vec) -> Result<()> { todo!() } +} -- cgit v1.2.3 From d1a9d0d8a8746a63b93f8f14ee4f51b1cd240349 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 29 Sep 2024 15:26:46 +0200 Subject: dnf install --- src/packages/dnf.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/packages') diff --git a/src/packages/dnf.rs b/src/packages/dnf.rs index aad9c8f..6f451ee 100644 --- a/src/packages/dnf.rs +++ b/src/packages/dnf.rs @@ -1,4 +1,4 @@ -use std::process::Command; +use std::process::{Command, Stdio}; use super::{Package, PackageList, PackageManager}; @@ -46,5 +46,17 @@ impl PackageManager for Dnf { }) } - fn install(&self, _pkgs: Vec) -> Result<()> { todo!() } + fn install(&self, pkgs: Vec) -> Result<()> { + let mut args = vec!["dnf".to_string(), "install".to_string(), "--assumeyes".to_string()]; + + for pkg in pkgs { + args.push(pkg.id); + } + Command::new("sudo") + .stdout(Stdio::inherit()) + .args(args) + .spawn()? + .wait_with_output()?; + Ok(()) + } } -- cgit v1.2.3