From 5b8aa7ff765a210f480503f8a51b50f2899d3bf4 Mon Sep 17 00:00:00 2001 From: michal Date: Mon, 17 Jan 2022 13:26:31 +0000 Subject: [PATCH] initial sorting algorithm --- src/internal/mod.rs | 9 +++++ src/{ => internal}/rpc.rs | 29 +++++++++++----- src/internal/sort.rs | 22 ++++++++++++ src/internal/structs.rs | 20 +++++++++++ src/main.rs | 67 ++++++++++++++++++++++++++++++------- src/operations/install.rs | 3 ++ src/operations/mod.rs | 7 ++++ src/operations/query.rs | 0 src/operations/uninstall.rs | 0 9 files changed, 136 insertions(+), 21 deletions(-) create mode 100644 src/internal/mod.rs rename src/{ => internal}/rpc.rs (59%) create mode 100644 src/internal/sort.rs create mode 100644 src/internal/structs.rs create mode 100644 src/operations/install.rs create mode 100644 src/operations/mod.rs create mode 100644 src/operations/query.rs create mode 100644 src/operations/uninstall.rs diff --git a/src/internal/mod.rs b/src/internal/mod.rs new file mode 100644 index 0000000..984ebd0 --- /dev/null +++ b/src/internal/mod.rs @@ -0,0 +1,9 @@ +pub mod rpc; +pub mod structs; +mod sort; + +pub fn sort(a: &[String]) -> structs::Sorted { + sort::sort(a) +} + + diff --git a/src/rpc.rs b/src/internal/rpc.rs similarity index 59% rename from src/rpc.rs rename to src/internal/rpc.rs index ab9ab35..9972e76 100644 --- a/src/rpc.rs +++ b/src/internal/rpc.rs @@ -22,20 +22,33 @@ pub struct SearchResults { pub results: Vec, } -pub fn rpcinfo(pkg: &str) -> Package { +pub struct InfoResults { + pub found: bool, + pub package: Option +} + +pub fn rpcinfo(pkg: String) -> InfoResults { let res = reqwest::blocking::get(&format!( "https://aur.archlinux.org/rpc/?v=5&type=info&arg={}", pkg - )).unwrap(); + )).unwrap().json::().unwrap(); - res.json::().unwrap().results[0].clone() + if res.results.is_empty() { + InfoResults { + found: false, + package: None + } + } else { + InfoResults { + found: true, + package: Some(res.results[0].clone()) + } + } } -pub fn rpcsearch(pkg: &str) -> SearchResults { - let res = reqwest::blocking::get(&format!( +pub fn rpcsearch(pkg: String) -> SearchResults { + reqwest::blocking::get(&format!( "https://aur.archlinux.org/rpc/?v=5&type=search&arg={}", pkg - )).unwrap(); - - res.json().unwrap() + )).unwrap().json().unwrap() } diff --git a/src/internal/sort.rs b/src/internal/sort.rs new file mode 100644 index 0000000..3000f7f --- /dev/null +++ b/src/internal/sort.rs @@ -0,0 +1,22 @@ +use crate::internal::{structs, rpc}; + +pub fn sort(a: &[String]) -> structs::Sorted { + #[allow(unused_mut)] + let mut repo: Vec = vec![]; + let mut aur: Vec = vec![]; + let mut nf: Vec = vec![]; + + for b in a { + if rpc::rpcinfo(b.to_string()).found { + aur.push(b.to_string()); + } else { + nf.push(b.to_string()); + } + } + + structs::Sorted::new( + repo, + aur, + nf + ) +} \ No newline at end of file diff --git a/src/internal/structs.rs b/src/internal/structs.rs new file mode 100644 index 0000000..5b2fccd --- /dev/null +++ b/src/internal/structs.rs @@ -0,0 +1,20 @@ +#[derive(Debug)] +pub struct Sorted { + #[allow(dead_code)] + repo: Vec, + #[allow(dead_code)] + aur: Vec, + #[allow(dead_code)] + nf: Vec +} + +impl Sorted { + pub fn new(repo: Vec, aur: Vec, nf: Vec) -> Self { + let a: Sorted = Sorted { + repo, + aur, + nf + }; + a + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index cf733f5..6c3dfd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ -mod rpc; +mod operations; +mod internal; + use clap::{App, Arg, SubCommand}; +use crate::internal::sort; fn main() { let matches = App::new("Amethyst") @@ -12,27 +15,65 @@ fn main() { .help("Sets the level of verbosity"), ) .subcommand( - SubCommand::with_name ("install") + SubCommand::with_name("install") .about("Installs a package from either the AUR or the PacMan-defined repositories") + .aliases(&["-S", "ins"]) + .arg( + Arg::with_name("noconfirm") + .short("y") + .long("noconfirm") + .help("Do not ask for confirmation before installing packages"), + ) + .arg( + Arg::with_name("package(s)") + .help("The name of the package(s) to install") + .required(true) + .multiple(true) + .index(1), + ), + ) + .subcommand( + SubCommand::with_name("remove") + .about("Removes a previously installed package") + .aliases(&["-R", "rm"]) .arg( Arg::with_name("noconfirm") .short("y") .long("noconfirm") - .help("Do not ask for confirmation before installing the package") + .help("Do not ask for confirmation before removing packages"), ) .arg( - Arg::with_name("package") - .help("The name of the package to install") + Arg::with_name("recursive") + .short("s") + .long("recursive") + .help("Recursively uninstall orphaned dependencies"), + ) + .arg( + Arg::with_name("package(s)") + .help("The name of the package(s) to remove") .required(true) - .index(1) - ) + .multiple(true) + .index(1), + ), ) .get_matches(); - match matches.occurrences_of("verbose") { - 0 => println!("No verbosity"), - 1 => println!("Some extra information"), - 2 => println!("Plenty of debug text"), - _ => println!("Screensaver mode"), - } + let verbosity = matches.occurrences_of("verbose"); + + let packages: Vec = matches + .subcommand_matches("install") + .unwrap() + .values_of("package(s)") + .unwrap() + .into_iter().map(|s| s.to_string()).collect(); + + if let true = matches.is_present("install") { + println!( + "Installing: {}\nVerbosity: {}\n{:?}", + packages.join(", "), + verbosity, + sort(&packages) + ); + operations::install(packages); + } } diff --git a/src/operations/install.rs b/src/operations/install.rs new file mode 100644 index 0000000..dcc1ce4 --- /dev/null +++ b/src/operations/install.rs @@ -0,0 +1,3 @@ +pub fn install(a: Vec) { + println!("{:?}", &a); +} \ No newline at end of file diff --git a/src/operations/mod.rs b/src/operations/mod.rs new file mode 100644 index 0000000..7d75b7b --- /dev/null +++ b/src/operations/mod.rs @@ -0,0 +1,7 @@ +mod install; +mod uninstall; +mod query; + +pub fn install(a: Vec) { + install::install(a); +} \ No newline at end of file diff --git a/src/operations/query.rs b/src/operations/query.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/operations/uninstall.rs b/src/operations/uninstall.rs new file mode 100644 index 0000000..e69de29