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/dnf.rs') 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