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.rs | 5 +++++ src/packages/dnf.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/packages/dnf.rs (limited to 'src') diff --git a/src/packages.rs b/src/packages.rs index de818f4..2844a9b 100644 --- a/src/packages.rs +++ b/src/packages.rs @@ -1,11 +1,13 @@ use std::{fs::File, io::Read}; +use dnf::Dnf; use pacman::Pacman; use portage::Portage; use serde::{Deserialize, Serialize}; use crate::error::{Error, Result}; +mod dnf; mod pacman; mod portage; @@ -32,6 +34,7 @@ pub struct Package { #[derive(Debug, Clone, clap::ValueEnum, Serialize, Deserialize)] pub enum Manager { + Dnf, Pacman, Portage, } @@ -65,6 +68,7 @@ impl Manager { fn from_str(value: &str) -> Result> { Ok(match value { + "fedora" => Box::new(Dnf), "arch" => Box::new(Pacman), "gentoo" => Box::new(Portage), _ => return Err(Error::Unsupported), @@ -73,6 +77,7 @@ impl Manager { fn to_package_manager(&self) -> Box { match self { + Self::Dnf => Box::new(Dnf), Self::Pacman => Box::new(Pacman), Self::Portage => Box::new(Portage), } 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