From 05d8940b304807761fcf6f33d9879fc335a41139 Mon Sep 17 00:00:00 2001 From: axtlos <3alouchi2006@gmail.com> Date: Sun, 10 Oct 2021 23:55:25 +0200 Subject: [PATCH 1/3] fix my messup with testing part 2 --- Cargo.toml | 3 ++ main.rs | 80 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/mods.rs | 1 + src/mods/clone.rs | 4 ++- src/mods/database.rs | 46 +++++++++++++++++++++++++ src/mods/uninstall.rs | 18 ++++++---- src/mods/upgrade.rs | 36 +++++++++++++++++-- 8 files changed, 179 insertions(+), 10 deletions(-) create mode 100644 main.rs create mode 100644 src/mods/database.rs diff --git a/Cargo.toml b/Cargo.toml index 9f98560..c460000 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,6 @@ ansi_term = "*" uwuizer = "*" moins = "*" regex = "*" +toml_edit = "*" +toml = "*" +bytes = "*" \ No newline at end of file diff --git a/main.rs b/main.rs new file mode 100644 index 0000000..84b65d3 --- /dev/null +++ b/main.rs @@ -0,0 +1,80 @@ + +mod mods; +use mods::{clearcache::clearcache, clone::clone, help::help, install::install, inssort::inssort, search::{a_search, r_search}, uninstall::uninstall, upgrade::upgrade, update::update, ver::ver, strs::inf, strs::err_unrec, strs::err_rec, xargs::*, database::addPkg}; +use std::{env, process::exit, process::Command}; + +fn main() { + // let statements + let args: Vec = env::args().collect(); + let mut pkgs: Vec = env::args().skip(2).collect(); + + // args catch + if args.len() <= 1 { + help(); + exit(1); + } + + let oper = &args[1]; + let noconfirm: bool = noconf(&args); + + argssort(&mut pkgs); + + // install + if oper == "-S" || oper == "-Sn" || oper == "ins" { + inssort(noconfirm, pkgs); + + // remove + } else if oper == "-R" || oper == "-Rn " || oper == "-Rsn" || oper == "-Rs" || oper == "rm" { + uninstall(noconfirm, pkgs); + + // upgrade + } else if oper == "-Syu" || oper == "-Syun" || oper == "upg" { + upgrade(noconfirm); + + // update + } else if oper == "-Sy" || oper == "upd" { + update(); + } else if oper == "-db" { + addPkg(pkgs); + // general search + } else if oper == "-Ss" || oper == "sea" { + r_search(&args[2]); + a_search(&args[2]); + + // aur search + } else if oper == "-Sa" || oper == "aursea" { + a_search(&args[2]); + + // repo search + } else if oper == "-Sr" || oper == "repsea" { + r_search(&args[2]); + + // clear cache !! DEBUG ONLY !! DO NOT DO THIS IF YOU DONT KNOW WHAT YOURE DOING !! + } else if oper == "-Cc" || oper == "clr" { + clearcache(); + + // version / contrib + } else if oper == "-v" || oper == "-V" || oper == "ver" { + ver(); + + // help + } else if oper == "-h" || oper == "help" { + help(); + // pacman passthrough + } else { + let pass = Command::new("pacman") + .args(env::args().skip(1)) + .status() + .expect("Something has gone wrong."); + + match pass.code() { + Some(1) => { + err_rec(format!("No such operation \"{}\"", args.join(" "))); + inf(format!("Try running \"ame help\" for an overview of how to use ame")) + } + Some(_) => {} + None => { + err_unrec(format!("Something has gone terribly wrong.")) + }} + } +} diff --git a/src/main.rs b/src/main.rs index fdcf379..9d71995 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use mods::{ upgrade::upgrade, ver::ver, xargs::*, + database::{addPkg, remPkg} }; use std::{env, process::exit, process::Command}; diff --git a/src/mods.rs b/src/mods.rs index 9581b1f..adff73a 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -10,3 +10,4 @@ pub mod update; pub mod upgrade; pub mod ver; pub mod xargs; +pub mod database; \ No newline at end of file diff --git a/src/mods/clone.rs b/src/mods/clone.rs index 9ad657c..0d64410 100644 --- a/src/mods/clone.rs +++ b/src/mods/clone.rs @@ -1,6 +1,6 @@ use crate::{ err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, - mods::uninstall::uninstall, + mods::uninstall::uninstall, mods::database::addPkg }; use git2::Repository; use moins::Moins; @@ -102,6 +102,7 @@ pub fn clone(noconfirm: bool, pkg: &str) { match install_result { Ok(_) => { uninstall_make_depend(pkg); + addPkg(false, pkg); } Err(_) => { err_unrec(format!("Couldn't install {}", pkg)); @@ -117,6 +118,7 @@ pub fn clone(noconfirm: bool, pkg: &str) { match install_result.code() { Some(0) => { uninstall_make_depend(pkg); + addPkg(false, pkg); } Some(_) => { err_unrec(format!("Couldn't install {}", pkg)); diff --git a/src/mods/database.rs b/src/mods/database.rs new file mode 100644 index 0000000..8ff4927 --- /dev/null +++ b/src/mods/database.rs @@ -0,0 +1,46 @@ +use regex::bytes; +use toml_edit::{Document, value}; +use std::io::{Read, Write, Error}; +use std::fs::File; + +pub fn remPkg(pkgs: &Vec) { + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let mut database = std::fs::read_to_string(&file).expect("cant open database"); + + let mut updateDatabase = database; + for i in pkgs { + if updateDatabase.contains(i) { + let results = raur::search(&i); + for res in &results { + let databaseEntry = format!("{} = {{ name = \"{}\", version = \"{}\"}}\n",&res[0].name, &res[0].name, &res[0].version); + updateDatabase = format!("{}",updateDatabase.replace(&databaseEntry, "")); + } + } + } + let fileAsPath = File::create(std::path::Path::new(&file)).unwrap(); + write!(&fileAsPath, "{}", updateDatabase); + +} + +pub fn addPkg(fromRepo: bool, pkg: &str) -> Result<(), Error> { + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let database = std::fs::read_to_string(&file).expect("cant open database"); + let mut fileAsPath = File::create(std::path::Path::new(&file))?; + + let mut dbParsed = database.parse::().expect("invalid Database"); + if fromRepo == false { + let results = raur::search(&pkg); + for res in &results { + for r in res { + dbParsed[&r.name]["name"] = value(&r.name); + dbParsed[&r.name]["version"] = value(&r.version); + } + } + } else { + dbParsed[&pkg]["name"] = value(pkg); + dbParsed[&pkg]["version"] = value(pkg); + } + print!("{}",dbParsed); + fileAsPath.write_all(format!("{}",dbParsed).as_bytes()).unwrap(); + Ok(()) +} \ No newline at end of file diff --git a/src/mods/uninstall.rs b/src/mods/uninstall.rs index 809dc15..b672b55 100644 --- a/src/mods/uninstall.rs +++ b/src/mods/uninstall.rs @@ -1,4 +1,4 @@ -use crate::mods::strs::{err_rec, err_unrec, sec, succ}; +use crate::mods::{strs::{err_rec, err_unrec, sec, succ}, database::remPkg}; use runas::Command; use std::{fs, path::Path}; @@ -15,10 +15,12 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { .status() .expect("Couldn't call pacman"); match result.code() { - Some(0) => succ(format!( + Some(0) => { + succ(format!( "Succesfully uninstalled packages: {}", - &pkgs.join(" ") - )), + &pkgs.join(" "))); + remPkg(&pkgs); + }, Some(_) => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), None => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), }; @@ -29,10 +31,12 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { .status() .expect("Couldn't call pacman"); match result.code() { - Some(0) => succ(format!( + Some(0) => { + succ(format!( "Succesfully uninstalled packages: {}", - &pkgs.join(" ") - )), + &pkgs.join(" "))); + remPkg(&pkgs); + }, Some(_) => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), None => err_rec(format!("Couldn't uninstall packages: {}", &pkgs.join(" "))), }; diff --git a/src/mods/upgrade.rs b/src/mods/upgrade.rs index 168dcbd..bddc9c7 100644 --- a/src/mods/upgrade.rs +++ b/src/mods/upgrade.rs @@ -1,11 +1,16 @@ use crate::mods::strs::{err_unrec, inf, sec, succ}; use runas::Command; use std::{env, fs}; +use toml; pub fn upgrade(noconfirm: bool) { let homepath = std::env::var("HOME").unwrap(); let cachedir = format!("/{}/.cache/ame/", homepath); let cache_exists = std::path::Path::new(&format!("/{}/.cache/ame/", homepath)).is_dir(); + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let database = std::fs::read_to_string(&file).expect("cant open database"); + let mut dbParsed = database.parse::().expect("invalid Database"); + if cache_exists == false { let cachecreate = fs::create_dir_all(&cachedir); match cachecreate { @@ -40,7 +45,34 @@ pub fn upgrade(noconfirm: bool) { }; } - for file in std::fs::read_dir(&cachedir).unwrap() { + println!("{:?}", dbParsed); + for entry in dbParsed.as_table() { + for (key, value) in &*entry { + //println!("{} / {}", key, value); + for (option, entry) in { + println!("{} / {}", option, entry); + } + //if key.contains("name") { + /* println!("{}",value); + let results = raur::search(format!("{}",entry["paru"])); + println!("{}",format!("{}",entry)); + let mut test = value.to_string().replace("\"", ""); + test = test.replace("version", "").replace("name",""); + test = test.replace("=", ""); + println!("{}",test.replace(" ",""));*/ + //let results = raur::search(format!("{}",value)); + //for res in results { + // println!("{}",&res[0].name); + //} + /*} else if key.contains("version") { + if value.as_integer() == lVersion { + println!("upgrading"); + } + }*/ + } + } + + /*for file in std::fs::read_dir(&cachedir).unwrap() { let dir = &file.unwrap().path(); let output = std::process::Command::new("git") .arg("pull") @@ -82,5 +114,5 @@ pub fn upgrade(noconfirm: bool) { None => err_unrec(format!("Couldn't install new AUR package version")), }; } - } + }*/ } From ea5f18af9efc026cc6346a2668195e031bdf2435 Mon Sep 17 00:00:00 2001 From: axtlos <3alouchi2006@gmail.com> Date: Tue, 12 Oct 2021 21:05:16 +0200 Subject: [PATCH 2/3] database now works with upgrading too(im sorry for the horrible code jnats) --- src/mods/upgrade.rs | 217 +++++++++++++++++++++++++++++++------------- 1 file changed, 154 insertions(+), 63 deletions(-) diff --git a/src/mods/upgrade.rs b/src/mods/upgrade.rs index bddc9c7..704cdd0 100644 --- a/src/mods/upgrade.rs +++ b/src/mods/upgrade.rs @@ -1,7 +1,30 @@ -use crate::mods::strs::{err_unrec, inf, sec, succ}; +//use crate::mods::strs::{err_unrec, inf, sec, succ}; +use crate::{ + err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, + mods::uninstall::uninstall, mods::database::addPkg +}; use runas::Command; -use std::{env, fs}; +use std::{env, fs, path::Path}; use toml; +use git2::Repository; + + +fn uninstall_make_depend(pkg: &str) { + let make_depends = raur::info(&[&pkg]).unwrap()[0].make_depends.clone(); + + if make_depends.len() != 0 { + inf(format!( + "{} installed following make dependencies: {}", + pkg, + make_depends.join(", ") + )); + let remove = prompt(format!("Would you like to remove them?")); + if remove == true { + uninstall(true, make_depends); + } + } + succ(format!("Succesfully upgraded {}", pkg)); +} pub fn upgrade(noconfirm: bool) { let homepath = std::env::var("HOME").unwrap(); @@ -48,71 +71,139 @@ pub fn upgrade(noconfirm: bool) { println!("{:?}", dbParsed); for entry in dbParsed.as_table() { for (key, value) in &*entry { - //println!("{} / {}", key, value); - for (option, entry) in { - println!("{} / {}", option, entry); - } - //if key.contains("name") { - /* println!("{}",value); - let results = raur::search(format!("{}",entry["paru"])); - println!("{}",format!("{}",entry)); - let mut test = value.to_string().replace("\"", ""); - test = test.replace("version", "").replace("name",""); - test = test.replace("=", ""); - println!("{}",test.replace(" ",""));*/ - //let results = raur::search(format!("{}",value)); - //for res in results { - // println!("{}",&res[0].name); - //} - /*} else if key.contains("version") { - if value.as_integer() == lVersion { - println!("upgrading"); - } - }*/ - } - } + 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); + if res[0].version.contains(&version) { + let keydir = format!("{}{}",&cachedir,&key); + if std::path::Path::new(&keydir).is_dir() { + let cd_result = env::set_current_dir(&keydir); + match cd_result { + Ok(_) => inf(format!("Entered package directory")), + Err(_) => err_unrec(format!("Could not enter package directory")), + } + inssort(true, package[0].depends.clone()); - /*for file in std::fs::read_dir(&cachedir).unwrap() { - let dir = &file.unwrap().path(); - let output = std::process::Command::new("git") - .arg("pull") - .output() - .unwrap(); - let update_available = String::from_utf8(output.stdout).unwrap(); + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .arg("--needed") + .status(); + match install_result { + Ok(_) => { + uninstall_make_depend(&key); + } + Err(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + }; - let cd_result = env::set_current_dir(&dir); - match cd_result { - Ok(_) => inf(format!("Entered AUR package directory to pull changes")), - Err(_) => err_unrec(format!( - "Could not enter AUR package directory to pull changes" - )), - } + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--needed") + .status() + .expect("Couldn't call makepkg"); + match install_result.code() { + Some(0) => { + uninstall_make_depend(&key); + } + Some(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + None => { + err_unrec(format!("Couldn't install {}", &key)); + } + }; + + } else { + inf(format!("Cloning {} ...", &key)); - if update_available != "Already up to date." { - let path_as_str = &dir.display().to_string(); - let pkg: Vec<&str> = path_as_str.split("/").collect(); + if Path::new(&keydir).is_dir() { + let rm_result = fs::remove_dir_all(&keydir); + match rm_result { + Ok(_) => inf(format!( + "Package path for {} already found. Removing to reinstall", + &key + )), + Err(_) => err_unrec(format!( + "Package path for {} already found, but could not remove to reinstall", + &key + )), + } + } + + let dir_result = fs::create_dir(&keydir); + match dir_result { + Ok(_) => inf(format!("Created package directory for {}", &key)), + Err(_) => err_unrec(format!("Couldn't create package directory for {}", &key)), + } + + let cd_result = env::set_current_dir(&keydir); + match cd_result { + Ok(_) => inf(format!("Entered package directory")), + Err(_) => err_unrec(format!("Could not enter package directory")), + } + + inssort(true, package[0].depends.clone()); - inf(format!("{} is up to date", pkg[pkg.len() - 1])); - } else { - let cd2_result = env::set_current_dir(&dir); - match cd2_result { - Ok(_) => inf(format!( - "Entering AUR package directory to install new version" - )), - Err(_) => err_unrec(format!( - "Couldn't enter AUR package directory to install new version" - )), - } + let clone = Repository::clone(&url, Path::new(&keydir)); + match clone { + Ok(_) => { + inf(format!("Cloning {} into package directory", &key)); + } + Err(_) => err_unrec(format!("Failed cloning {} into package directory", &key)), + } + } - let makepkg_result = std::process::Command::new("makepkg") - .arg("-si") - .status() - .expect("Couldn't call makepkg"); - match makepkg_result.code() { - Some(0) => succ(format!("New AUR package version installed")), - Some(_) => err_unrec(format!("Couldn't install new AUR package version")), - None => err_unrec(format!("Couldn't install new AUR package version")), - }; + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .arg("--needed") + .status(); + match install_result { + Ok(_) => { + uninstall_make_depend(&key); + } + Err(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + }; + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--needed") + .status() + .expect("Couldn't call makepkg"); + match install_result.code() { + Some(0) => { + uninstall_make_depend(&key); + } + Some(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + None => { + err_unrec(format!("Couldn't install {}", &key)); + } + }; + + } else { + println!("not upgrading!"); + if std::path::Path::new(&format!("{}{}",&cachedir,&key)).is_dir() { + println!("not cloning"); + } else { + println!("cloning"); + } + } + } + } - }*/ + } } From a12b2f692ab778dc06b3c7b95db28d27d2380b36 Mon Sep 17 00:00:00 2001 From: jnats Date: Tue, 12 Oct 2021 21:00:49 +0100 Subject: [PATCH 3/3] rustfmt + code cleanup --- Cargo.toml | 4 +- main.rs | 3 +- src/main.rs | 1 - src/mods.rs | 2 +- src/mods/clone.rs | 16 ++- src/mods/database.rs | 100 +++++++++--------- src/mods/uninstall.rs | 23 +++-- src/mods/upgrade.rs | 228 ++++++++++++++++++++++-------------------- src/mods/ver.rs | 4 +- 9 files changed, 204 insertions(+), 177 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c460000..d9a413e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ame" -version = "2.3.2" +version = "2.4.0" authors = [ "jnats ", "axtlos " ] edition = "2018" description = "a fast and efficient aur helper." @@ -17,4 +17,4 @@ moins = "*" regex = "*" toml_edit = "*" toml = "*" -bytes = "*" \ No newline at end of file +bytes = "*" diff --git a/main.rs b/main.rs index 84b65d3..99d68c7 100644 --- a/main.rs +++ b/main.rs @@ -34,8 +34,7 @@ fn main() { // update } else if oper == "-Sy" || oper == "upd" { update(); - } else if oper == "-db" { - addPkg(pkgs); + // general search } else if oper == "-Ss" || oper == "sea" { r_search(&args[2]); diff --git a/src/main.rs b/src/main.rs index 9d71995..fdcf379 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ use mods::{ upgrade::upgrade, ver::ver, xargs::*, - database::{addPkg, remPkg} }; use std::{env, process::exit, process::Command}; diff --git a/src/mods.rs b/src/mods.rs index adff73a..55a48a6 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -1,5 +1,6 @@ pub mod clearcache; pub mod clone; +pub mod database; pub mod help; pub mod inssort; pub mod install; @@ -10,4 +11,3 @@ pub mod update; pub mod upgrade; pub mod ver; pub mod xargs; -pub mod database; \ No newline at end of file diff --git a/src/mods/clone.rs b/src/mods/clone.rs index 0d64410..ab660c9 100644 --- a/src/mods/clone.rs +++ b/src/mods/clone.rs @@ -1,6 +1,6 @@ use crate::{ - err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, - mods::uninstall::uninstall, mods::database::addPkg + err_unrec, inf, inssort, mods::database::add_pkg, mods::strs::prompt, mods::strs::sec, + mods::strs::succ, mods::uninstall::uninstall, }; use git2::Repository; use moins::Moins; @@ -102,7 +102,11 @@ pub fn clone(noconfirm: bool, pkg: &str) { match install_result { Ok(_) => { uninstall_make_depend(pkg); - addPkg(false, 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)); @@ -118,7 +122,11 @@ pub fn clone(noconfirm: bool, pkg: &str) { match install_result.code() { Some(0) => { uninstall_make_depend(pkg); - addPkg(false, 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)); diff --git a/src/mods/database.rs b/src/mods/database.rs index 8ff4927..6e78032 100644 --- a/src/mods/database.rs +++ b/src/mods/database.rs @@ -1,46 +1,54 @@ -use regex::bytes; -use toml_edit::{Document, value}; -use std::io::{Read, Write, Error}; -use std::fs::File; - -pub fn remPkg(pkgs: &Vec) { - let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); - let mut database = std::fs::read_to_string(&file).expect("cant open database"); - - let mut updateDatabase = database; - for i in pkgs { - if updateDatabase.contains(i) { - let results = raur::search(&i); - for res in &results { - let databaseEntry = format!("{} = {{ name = \"{}\", version = \"{}\"}}\n",&res[0].name, &res[0].name, &res[0].version); - updateDatabase = format!("{}",updateDatabase.replace(&databaseEntry, "")); - } - } - } - let fileAsPath = File::create(std::path::Path::new(&file)).unwrap(); - write!(&fileAsPath, "{}", updateDatabase); - -} - -pub fn addPkg(fromRepo: bool, pkg: &str) -> Result<(), Error> { - let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); - let database = std::fs::read_to_string(&file).expect("cant open database"); - let mut fileAsPath = File::create(std::path::Path::new(&file))?; - - let mut dbParsed = database.parse::().expect("invalid Database"); - if fromRepo == false { - let results = raur::search(&pkg); - for res in &results { - for r in res { - dbParsed[&r.name]["name"] = value(&r.name); - dbParsed[&r.name]["version"] = value(&r.version); - } - } - } else { - dbParsed[&pkg]["name"] = value(pkg); - dbParsed[&pkg]["version"] = value(pkg); - } - print!("{}",dbParsed); - fileAsPath.write_all(format!("{}",dbParsed).as_bytes()).unwrap(); - Ok(()) -} \ No newline at end of file +use crate::{err_unrec, inf}; +use std::fs::File; +use std::io::{Error, Write}; +use toml_edit::{value, Document}; + +pub fn rem_pkg(pkgs: &Vec) { + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let database = std::fs::read_to_string(&file).expect("Can't Open Database"); + + let mut update_database = database; + for i in pkgs { + if update_database.contains(i) { + let results = raur::search(&i); + for res in &results { + let database_entry = format!( + "{} = {{ name = \"{}\", version = \"{}\"}}\n", + &res[0].name, &res[0].name, &res[0].version + ); + update_database = format!("{}", update_database.replace(&database_entry, "")); + } + } + } + let file_as_path = File::create(std::path::Path::new(&file)).unwrap(); + let db_update_res = write!(&file_as_path, "{}", update_database); + match db_update_res { + Ok(_) => inf(format!("Database update successful")), + Err(_) => err_unrec(format!("Couldn't update database")), + } +} + +pub fn add_pkg(from_repo: bool, pkg: &str) -> Result<(), Error> { + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let database = std::fs::read_to_string(&file).expect("cant open database"); + let mut file_as_path = File::create(std::path::Path::new(&file))?; + + let mut db_parsed = database.parse::().expect("invalid Database"); + 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); + } + } + } else { + db_parsed[&pkg]["name"] = value(pkg); + db_parsed[&pkg]["version"] = value(pkg); + } + print!("{}", db_parsed); + file_as_path + .write_all(format!("{}", db_parsed).as_bytes()) + .unwrap(); + Ok(()) +} diff --git a/src/mods/uninstall.rs b/src/mods/uninstall.rs index b672b55..72738b6 100644 --- a/src/mods/uninstall.rs +++ b/src/mods/uninstall.rs @@ -1,4 +1,7 @@ -use crate::mods::{strs::{err_rec, err_unrec, sec, succ}, database::remPkg}; +use crate::mods::{ + database::rem_pkg, + strs::{err_rec, err_unrec, sec, succ}, +}; use runas::Command; use std::{fs, path::Path}; @@ -17,10 +20,11 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { match result.code() { Some(0) => { succ(format!( - "Succesfully uninstalled packages: {}", - &pkgs.join(" "))); - remPkg(&pkgs); - }, + "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(" "))), }; @@ -33,10 +37,11 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec) { match result.code() { Some(0) => { succ(format!( - "Succesfully uninstalled packages: {}", - &pkgs.join(" "))); - remPkg(&pkgs); - }, + "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(" "))), }; diff --git a/src/mods/upgrade.rs b/src/mods/upgrade.rs index 704cdd0..9806626 100644 --- a/src/mods/upgrade.rs +++ b/src/mods/upgrade.rs @@ -1,13 +1,11 @@ //use crate::mods::strs::{err_unrec, inf, sec, succ}; use crate::{ - err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, - mods::uninstall::uninstall, mods::database::addPkg + err_unrec, inf, inssort, mods::strs::prompt, mods::strs::sec, mods::strs::succ, uninstall, }; +use git2::Repository; use runas::Command; use std::{env, fs, path::Path}; use toml; -use git2::Repository; - fn uninstall_make_depend(pkg: &str) { let make_depends = raur::info(&[&pkg]).unwrap()[0].make_depends.clone(); @@ -30,19 +28,16 @@ pub fn upgrade(noconfirm: bool) { let homepath = std::env::var("HOME").unwrap(); let cachedir = format!("/{}/.cache/ame/", homepath); let cache_exists = std::path::Path::new(&format!("/{}/.cache/ame/", homepath)).is_dir(); - let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); - let database = std::fs::read_to_string(&file).expect("cant open database"); - let mut dbParsed = database.parse::().expect("invalid Database"); + let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); + let database = std::fs::read_to_string(&file).expect("Can't open database"); + let db_parsed = database.parse::().expect("Invalid Database"); if cache_exists == false { let cachecreate = fs::create_dir_all(&cachedir); match cachecreate { - Ok(_) => { - inf(format!("Creating cachedir. (didn't exist previously)")) + Ok(_) => inf(format!("Creating cachedir. (didn't exist previously)")), + Err(_) => err_unrec(format!("Couldn't create cachedir")), } - Err(_) => { - err_unrec(format!("Couldn't create cachedir")) - }} } sec(format!("Performing system upgrade")); if noconfirm == true { @@ -68,99 +63,38 @@ pub fn upgrade(noconfirm: bool) { }; } - println!("{:?}", dbParsed); - for entry in dbParsed.as_table() { + println!("{:?}", db_parsed); + for entry in db_parsed.as_table() { 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); - if res[0].version.contains(&version) { - let keydir = format!("{}{}",&cachedir,&key); - if std::path::Path::new(&keydir).is_dir() { - let cd_result = env::set_current_dir(&keydir); - match cd_result { - Ok(_) => inf(format!("Entered package directory")), - Err(_) => err_unrec(format!("Could not enter package directory")), - } - inssort(true, package[0].depends.clone()); - - sec(format!("Installing {} ...", &key)); - let install_result = std::process::Command::new("makepkg") - .arg("-si") - .arg("--noconfirm") - .arg("--needed") - .status(); - match install_result { - Ok(_) => { - uninstall_make_depend(&key); - } - Err(_) => { - err_unrec(format!("Couldn't install {}", &key)); - } - }; - - sec(format!("Installing {} ...", &key)); - let install_result = std::process::Command::new("makepkg") - .arg("-si") - .arg("--needed") - .status() - .expect("Couldn't call makepkg"); - match install_result.code() { - Some(0) => { - uninstall_make_depend(&key); - } - Some(_) => { - err_unrec(format!("Couldn't install {}", &key)); - } - None => { - err_unrec(format!("Couldn't install {}", &key)); - } - }; - - } else { - inf(format!("Cloning {} ...", &key)); - - if Path::new(&keydir).is_dir() { - let rm_result = fs::remove_dir_all(&keydir); - match rm_result { - Ok(_) => inf(format!( - "Package path for {} already found. Removing to reinstall", - &key - )), - Err(_) => err_unrec(format!( - "Package path for {} already found, but could not remove to reinstall", - &key - )), - } - } - - let dir_result = fs::create_dir(&keydir); - match dir_result { - Ok(_) => inf(format!("Created package directory for {}", &key)), - Err(_) => err_unrec(format!("Couldn't create package directory for {}", &key)), - } - - let cd_result = env::set_current_dir(&keydir); - match cd_result { - Ok(_) => inf(format!("Entered package directory")), - Err(_) => err_unrec(format!("Could not enter package directory")), - } - - inssort(true, package[0].depends.clone()); - - let clone = Repository::clone(&url, Path::new(&keydir)); - match clone { - Ok(_) => { - inf(format!("Cloning {} into package directory", &key)); - } - Err(_) => err_unrec(format!("Failed cloning {} into package directory", &key)), - } + 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); + if res[0].version.contains(&version) { + let keydir = format!("{}{}", &cachedir, &key); + if std::path::Path::new(&keydir).is_dir() { + let cd_result = env::set_current_dir(&keydir); + match cd_result { + Ok(_) => inf(format!("Entered package directory")), + Err(_) => err_unrec(format!("Could not enter package directory")), } + inssort(true, package[0].depends.clone()); sec(format!("Installing {} ...", &key)); let install_result = std::process::Command::new("makepkg") @@ -176,6 +110,7 @@ pub fn upgrade(noconfirm: bool) { err_unrec(format!("Couldn't install {}", &key)); } }; + sec(format!("Installing {} ...", &key)); let install_result = std::process::Command::new("makepkg") .arg("-si") @@ -193,17 +128,90 @@ pub fn upgrade(noconfirm: bool) { err_unrec(format!("Couldn't install {}", &key)); } }; + } else { + inf(format!("Cloning {} ...", &key)); + + if Path::new(&keydir).is_dir() { + let rm_result = fs::remove_dir_all(&keydir); + match rm_result { + Ok(_) => inf(format!( + "Package path for {} already found. Removing to reinstall", + &key + )), + Err(_) => err_unrec(format!( + "Package path for {} already found, but could not remove to reinstall", + &key + )), + } + } - } else { - println!("not upgrading!"); - if std::path::Path::new(&format!("{}{}",&cachedir,&key)).is_dir() { - println!("not cloning"); - } else { - println!("cloning"); + let dir_result = fs::create_dir(&keydir); + match dir_result { + Ok(_) => inf(format!("Created package directory for {}", &key)), + Err(_) => { + err_unrec(format!("Couldn't create package directory for {}", &key)) + } + } + + let cd_result = env::set_current_dir(&keydir); + match cd_result { + Ok(_) => inf(format!("Entered package directory")), + Err(_) => err_unrec(format!("Could not enter package directory")), + } + + inssort(true, package[0].depends.clone()); + + let clone = Repository::clone(&url, Path::new(&keydir)); + match clone { + Ok(_) => { + inf(format!("Cloning {} into package directory", &key)); + } + Err(_) => { + err_unrec(format!("Failed cloning {} into package directory", &key)) + } + } + } + + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--noconfirm") + .arg("--needed") + .status(); + match install_result { + Ok(_) => { + uninstall_make_depend(&key); + } + Err(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + }; + sec(format!("Installing {} ...", &key)); + let install_result = std::process::Command::new("makepkg") + .arg("-si") + .arg("--needed") + .status() + .expect("Couldn't call makepkg"); + match install_result.code() { + Some(0) => { + uninstall_make_depend(&key); + } + Some(_) => { + err_unrec(format!("Couldn't install {}", &key)); + } + None => { + err_unrec(format!("Couldn't install {}", &key)); } + }; + } else { + println!("not upgrading!"); + if std::path::Path::new(&format!("{}{}", &cachedir, &key)).is_dir() { + println!("not cloning"); + } else { + println!("cloning"); } } - + } } } } diff --git a/src/mods/ver.rs b/src/mods/ver.rs index 4a80061..a78d44f 100644 --- a/src/mods/ver.rs +++ b/src/mods/ver.rs @@ -1,9 +1,9 @@ -use crate::mods::strs::inf; +use crate::inf; use ansi_term::Colour; pub fn ver() { println!(""); - inf(format!("ame - v2.3.2")); + inf(format!("ame - v2.4.0")); println!(""); inf(format!("Contributors:")); println!("- axtlos ");