Initial impl of passing unrecognised commands to pacman

i18n
Michal 2 years ago committed by GitHub
parent 385329047e
commit f485c256e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<Operation>,
@ -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,
}

@ -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::<Vec<&str>>();
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::<Vec<String>>()[1] == "-Qa" || env::args().collect::<Vec<String>>()[1] == "-Qm";
let repo = args.repo || env::args().collect::<Vec<String>>()[1] == "-Qr" || env::args().collect::<Vec<String>>()[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")

Loading…
Cancel
Save