From 1b99a4a1ed7772c9b68e59f46e493ea5b4715239 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 14 Sep 2024 14:20:52 +0200 Subject: add portage package manager --- src/packages/portage.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'src/packages/portage.rs') diff --git a/src/packages/portage.rs b/src/packages/portage.rs index 7fa09a8..dac0c2f 100644 --- a/src/packages/portage.rs +++ b/src/packages/portage.rs @@ -1,16 +1,63 @@ -use tracing::error; +use std::process::{Command, Stdio}; -use super::{PackageList, PackageManager}; +use crate::error::Error; + +use super::{Package, PackageList, PackageManager}; pub struct Portage; impl PackageManager for Portage { fn get_installed(&self) -> crate::error::Result { - todo!() + let eix_pkgs = Command::new("eix") + .args(["--world", "-c*"]) + .output() + .unwrap(); + + let eix_pkgs_out = String::from_utf8(eix_pkgs.stdout).unwrap(); + + let mut pkgs: Vec = Vec::new(); + let portage_pkgs: Vec<&str> = eix_pkgs_out.split('\n').collect(); + for pkg in portage_pkgs { + if pkg.is_empty() { + continue; + }; + let split = pkg.split_once('@'); + let info = if let Some(sp) = split { + sp.0 + } else { + return Err(Error::UnknownOutput); + }; + + let (id, version) = if let Some(res) = info.split_once('(') { + (res.0.split_at(4).1.trim().to_string(), res.1.to_string()) + } else { + return Err(Error::UnknownOutput); + }; + + pkgs.push(Package { + id, + version, + explicit: true, + }); + } + + Ok(PackageList { + packages: pkgs, + manager: super::Manager::Portage, + }) } fn install(&self, pkgs: Vec) -> crate::error::Result<()> { - error!("Install {pkgs:?}"); - todo!() + let mut args = vec!["emerge".to_string()]; + + for pkg in pkgs { + args.push(pkg.id); + } + Command::new("doas") + .stdout(Stdio::inherit()) + .args(args) + .spawn()? + .wait_with_output()?; + Ok(()) } } -- cgit v1.2.3