diff --git a/src/main.rs b/src/main.rs index c764940..b71b114 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, 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, 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}; fn main() { @@ -18,11 +18,14 @@ fn main() { match oper.as_str() { "-S" | "-Sn" | "ins" => { - inssort(noconfirm, pkgs); + inssort(noconfirm, false, pkgs); } - "-R" | "-Rn" | "-Rsn" | "-Rs" | "rm" => { + "-R" | "-Rn" | "rm" => { uninstall(noconfirm, pkgs); } + "-Rs" | "-Rsn" | "purge" => { + purge(noconfirm, pkgs) + } "-Syu" | "-Syun" |"upg" => { upgrade(noconfirm); } diff --git a/src/mods.rs b/src/mods.rs index 55a48a6..4a85e02 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -11,3 +11,4 @@ pub mod update; pub mod upgrade; pub mod ver; pub mod xargs; +pub mod purge; \ No newline at end of file diff --git a/src/mods/clone.rs b/src/mods/clone.rs index ab660c9..451c6bf 100644 --- a/src/mods/clone.rs +++ b/src/mods/clone.rs @@ -23,7 +23,7 @@ fn uninstall_make_depend(pkg: &str) { succ(format!("Succesfully installed {}", pkg)); } -pub fn clone(noconfirm: bool, pkg: &str) { +pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { let cachedir = format!("{}/.cache/ame", std::env::var("HOME").unwrap()); let path = Path::new(&cachedir); let pkgdir = format!("{}/{}", &cachedir, &pkg); @@ -73,7 +73,7 @@ pub fn clone(noconfirm: bool, pkg: &str) { sec(format!("Installing AUR package depends")); - inssort(noconfirm, package[0].depends.clone()); + inssort(noconfirm, true, package[0].depends.clone()); let clone = Repository::clone(&url, Path::new(&pkgdir)); match clone { @@ -82,58 +82,80 @@ pub fn clone(noconfirm: bool, pkg: &str) { } Err(_) => err_unrec(format!("Failed cloning {} into package directory", pkg)), } + if as_dep == false { + if noconfirm == false { + let pkgbuild = prompt(format!("View PKGBUILD?")); - if noconfirm == false { - let pkgbuild = prompt(format!("View PKGBUILD?")); - - if pkgbuild == true { - let mut pkgbld = fs::read_to_string(format!("{}/PKGBUILD", &pkgdir)).unwrap(); - Moins::run(&mut pkgbld, None); + if pkgbuild == true { + let mut pkgbld = fs::read_to_string(format!("{}/PKGBUILD", &pkgdir)).unwrap(); + Moins::run(&mut pkgbld, None); + } } - } - if noconfirm == true { - sec(format!("Installing {} ...", pkg)); - let install_result = Command::new("makepkg") - .arg("-si") - .arg("--noconfirm") - .arg("--needed") - .status(); - match install_result { - Ok(_) => { - uninstall_make_depend(pkg); - let add_pkg_res = add_pkg(false, pkg); - match add_pkg_res { - Ok(_) => inf(format!("Added package {} to database", pkg)), - Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), + if noconfirm == true { + sec(format!("Installing {} ...", pkg)); + let install_result = Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .arg("--needed") + .status(); + match install_result { + Ok(_) => { + uninstall_make_depend(pkg); + let add_pkg_res = add_pkg(false, pkg); + match add_pkg_res { + Ok(_) => inf(format!("Added package {} to database", pkg)), + Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), + } } - } - Err(_) => { - err_unrec(format!("Couldn't install {}", pkg)); - } - }; + Err(_) => { + err_unrec(format!("Couldn't install {}", pkg)); + } + }; + } else { + sec(format!("Installing {} ...", pkg)); + let install_result = Command::new("makepkg") + .arg("-si") + .arg("--needed") + .status() + .expect("Couldn't call makepkg"); + match install_result.code() { + Some(0) => { + uninstall_make_depend(pkg); + let add_pkg_res = add_pkg(false, pkg); + match add_pkg_res { + Ok(_) => inf(format!("Added package {} to database", pkg)), + Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), + } + } + Some(_) => { + err_unrec(format!("Couldn't install {}", pkg)); + } + None => { + err_unrec(format!("Couldn't install {}", pkg)); + } + }; + } } else { sec(format!("Installing {} ...", pkg)); - let install_result = Command::new("makepkg") - .arg("-si") - .arg("--needed") - .status() - .expect("Couldn't call makepkg"); - match install_result.code() { - Some(0) => { - uninstall_make_depend(pkg); - let add_pkg_res = add_pkg(false, pkg); - match add_pkg_res { - Ok(_) => inf(format!("Added package {} to database", pkg)), - Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), + let install_result = Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .arg("--needed") + .arg("--asdeps") + .status(); + match install_result { + Ok(_) => { + uninstall_make_depend(pkg); + let add_pkg_res = add_pkg(false, pkg); + match add_pkg_res { + Ok(_) => inf(format!("Added package {} to database", pkg)), + Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), + } } - } - Some(_) => { - err_unrec(format!("Couldn't install {}", pkg)); - } - None => { - err_unrec(format!("Couldn't install {}", pkg)); - } - }; + Err(_) => { + err_unrec(format!("Couldn't install {}", pkg)); + } + }; } } diff --git a/src/mods/database.rs b/src/mods/database.rs index 1044a20..78de816 100644 --- a/src/mods/database.rs +++ b/src/mods/database.rs @@ -4,6 +4,74 @@ use std::io::{Error, Write}; use toml_edit::{value, Document}; use crate::mods::strs::{err_rec}; +pub fn get_value(pkg: &str, sear_value: &str) -> String { + let homepath = std::env::var("HOME").unwrap(); + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let mut database = String::new(); + match std::path::Path::new(&file).exists() { + true => { + database = std::fs::read_to_string(&file).expect("Can't Open Database"); + } + false => { + let _cdar = std::fs::create_dir_all(format!("/{}/.local/ame/",homepath)); + match _cdar { + Ok(_) => { + inf(format!("Created path for database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create path for database (~/.local/ame)")) + } + } + err_rec(String::from("Datbase wasn't found, creating new one")); + let _dbfile = std::fs::File::create(&file); + match _dbfile { + Ok(_) => { + inf(format!("Created empty database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create database")) + } + } + } + } + let db_parsed = database.parse::().expect("Invalid Database"); + + let mut return_val = String::new(); + for entry in db_parsed.as_table() { + for (key, value) in &*entry { + if key.contains(pkg) { + let results = raur::search(format!("{}",key)); + for res in results { + match sear_value { + "name" => { + return_val = value + .to_string() + .replace("name", "") + .replace("version", "") + .replace(" = ", "") + .replace("\"", "") + .replace(format!("{}", &res[0].version.to_string()).as_str(), ""); + } + "version" => { + return_val = value + .to_string() + .replace("name", "") + .replace("version", "") + .replace(" = ", "") + .replace("\"", "") + .replace(format!("{}", &res[0].name.to_string()).as_str(), ""); + } + _ => { + err_unrec(format!("")); + } + } + } + } + } + } + return return_val; +} + pub fn rem_pkg(pkgs: &Vec) { let homepath = std::env::var("HOME").unwrap(); let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); @@ -91,10 +159,10 @@ pub fn add_pkg(from_repo: bool, pkg: &str) -> Result<(), Error> { if from_repo == false { let results = raur::search(&pkg); for res in &results { - for r in res { - db_parsed[&r.name]["name"] = value(&r.name); - db_parsed[&r.name]["version"] = value(&r.version); - } + //for r in res { + db_parsed[&res[0].name]["name"] = value(&res[0].name); + db_parsed[&res[0].name]["version"] = value(&res[0].version); + //} } } else { db_parsed[&pkg]["name"] = value(pkg); diff --git a/src/mods/inssort.rs b/src/mods/inssort.rs index 36b50c1..c9eceab 100644 --- a/src/mods/inssort.rs +++ b/src/mods/inssort.rs @@ -1,7 +1,7 @@ use crate::{clone, err_unrec, install, mods::strs::sec}; use std::process::{Command, Stdio}; -pub fn inssort(noconfirm: bool, pkgs: Vec) { +pub fn inssort(noconfirm: bool, as_dep: bool, pkgs: Vec) { let mut repo = vec![]; let mut aur = vec![]; let re = regex::Regex::new(r"(\S+)((?:>=|<=)\S+$)").unwrap(); @@ -42,13 +42,25 @@ pub fn inssort(noconfirm: bool, pkgs: Vec) { } } } - if repo.len() != 0 { - sec(format!("Installing repo packages: {}", &repo.join(", "))); - install(noconfirm, &repo.join(" ")); - } + 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, &a); + for a in aur { + sec(format!("Couldn't find {} in repos. Searching AUR", a)); + clone(noconfirm, true, &a); + } } } diff --git a/src/mods/install.rs b/src/mods/install.rs index 1d86e80..5d427f1 100644 --- a/src/mods/install.rs +++ b/src/mods/install.rs @@ -1,25 +1,41 @@ use crate::mods::strs::{err_unrec, succ}; use runas::Command; -pub fn install(noconfirm: bool, pkg: &str) { +pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { let pkgs: Vec<&str> = pkg.split(" ").collect(); - if noconfirm == true { - let result = Command::new("pacman") - .arg("-Sy") - .arg("--noconfirm") - .arg("--needed") - .args(&pkgs) - .status() - .expect("Couldn't call pacman"); - match result.code() { - Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), - Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), - None => err_unrec(format!("Couldn't install packages: {}", pkg)), - }; + 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: {}", pkg)), + Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), + None => err_unrec(format!("Couldn't install packages: {}", pkg)), + }; + } 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: {}", pkg)), + Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), + None => err_unrec(format!("Couldn't install packages: {}", pkg)), + }; + } } else { let result = Command::new("pacman") - .arg("-Sy") + .arg("-S") + .arg("--noconfirm") .arg("--needed") + .arg("--asdeps") .args(&pkgs) .status() .expect("Couldn't call pacman"); diff --git a/src/mods/purge.rs b/src/mods/purge.rs new file mode 100644 index 0000000..6f5d0fb --- /dev/null +++ b/src/mods/purge.rs @@ -0,0 +1,60 @@ +use crate::mods::{ + database::rem_pkg, + strs::{err_rec, err_unrec, sec, succ}, +}; +use runas::Command; +use std::{fs, path::Path}; + +pub fn purge(noconfirm: bool, pkgs: Vec) { + sec(format!( + "Attempting to uninstall packages: {}", + &pkgs.join(" ") + )); + if noconfirm == true { + let result = Command::new("pacman") + .arg("-Rs") + .args(&pkgs) + .arg("--noconfirm") + .status() + .expect("Couldn't call pacman"); + match result.code() { + Some(0) => { + succ(format!( + "Succesfully uninstalled packages: {}", + &pkgs.join(" ") + )); + rem_pkg(&pkgs); + } + Some(_) => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), + None => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), + }; + } else { + let result = Command::new("pacman") + .arg("-Rs") + .args(&pkgs) + .status() + .expect("Couldn't call pacman"); + match result.code() { + Some(0) => { + succ(format!( + "Succesfully uninstalled packages: {}", + &pkgs.join(" ") + )); + rem_pkg(&pkgs); + } + Some(_) => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), + None => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), + }; + } + for pkg in &pkgs { + let pkgdir = format!("{}/.cache/ame/{}", std::env::var("HOME").unwrap(), pkg); + let path = Path::new(&pkgdir); + if path.is_dir() { + let rm_result = fs::remove_dir_all(&path); + match rm_result { + Ok(_) => succ(format!("Removed AUR cache directory for {}", pkg)), + Err(_) => err_unrec(format!("Failed to remove AUR cache directory for {}", pkg)), + }; + } + } +} diff --git a/src/mods/uninstall.rs b/src/mods/uninstall.rs index 72738b6..71bb091 100644 --- a/src/mods/uninstall.rs +++ b/src/mods/uninstall.rs @@ -12,7 +12,7 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { )); if noconfirm == true { let result = Command::new("pacman") - .arg("-Rs") + .arg("-R") .args(&pkgs) .arg("--noconfirm") .status() @@ -30,7 +30,7 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { }; } else { let result = Command::new("pacman") - .arg("-Rs") + .arg("-R") .args(&pkgs) .status() .expect("Couldn't call pacman"); diff --git a/src/mods/upgrade.rs b/src/mods/upgrade.rs index 5b98486..b339844 100644 --- a/src/mods/upgrade.rs +++ b/src/mods/upgrade.rs @@ -1,5 +1,5 @@ use crate::{ - err_rec, err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, uninstall, + err_rec, err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, uninstall, mods::database::get_value, }; use git2::Repository; use runas::Command; @@ -78,29 +78,13 @@ pub fn upgrade(noconfirm: bool) { }; } - println!("{:?}", db_parsed); for entry in db_parsed.as_table() { - for (key, value) in &*entry { + for (key, _value) in &*entry { let results = raur::search(format!("{}", key)); for res in results { - println!("{}", &res[0].name); let url = format!("https://aur.archlinux.org/{}.git", key); let package = raur::info(&[key]).unwrap(); - let version = value - .to_string() - .replace("name", "") - .replace("version", "") - .replace(" = ", "") - .replace("\"", "") - .replace(format!("{}", &res[0].name.to_string()).as_str(), ""); - let name = value - .to_string() - .replace("name", "") - .replace("version", "") - .replace(" = ", "") - .replace("\"", "") - .replace(format!("{}", &res[0].version.to_string()).as_str(), ""); - println!("{} / {}", name, version); + let version = get_value(&key, "version"); if res[0].version.contains(&version) { let keydir = format!("{}{}", &cachedir, &key); if std::path::Path::new(&keydir).is_dir() { @@ -109,7 +93,7 @@ pub fn upgrade(noconfirm: bool) { Ok(_) => inf(format!("Entered package directory")), Err(_) => err_unrec(format!("Could not enter package directory")), } - inssort(true, package[0].depends.clone()); + inssort(true, true,package[0].depends.clone()); sec(format!("Installing {} ...", &key)); let install_result = std::process::Command::new("makepkg") @@ -174,7 +158,7 @@ pub fn upgrade(noconfirm: bool) { Err(_) => err_unrec(format!("Could not enter package directory")), } - inssort(true, package[0].depends.clone()); + inssort(true, true, package[0].depends.clone()); let clone = Repository::clone(&url, Path::new(&keydir)); match clone { @@ -219,12 +203,7 @@ pub fn upgrade(noconfirm: bool) { } }; } else { - println!("not upgrading!"); - if std::path::Path::new(&format!("{}{}", &cachedir, &key)).is_dir() { - println!("not cloning"); - } else { - println!("cloning"); - } + inf(format!("Package {} already up to date", &key)); } } }