From f485c256e65395651c8362b2937cc9f3428bb57a Mon Sep 17 00:00:00 2001 From: Michal Date: Fri, 26 Aug 2022 06:32:02 +0000 Subject: [PATCH] Initial impl of passing unrecognised commands to pacman --- src/args.rs | 10 +++++----- src/main.rs | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/args.rs b/src/args.rs index 167b130..bb75d8f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand, ValueHint}; #[derive(Debug, Clone, Parser)] -#[clap(bin_name = "ame", name = "Amethyst", version = env ! ("CARGO_PKG_VERSION"), about = env ! ("CARGO_PKG_DESCRIPTION"), infer_subcommands = true)] +#[clap(bin_name = "ame", name = "Amethyst", version = env ! ("CARGO_PKG_VERSION"), about = env ! ("CARGO_PKG_DESCRIPTION"), infer_subcommands = true, allow_external_subcommands = true, allow_hyphen_values = true)] pub struct Args { #[clap(subcommand)] pub subcommand: Option, @@ -109,12 +109,12 @@ pub struct SearchArgs { #[derive(Default, Debug, Clone, Parser)] pub struct QueryArgs { - /// Lists AUR/foreign packages - #[clap(long, short, from_global)] + /// Lists AUR/foreign packages [-Qa, -Qm] + #[clap(long, short)] pub aur: bool, - /// Lists repo/native packages - #[clap(long, short, from_global)] + /// Lists repo/native packages [-Qr, -Qn] + #[clap(long, short)] pub repo: bool, } diff --git a/src/main.rs b/src/main.rs index 946afee..d49fe04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ #![allow(clippy::too_many_lines)] use args::Args; -use clap::Parser; +use clap::{Parser, CommandFactory}; use clap_complete::{Generator, Shell}; use internal::commands::ShellCommand; use internal::error::SilentUnwrap; @@ -31,6 +31,7 @@ fn main() { crash!( AppExitCode::RunAsRoot, "Running amethyst as root is disallowed as it can lead to system breakage. Instead, amethyst will prompt you when it needs superuser permissions"); } + // Parse arguments let args: Args = Args::parse(); @@ -74,6 +75,25 @@ fn main() { .to_string() }; + // List of possible options + let opers = vec!["install", "remove", "upgrade", "search", "query", "info", "clean", "diff", "gencomp"]; + + // If arg is completely unrecognized, attempt to pass it to pacman + match args::Args::command().get_matches().subcommand() { + Some((ext, ext_m)) => { + if !opers.contains(&ext) { + let mut m = ext_m.values_of("").unwrap_or_default().collect::>(); + m.insert(0, ext); + + info!("Passing unrecognized flags \"{}\" to pacman", m.join(" ")); + + let child = ShellCommand::pacman().args(m).elevated().wait().silent_unwrap(AppExitCode::PacmanError); + std::process::exit(child.code().unwrap_or(1)); + } + } + _ => {} + } + // Match args match args.subcommand.unwrap_or_default() { Operation::Install(install_args) => cmd_install(install_args, options, &cachedir), @@ -95,6 +115,8 @@ fn main() { cmd_gencomp(&gencomp_args); } } + + } fn cmd_install(args: InstallArgs, options: Options, cachedir: &str) { @@ -217,21 +239,25 @@ fn cmd_search(args: &SearchArgs, options: Options) { } fn cmd_query(args: &QueryArgs) { - if args.aur { + let aur = args.aur || env::args().collect::>()[1] == "-Qa" || env::args().collect::>()[1] == "-Qm"; + let repo = args.repo || env::args().collect::>()[1] == "-Qr" || env::args().collect::>()[1] == "-Qn"; + let both = !aur && !repo; + + if aur { // If AUR query, query AUR ShellCommand::pacman() .arg("-Qm") .wait_success() .silent_unwrap(AppExitCode::PacmanError); } - if args.repo { + if repo { // If repo query, query repos ShellCommand::pacman() .arg("-Qn") .wait_success() .silent_unwrap(AppExitCode::PacmanError); } - if !args.repo && !args.aur { + if both { // If no query type specified, query both ShellCommand::pacman() .arg("-Qn")