removing packages with dependencies now works, as well as some code cleanup

i18n
axtlos 3 years ago
parent 189e897831
commit 9638607624

@ -1,5 +1,5 @@
mod mods; 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}; use std::{env, process::exit, process::Command};
fn main() { fn main() {
@ -18,11 +18,14 @@ fn main() {
match oper.as_str() { match oper.as_str() {
"-S" | "-Sn" | "ins" => { "-S" | "-Sn" | "ins" => {
inssort(noconfirm, pkgs); inssort(noconfirm, false, pkgs);
} }
"-R" | "-Rn" | "-Rsn" | "-Rs" | "rm" => { "-R" | "-Rn" | "rm" => {
uninstall(noconfirm, pkgs); uninstall(noconfirm, pkgs);
} }
"-Rs" | "-Rsn" | "purge" => {
purge(noconfirm, pkgs)
}
"-Syu" | "-Syun" |"upg" => { "-Syu" | "-Syun" |"upg" => {
upgrade(noconfirm); upgrade(noconfirm);
} }

@ -11,3 +11,4 @@ pub mod update;
pub mod upgrade; pub mod upgrade;
pub mod ver; pub mod ver;
pub mod xargs; pub mod xargs;
pub mod purge;

@ -23,7 +23,7 @@ fn uninstall_make_depend(pkg: &str) {
succ(format!("Succesfully installed {}", pkg)); 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 cachedir = format!("{}/.cache/ame", std::env::var("HOME").unwrap());
let path = Path::new(&cachedir); let path = Path::new(&cachedir);
let pkgdir = format!("{}/{}", &cachedir, &pkg); let pkgdir = format!("{}/{}", &cachedir, &pkg);
@ -73,7 +73,7 @@ pub fn clone(noconfirm: bool, pkg: &str) {
sec(format!("Installing AUR package depends")); 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)); let clone = Repository::clone(&url, Path::new(&pkgdir));
match clone { match clone {
@ -82,58 +82,80 @@ pub fn clone(noconfirm: bool, pkg: &str) {
} }
Err(_) => err_unrec(format!("Failed cloning {} into package directory", pkg)), 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 { if pkgbuild == true {
let pkgbuild = prompt(format!("View PKGBUILD?")); 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 { if noconfirm == true {
sec(format!("Installing {} ...", pkg)); sec(format!("Installing {} ...", pkg));
let install_result = Command::new("makepkg") let install_result = Command::new("makepkg")
.arg("-si") .arg("-si")
.arg("--noconfirm") .arg("--noconfirm")
.arg("--needed") .arg("--needed")
.status(); .status();
match install_result { match install_result {
Ok(_) => { Ok(_) => {
uninstall_make_depend(pkg); uninstall_make_depend(pkg);
let add_pkg_res = add_pkg(false, pkg); let add_pkg_res = add_pkg(false, pkg);
match add_pkg_res { match add_pkg_res {
Ok(_) => inf(format!("Added package {} to database", pkg)), 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 add package {} to database", pkg)),
}
} }
} Err(_) => {
Err(_) => { err_unrec(format!("Couldn't install {}", pkg));
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 { } else {
sec(format!("Installing {} ...", pkg)); sec(format!("Installing {} ...", pkg));
let install_result = Command::new("makepkg") let install_result = Command::new("makepkg")
.arg("-si") .arg("-si")
.arg("--needed") .arg("--noconfirm")
.status() .arg("--needed")
.expect("Couldn't call makepkg"); .arg("--asdeps")
match install_result.code() { .status();
Some(0) => { match install_result {
uninstall_make_depend(pkg); Ok(_) => {
let add_pkg_res = add_pkg(false, pkg); uninstall_make_depend(pkg);
match add_pkg_res { let add_pkg_res = add_pkg(false, pkg);
Ok(_) => inf(format!("Added package {} to database", pkg)), match add_pkg_res {
Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)), Ok(_) => inf(format!("Added package {} to database", pkg)),
Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)),
}
} }
} Err(_) => {
Some(_) => { err_unrec(format!("Couldn't install {}", pkg));
err_unrec(format!("Couldn't install {}", pkg)); }
} };
None => {
err_unrec(format!("Couldn't install {}", pkg));
}
};
} }
} }

@ -4,6 +4,74 @@ use std::io::{Error, Write};
use toml_edit::{value, Document}; use toml_edit::{value, Document};
use crate::mods::strs::{err_rec}; 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::<toml::Value>().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<String>) { pub fn rem_pkg(pkgs: &Vec<String>) {
let homepath = std::env::var("HOME").unwrap(); let homepath = std::env::var("HOME").unwrap();
let file = format!("{}/.local/ame/aurPkgs.db", 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 { if from_repo == false {
let results = raur::search(&pkg); let results = raur::search(&pkg);
for res in &results { for res in &results {
for r in res { //for r in res {
db_parsed[&r.name]["name"] = value(&r.name); db_parsed[&res[0].name]["name"] = value(&res[0].name);
db_parsed[&r.name]["version"] = value(&r.version); db_parsed[&res[0].name]["version"] = value(&res[0].version);
} //}
} }
} else { } else {
db_parsed[&pkg]["name"] = value(pkg); db_parsed[&pkg]["name"] = value(pkg);

@ -1,7 +1,7 @@
use crate::{clone, err_unrec, install, mods::strs::sec}; use crate::{clone, err_unrec, install, mods::strs::sec};
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
pub fn inssort(noconfirm: bool, pkgs: Vec<String>) { pub fn inssort(noconfirm: bool, as_dep: bool, pkgs: Vec<String>) {
let mut repo = vec![]; let mut repo = vec![];
let mut aur = vec![]; let mut aur = vec![];
let re = regex::Regex::new(r"(\S+)((?:>=|<=)\S+$)").unwrap(); let re = regex::Regex::new(r"(\S+)((?:>=|<=)\S+$)").unwrap();
@ -42,13 +42,25 @@ pub fn inssort(noconfirm: bool, pkgs: Vec<String>) {
} }
} }
} }
if repo.len() != 0 { if as_dep == false {
sec(format!("Installing repo packages: {}", &repo.join(", "))); if repo.len() != 0 {
install(noconfirm, &repo.join(" ")); 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 { for a in aur {
sec(format!("Couldn't find {} in repos. Searching AUR", a)); sec(format!("Couldn't find {} in repos. Searching AUR", a));
clone(noconfirm, &a); clone(noconfirm, true, &a);
}
} }
} }

@ -1,25 +1,41 @@
use crate::mods::strs::{err_unrec, succ}; use crate::mods::strs::{err_unrec, succ};
use runas::Command; 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(); let pkgs: Vec<&str> = pkg.split(" ").collect();
if noconfirm == true { if as_dep == false {
let result = Command::new("pacman") if noconfirm == true {
.arg("-Sy") let result = Command::new("pacman")
.arg("--noconfirm") .arg("-S")
.arg("--needed") .arg("--noconfirm")
.args(&pkgs) .arg("--needed")
.status() .args(&pkgs)
.expect("Couldn't call pacman"); .status()
match result.code() { .expect("Couldn't call pacman");
Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), match result.code() {
Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), Some(0) => succ(format!("Succesfully installed packages: {}", pkg)),
None => err_unrec(format!("Couldn't install 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 { } else {
let result = Command::new("pacman") let result = Command::new("pacman")
.arg("-Sy") .arg("-S")
.arg("--noconfirm")
.arg("--needed") .arg("--needed")
.arg("--asdeps")
.args(&pkgs) .args(&pkgs)
.status() .status()
.expect("Couldn't call pacman"); .expect("Couldn't call pacman");

@ -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<String>) {
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)),
};
}
}
}

@ -12,7 +12,7 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec<String>) {
)); ));
if noconfirm == true { if noconfirm == true {
let result = Command::new("pacman") let result = Command::new("pacman")
.arg("-Rs") .arg("-R")
.args(&pkgs) .args(&pkgs)
.arg("--noconfirm") .arg("--noconfirm")
.status() .status()
@ -30,7 +30,7 @@ pub fn uninstall(noconfirm: bool, pkgs: Vec<String>) {
}; };
} else { } else {
let result = Command::new("pacman") let result = Command::new("pacman")
.arg("-Rs") .arg("-R")
.args(&pkgs) .args(&pkgs)
.status() .status()
.expect("Couldn't call pacman"); .expect("Couldn't call pacman");

@ -1,5 +1,5 @@
use crate::{ 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 git2::Repository;
use runas::Command; use runas::Command;
@ -78,29 +78,13 @@ pub fn upgrade(noconfirm: bool) {
}; };
} }
println!("{:?}", db_parsed);
for entry in db_parsed.as_table() { for entry in db_parsed.as_table() {
for (key, value) in &*entry { for (key, _value) in &*entry {
let results = raur::search(format!("{}", key)); let results = raur::search(format!("{}", key));
for res in results { for res in results {
println!("{}", &res[0].name);
let url = format!("https://aur.archlinux.org/{}.git", key); let url = format!("https://aur.archlinux.org/{}.git", key);
let package = raur::info(&[key]).unwrap(); let package = raur::info(&[key]).unwrap();
let version = value let version = get_value(&key, "version");
.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) { if res[0].version.contains(&version) {
let keydir = format!("{}{}", &cachedir, &key); let keydir = format!("{}{}", &cachedir, &key);
if std::path::Path::new(&keydir).is_dir() { if std::path::Path::new(&keydir).is_dir() {
@ -109,7 +93,7 @@ pub fn upgrade(noconfirm: bool) {
Ok(_) => inf(format!("Entered package directory")), Ok(_) => inf(format!("Entered package directory")),
Err(_) => err_unrec(format!("Could not enter 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)); sec(format!("Installing {} ...", &key));
let install_result = std::process::Command::new("makepkg") 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")), 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)); let clone = Repository::clone(&url, Path::new(&keydir));
match clone { match clone {
@ -219,12 +203,7 @@ pub fn upgrade(noconfirm: bool) {
} }
}; };
} else { } else {
println!("not upgrading!"); inf(format!("Package {} already up to date", &key));
if std::path::Path::new(&format!("{}{}", &cachedir, &key)).is_dir() {
println!("not cloning");
} else {
println!("cloning");
}
} }
} }
} }

Loading…
Cancel
Save