From 12cf74ce500aa9b0cdee7dd8b5cbdf492e067797 Mon Sep 17 00:00:00 2001 From: jnats Date: Sat, 25 Sep 2021 13:23:41 +0100 Subject: [PATCH] added install sorting and noconfirm --- Cargo.toml | 2 +- src/main.rs | 72 +++++++++++++++++++++++++++++++++---------- src/mods/clone.rs | 37 +++++++++++++++------- src/mods/install.rs | 28 ++++++++++++----- src/mods/uninstall.rs | 27 +++++++++++----- src/mods/upgrade.rs | 34 ++++++++++++++------ 6 files changed, 145 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88bd916..d67d99c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ame" -version = "3.0.0" +version = "2.1.0" authors = [ "jnats ", "axtlos " ] edition = "2018" description = "a fast and efficient aur helper." diff --git a/src/main.rs b/src/main.rs index 6a0e4ec..d39a112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ + mod mods; -use mods::{clearcache::clearcache, clone::clone, help::help, install::install, search::{a_search, r_search}, uninstall::uninstall, upgrade::upgrade, update::update, ver::ver, strs::inf}; +use mods::{clearcache::clearcache, clone::clone, help::help, install::install, search::{a_search, r_search}, uninstall::uninstall, upgrade::upgrade, update::update, ver::ver, strs::inf, strs::err_unrec}; use std::{env, process::exit, process::Command, process::Stdio}; fn main() { @@ -17,32 +18,69 @@ fn main() { let oper = &args[1]; // install - if oper == "-S" || oper == "ins" { - for arg in env::args().skip(2) { + if oper == "-S" || oper == "-Sn" || oper == "ins" { + let pkgs = env::args().skip(2); + let mut repo = vec![]; + let mut aur = vec![]; + for pkg in pkgs { let out = Command::new("pacman") .arg("-Ss") - .arg(&arg) + .arg(&pkg) .stdout(Stdio::null()) .status() - .expect(""); - if out.code() == Some(0) { - inf(format!("Installing {}", arg)); - install(&arg); - } else { - inf(format!("Cloning {} from the AUR", arg)); - clone(&arg); + .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 repo.len() != 0 { + inf(format!("Installing repo packages: {}", &repo.join(", "))); + if oper == "-Sn" { + install(true, &repo.join(" ")); + } else { + install(false, &repo.join(" ")); + } + } + + for a in aur { + inf(format!("Installing AUR package: {}", a)); + if oper == "-Sn" { + clone(true, &a); + } else { + clone(false, &a); + } } - } // remove - } else if oper == "-R" || oper == "-Rs" || oper == "rm" { - for arg in env::args().skip(2) { - uninstall(&arg); + } else if oper == "-R" || oper == "-Rn " || oper == "-Rsn" || oper == "-Rs" || oper == "rm" { + if oper == "-Rn" || oper == "-Rsn" { + for arg in env::args().skip(2) { + uninstall(true, &arg); + } + } else { + for arg in env::args().skip(2) { + uninstall(false, &arg); + } } // upgrade - } else if oper == "-Syu" || oper == "upg" { - upgrade(&cache_path); + } else if oper == "-Syu" || oper == "-Syun" || oper == "upg" { + inf(format!("Performing system upgrade")); + if oper == ("-Syun") { + upgrade(true, &cache_path); + } else { + upgrade(false, &cache_path); + } // update } else if oper == "-Sy" || oper == "upd" { diff --git a/src/mods/clone.rs b/src/mods/clone.rs index f3d3fb3..a9c9705 100644 --- a/src/mods/clone.rs +++ b/src/mods/clone.rs @@ -2,7 +2,7 @@ use git2::Repository; use std::{env, fs, path::Path, process::Command}; use crate::mods::strs::{err_unrec, inf}; -pub fn clone(pkg: &str) { +pub fn clone(noconfirm: bool, pkg: &str) { let cachedir = format!("{}/.cache/ame", std::env::var("HOME").unwrap()); let path = Path::new(&cachedir); let pkgdir = format!("{}/{}", &cachedir, &pkg); @@ -68,15 +68,30 @@ pub fn clone(pkg: &str) { err_unrec(format!("Couldn't enter package directory for {}", pkg)) }} - inf(format!("Installing {} ...", pkg)); - let install_result = Command::new("makepkg") - .arg("-si") - .status(); - match install_result { - Ok(_) => { - inf(format!("Succesfully installed {}", pkg)); + if noconfirm == true { + inf(format!("Installing {} ...", pkg)); + let install_result = Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .status(); + match install_result { + Ok(_) => { + inf(format!("Succesfully installed {}", pkg)); + } + Err(_) => { + err_unrec(format!("Couldn't install {}", pkg)); + }}; + } else { + inf(format!("Installing {} ...", pkg)); + let install_result = Command::new("makepkg") + .arg("-si") + .status(); + match install_result { + Ok(_) => { + inf(format!("Succesfully installed {}", pkg)); + } + Err(_) => { + err_unrec(format!("Couldn't install {}", pkg)); + }}; } - Err(_) => { - err_unrec(format!("Couldn't install {}", pkg)); - }}; } diff --git a/src/mods/install.rs b/src/mods/install.rs index f0cb5bb..aaa40d3 100644 --- a/src/mods/install.rs +++ b/src/mods/install.rs @@ -1,13 +1,25 @@ use runas::Command; use crate::mods::strs::{inf, err_unrec}; -pub fn install(pkg: &str) { - let result = Command::new("pacman").arg("-Sy").arg(&pkg).status(); - match result { - Ok(_) => { - inf(format!("Succesfully installed {}", pkg)) +pub fn install(noconfirm: bool, pkg: &str) { + let pkgs: Vec<&str> = pkg.split(" ").collect(); + if noconfirm == true { + let result = Command::new("pacman").arg("-Sy").arg("--noconfirm").args(&pkgs).status(); + match result { + Ok(_) => { + inf(format!("Succesfully installed packages: {}", pkg)) + } + Err(_) => { + err_unrec(format!("Couldn't install packages: {}", pkg)) + }}; + } else { + let result = Command::new("pacman").arg("-Sy").args(&pkgs).status(); + match result { + Ok(_) => { + inf(format!("Succesfully installed packages: {}", pkg)) + } + Err(_) => { + err_unrec(format!("Couldn't install packages: {}", pkg)) + }}; } - Err(_) => { - err_unrec(format!("Couldn't install {}", pkg)) - }}; } diff --git a/src/mods/uninstall.rs b/src/mods/uninstall.rs index a420255..448ed33 100644 --- a/src/mods/uninstall.rs +++ b/src/mods/uninstall.rs @@ -1,14 +1,25 @@ use runas::Command; use crate::mods::strs::{inf, err_unrec}; -pub fn uninstall(pkg: &str) { +pub fn uninstall(noconfirm: bool, pkg: &str) { inf(format!("Attempting to uninstall {}", pkg)); - let result = Command::new("pacman").arg("-Rs").arg(&pkg).status(); - match result { - Ok(_) => { - println!("") + if noconfirm == true { + let result = Command::new("pacman").arg("-Rs").arg(&pkg).arg("--noconfirm").status(); + match result { + Ok(_) => { + println!("") + } + Err(_) => { + err_unrec(format!("Couldn't uninstall {}", pkg)) + }}; + } else { + let result = Command::new("pacman").arg("-Rs").arg(&pkg).status(); + match result { + Ok(_) => { + println!("") + } + Err(_) => { + err_unrec(format!("Couldn't uninstall {}", pkg)) + }}; } - Err(_) => { - err_unrec(format!("Couldn't uninstall {}", pkg)) - }}; } diff --git a/src/mods/upgrade.rs b/src/mods/upgrade.rs index 67709c0..2767a44 100644 --- a/src/mods/upgrade.rs +++ b/src/mods/upgrade.rs @@ -2,17 +2,31 @@ use runas::Command; use std::env; use crate::mods::strs::{err_unrec, inf}; -pub fn upgrade(cachedir: &str){ - let result = Command::new("pacman") - .arg("-Syu") - .status(); - match result { - Ok(_) => { - inf(format!("All repo packages upgraded")) +pub fn upgrade(noconfirm: bool, cachedir: &str){ + if noconfirm == true { + let result = Command::new("pacman") + .arg("-Syu") + .arg("--noconfirm") + .status(); + match result { + Ok(_) => { + inf(format!("All repo packages upgraded")) + } + Err(_) => { + err_unrec(format!("Couldn't upgrade packages")) + }}; + } else { + let result = Command::new("pacman") + .arg("-Syu") + .status(); + match result { + Ok(_) => { + inf(format!("All repo packages upgraded")) + } + Err(_) => { + err_unrec(format!("Couldn't upgrade packages")) + }}; } - Err(_) => { - err_unrec(format!("Couldn't upgrade packages")) - }}; for file in std::fs::read_dir(&cachedir).unwrap() { let dir = &file.unwrap().path();