From e8b05e2a89416af267bd904cc8365f6a2da1a5fd Mon Sep 17 00:00:00 2001 From: Amy Date: Wed, 3 Nov 2021 19:34:52 +0100 Subject: [PATCH] now able to install packages from a list with -Sl or insl, packages in list must be seperated by newline --- src/main.rs | 5 ++- src/mods/database.rs | 2 +- src/mods/inssort.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++ src/mods/install.rs | 57 ++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 906a734..80a3f4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod mods; -use mods::{clearcache::{clearcache}, clone::clone, help::help, inssort::inssort, install::install, purge::{purge}, search::{a_search, r_search}, strs::err_rec, strs::err_unrec, strs::inf, uninstall::{uninstall}, update::{update}, upgrade::{upgrade}, ver::ver, xargs::*}; +use mods::{clearcache::{clearcache}, clone::clone, help::help, inssort::{inssort, inssort_from_file}, install::install, purge::{purge}, search::{a_search, r_search}, strs::err_rec, strs::err_unrec, strs::inf, uninstall::{uninstall}, update::{update}, upgrade::{upgrade}, ver::ver, xargs::*}; use std::{env, process::exit, process::Command}; use nix::unistd::Uid; @@ -26,6 +26,9 @@ fn main() { "-S" | "-Sn" | "ins" => { inssort(noconfirm, false, pkgs); } + "-Sl" | "-Sln" | "insl" => { + inssort_from_file(noconfirm, false, &pkgs[0]); + } "-R" | "-Rn" | "rm" => { uninstall(noconfirm, pkgs); } diff --git a/src/mods/database.rs b/src/mods/database.rs index 9d4115a..e460f3e 100644 --- a/src/mods/database.rs +++ b/src/mods/database.rs @@ -169,4 +169,4 @@ pub fn add_pkg(from_repo: bool, pkg: &str) -> Result<(), Error> { .write_all(format!("{}", db_parsed).as_bytes()) .unwrap(); Ok(()) -} +} \ No newline at end of file diff --git a/src/mods/inssort.rs b/src/mods/inssort.rs index eb4c39b..021d321 100644 --- a/src/mods/inssort.rs +++ b/src/mods/inssort.rs @@ -90,3 +90,100 @@ pub fn inssort(noconfirm: bool, as_dep: bool, pkgs: Vec) { } } } + +//this function is used to install packages from a file +pub fn inssort_from_file(noconfirm: bool, as_dep: bool, file: &str) { + let mut pkgs: Vec = Vec::new(); + let mut contents = String::new(); + contents = std::fs::read_to_string(&file).expect("Couldn't read file"); + for line in contents.lines() { + println!("{}", line); + pkgs.push(line.to_string()); + } + let mut repo = vec![]; + let mut aur = vec![]; + let re = Regex::new(r"(\S+)((?:>=|<=)\S+$)").unwrap(); + let reg = Regex::new(r"((?:>=|<=)\S+$)").unwrap(); + for pkg in pkgs { + match pkg.contains("/") { + true => { + match pkg.split("/").collect::>()[0] == "aur" { + true => { + aur.push(pkg.split("/").collect::>()[1].to_string()); + } + false => { + let out = Command::new("bash") + .arg("-c") + .arg(format!("pacman -Sl {} | grep {}", pkg.split("/").collect::>()[0],pkg.split("/").collect::>()[1])) + .stdout(Stdio::null()) + .status() + .expect("Something has gone wrong."); + match out.code() { + Some(0) => repo.push(reg.replace_all(&pkg, "").to_string()), + Some(1) => err_unrec(format!("Package {} not found in repository {}", pkg.split("/").collect::>()[1],pkg.split("/").collect::>()[0])), + Some(_) => err_unrec(format!("Something has gone terribly wrong")), + None => err_unrec(format!("Process terminated")), + } + } + } + } + false => { + let caps = re.captures(&pkg); + match caps { + Some(_) => { + let out = Command::new("pacman") + .arg("-Ss") + .arg(format!( + "^{}$", + caps.unwrap().get(1).map_or("", |m| m.as_str()) + )) + .stdout(Stdio::null()) + .status() + .expect("Something has gone wrong."); + match out.code() { + Some(0) => repo.push(reg.replace_all(&pkg, "").to_string()), + Some(1) => aur.push(pkg), + Some(_) => err_unrec(format!("Something has gone terribly wrong")), + None => err_unrec(format!("Process terminated")), + } + } + None => { + let out = Command::new("pacman") + .arg("-Ss") + .arg(format!("^{}$", &pkg)) + .stdout(Stdio::null()) + .status() + .expect("Something has gone wrong."); + match out.code() { + Some(0) => repo.push(pkg), + Some(1) => aur.push(pkg), + Some(_) => err_unrec(format!("Something has gone terribly wrong")), + None => err_unrec(format!("Process terminated")), + } + } + } + } + } + } + if as_dep == false { + if repo.len() != 0 { + sec(format!("Installing repo packages: {}", &repo.join(", "))); + install(noconfirm, false, &repo.join(" ")); + } + + for a in aur { + sec(format!("Couldn't find {} in repos. Searching AUR", a)); + clone(noconfirm, false, &a); + } + } else { + if repo.len() != 0 { + sec(format!("Installing repo packages: {}", &repo.join(", "))); + install(noconfirm, true,&repo.join(" ")); + } + + for a in aur { + sec(format!("Couldn't find {} in repos. Searching AUR", a)); + clone(noconfirm, true, &a); + } + } +} \ No newline at end of file diff --git a/src/mods/install.rs b/src/mods/install.rs index 5d427f1..eb5ad83 100644 --- a/src/mods/install.rs +++ b/src/mods/install.rs @@ -1,5 +1,6 @@ use crate::mods::strs::{err_unrec, succ}; use runas::Command; +use std::fs::File; pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { let pkgs: Vec<&str> = pkg.split(" ").collect(); @@ -46,3 +47,59 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { }; } } + +/* +//this function is used to install packages from a file +pub fn install_from_file(noconfirm: bool, as_dep: bool, file: &str) { + let mut pkgs: Vec<&str> = Vec::new(); + // let mut file = File::open(file).expect("Couldn't open file"); + let mut contents = String::new(); + contents = std::fs::read_to_string(&file).expect("Couldn't read file"); + for line in contents.lines() { + println!("{}", line); + pkgs.push(line); + } + if as_dep == false { + if noconfirm == true { + let result = Command::new("pacman") + .arg("-S") + .arg("--noconfirm") + .arg("--needed") + .args(&pkgs) + .status() + .expect("Couldn't call pacman"); + match result.code() { + Some(0) => succ(format!("Succesfully installed packages: {}", contents)), + Some(_) => err_unrec(format!("Couldn't install packages: {}", contents)), + None => err_unrec(format!("Couldn't install packages: {}", contents)), + }; + } else { + let result = Command::new("pacman") + .arg("-S") + .arg("--needed") + .args(&pkgs) + .status() + .expect("Couldn't call pacman"); + match result.code() { + Some(0) => succ(format!("Succesfully installed packages: {}", contents)), + Some(_) => err_unrec(format!("Couldn't install packages: {}", contents)), + None => err_unrec(format!("Couldn't install packages: {}", contents)), + }; + } + } else { + let result = Command::new("pacman") + .arg("-S") + .arg("--noconfirm") + .arg("--needed") + .arg("--asdeps") + .args(&pkgs) + .status() + .expect("Couldn't call pacman"); + match result.code() { + Some(0) => succ(format!("Succesfully installed packages: {}", contents)), + Some(_) => err_unrec(format!("Couldn't install packages: {}", contents)), + None => err_unrec(format!("Couldn't install packages: {}", contents)), + }; + } +} +*/ \ No newline at end of file