summaryrefslogblamecommitdiff
path: root/src/packages/pacman.rs
blob: 0ad463b13e054e9c4640681a40dd6b2eb67a2da4 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                   
 
                                                  
 
                                  



                                
                                                    
















                                                                            
                                                 






                                                       
                         
               

         



                                            

     
                                                                
                                                                                               



                              
                            




                                     

     
use std::process::{Command, Stdio};

use super::{Package, PackageList, PackageManager};

use crate::error::{Error, Result};

pub struct Pacman;

impl PackageManager for Pacman {
    fn get_installed(&self) -> Result<PackageList> {
        let pm_pkgs = Command::new("pacman").args(["-Q"]).output().unwrap();
        let pm_e_pkgs = Command::new("pacman")
            .args(["-Q", "--explicit"])
            .output()
            .unwrap();

        let pm_pkgs_out = String::from_utf8(pm_pkgs.stdout).unwrap();
        let pm_e_pkgs_out = String::from_utf8(pm_e_pkgs.stdout).unwrap();

        let mut pkgs: Vec<Package> = Vec::new();
        let pacman_pkgs: Vec<&str> = pm_pkgs_out.split('\n').collect();
        for pkg in pacman_pkgs {
            if pkg.is_empty() {
                continue;
            };
            let split: Vec<&str> = pkg.split_whitespace().collect();
            if split.len() != 2 {
                return Err(Error::UnknownOutput);
            };

            let explicit = pm_e_pkgs_out.contains(pkg);

            pkgs.push(Package {
                id: split[0].to_string(),
                version: split[1].to_string(),
                explicit,
            });
        }

        Ok(PackageList {
            packages: pkgs,
            manager: super::Manager::Pacman,
        })
    }

    fn install(&self, pkgs: Vec<super::Package>) -> Result<()> {
        let mut args = vec!["pacman".to_string(), "--noconfirm".to_string(), "-S".to_string()];

        for pkg in pkgs {
            args.push(pkg.id);
        }
        Command::new("doas")
            .stdout(Stdio::inherit())
            .args(args)
            .spawn()?
            .wait_with_output()?;
        Ok(())
    }
}