[WIP] Potentially controversial arg change to support proper flag subcommands

i18n
Michal S 2 years ago committed by Michal
parent a72e99b7a6
commit 3790640524

@ -4,7 +4,7 @@ use crate::operations::SearchBy;
use clap::{Parser, Subcommand, ValueHint}; use clap::{Parser, Subcommand, ValueHint};
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[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)] #[clap(bin_name = "ame", name = "Amethyst", version = env ! ("CARGO_PKG_VERSION"), about = env ! ("CARGO_PKG_DESCRIPTION"), infer_subcommands = true)]
pub struct Args { pub struct Args {
#[clap(subcommand)] #[clap(subcommand)]
pub subcommand: Option<Operation>, pub subcommand: Option<Operation>,
@ -28,40 +28,36 @@ pub struct Args {
#[derive(Debug, Clone, Subcommand)] #[derive(Debug, Clone, Subcommand)]
pub enum Operation { pub enum Operation {
/// Installs a package from either the AUR or the Pacman-defined repositories /// Installs or searches for a package in either the AUR or the Pacman-defined repositories
#[clap(bin_name = "ame", name = "install", visible_aliases = & ["-S", "i"], aliases = & ["-Sa", "-Sr"])] #[clap(bin_name = "ame", name = "sync", aliases = & [ "-S" ], visible_aliases = & ["install", "i"], short_flag = 'S')]
Install(InstallArgs), Install(InstallArgs),
/// Removes a previously installed package /// Removes a previously installed package
#[clap(bin_name = "ame", name = "remove", visible_aliases = & ["rm", "r", "-Rs"])] #[clap(bin_name = "ame", name = "remove", visible_aliases = & ["rm", "r"], short_flag = 'R')]
Remove(RemoveArgs), Remove(RemoveArgs),
/// Searches for packages matching a provided pattern in the AUR/repos /// Searches for packages matching a provided pattern in the AUR/repos [aliases: -Ss]
#[clap(bin_name = "ame", name = "search", visible_aliases = & ["-Ss", "s"], aliases = & ["-Ssa", "-Ssr"])] #[clap(bin_name = "ame", name = "search")]
Search(SearchArgs), Search(InstallArgs),
/// Queries installed packages /// Queries installed packages
#[clap(bin_name = "ame", name = "query", visible_aliases = & ["-Q", "q"], aliases = & ["-Qa", "-Qr", "-Qm", "-Qn"])] #[clap(bin_name = "ame", name = "query", visible_aliases = & ["q"], short_flag = 'Q')]
Query(QueryArgs), Query(QueryArgs),
/// Gets info about a package
#[clap(bin_name = "ame", name = "info", visible_aliases = & ["-Qi"])]
Info(InfoArgs),
/// Upgrades locally installed packages to their latest versions (Default) /// Upgrades locally installed packages to their latest versions (Default)
#[clap(bin_name = "ame", name = "upgrade", visible_aliases = & ["-Syu", "u"])] #[clap(bin_name = "ame", name = "upgrade", visible_aliases = & ["u", "-Syu"])]
Upgrade(UpgradeArgs), Upgrade(UpgradeArgs),
/// Generates shell completions for supported shells (bash, fish, elvish, pwsh) /// Generates shell completions for supported shells (bash, fish, elvish, pwsh)
#[clap(bin_name = "ame", name = "gencomp", visible_aliases = & ["-g"])] #[clap(bin_name = "ame", name = "gencomp", visible_aliases = & ["g"], short_flag = 'G')]
GenComp(GenCompArgs), GenComp(GenCompArgs),
/// Removes all orphaned packages /// Removes all orphaned packages
#[clap(bin_name = "ame", name = "clean", visible_aliases = & ["-Sc", "c"])] #[clap(bin_name = "ame", name = "clean", visible_aliases = & ["c"], short_flag = 'C')]
Clean, Clean,
/// Runs pacdiff /// Runs pacdiff
#[clap(bin_name = "ame", name = "diff", visible_aliases = & ["-d"])] #[clap(bin_name = "ame", name = "diff", visible_aliases = & ["d"], short_flag = 'd')]
Diff, Diff,
} }
@ -84,6 +80,14 @@ pub struct InstallArgs {
/// Install the packages only from the pacman-defined repositories [-Sr] /// Install the packages only from the pacman-defined repositories [-Sr]
#[clap(long, short)] #[clap(long, short)]
pub repo: bool, pub repo: bool,
/// Search packages for a given pattern instead of installing
#[clap(hidden = true, short = 's')]
pub search: bool,
/// Searches by a specific field
#[clap(long, short)]
pub by: Option<SearchBy>,
} }
#[derive(Default, Debug, Clone, Parser)] #[derive(Default, Debug, Clone, Parser)]
@ -93,25 +97,6 @@ pub struct RemoveArgs {
pub packages: Vec<String>, pub packages: Vec<String>,
} }
#[derive(Default, Debug, Clone, Parser)]
pub struct SearchArgs {
/// Searches for the relevant packages in the AUR [-Ssa]
#[clap(long, short)]
pub aur: bool,
/// Searches only pacman repos for the package [-Ssr]
#[clap(long, short)]
pub repo: bool,
/// The string the package must match in the search
#[clap(required = true)]
pub search: String,
/// Searches by a specific field
#[clap(long, short)]
pub by: Option<SearchBy>,
}
#[derive(Default, Debug, Clone, Parser)] #[derive(Default, Debug, Clone, Parser)]
pub struct QueryArgs { pub struct QueryArgs {
/// Lists AUR/foreign packages [-Qa, -Qm] /// Lists AUR/foreign packages [-Qa, -Qm]
@ -121,13 +106,10 @@ pub struct QueryArgs {
/// Lists repo/native packages [-Qr, -Qn] /// Lists repo/native packages [-Qr, -Qn]
#[clap(long, short)] #[clap(long, short)]
pub repo: bool, pub repo: bool,
}
#[derive(Default, Debug, Clone, Parser)] /// Get information about a specific package
pub struct InfoArgs { #[clap(long, short)]
/// The name of the package(s) to get info on pub info: Option<String>,
#[clap(required = true)]
pub package: String,
} }
#[derive(Default, Debug, Clone, Parser)] #[derive(Default, Debug, Clone, Parser)]

@ -65,8 +65,9 @@ fn create_if_not_exist(dir: &Path) -> &Path {
pub fn wrap_text<S: AsRef<str>>(s: S, padding: usize) -> Vec<String> { pub fn wrap_text<S: AsRef<str>>(s: S, padding: usize) -> Vec<String> {
let subsequent_padding = " ".repeat(padding); let subsequent_padding = " ".repeat(padding);
let opts = textwrap::Options::new(crossterm::terminal::size().unwrap().0 as usize - padding) let opts =
.subsequent_indent(&subsequent_padding); textwrap::Options::new(crossterm::terminal::size().unwrap().0 as usize - (padding + 1))
.subsequent_indent(&subsequent_padding);
wrap(s.as_ref(), opts) wrap(s.as_ref(), opts)
.into_iter() .into_iter()
.map(String::from) .map(String::from)

@ -1,11 +1,11 @@
use args::{Args, GenCompArgs, InfoArgs}; use args::{Args, GenCompArgs};
use builder::pacman::{PacmanColor, PacmanQueryBuilder}; use builder::pacman::{PacmanColor, PacmanQueryBuilder};
use clap::Parser; use clap::Parser;
use internal::commands::ShellCommand; use internal::commands::ShellCommand;
use internal::error::SilentUnwrap; use internal::error::SilentUnwrap;
use crate::args::{InstallArgs, Operation, QueryArgs, RemoveArgs, SearchArgs}; use crate::args::{InstallArgs, Operation, QueryArgs, RemoveArgs};
use crate::interact::page_string; use crate::interact::page_string;
use crate::internal::detect; use crate::internal::detect;
use crate::internal::exit_code::AppExitCode; use crate::internal::exit_code::AppExitCode;
@ -14,7 +14,6 @@ use crate::logging::get_logger;
use crate::logging::Printable; use crate::logging::Printable;
use clap_complete::{Generator, Shell}; use clap_complete::{Generator, Shell};
use std::env;
use std::str::FromStr; use std::str::FromStr;
mod args; mod args;
@ -49,7 +48,14 @@ async fn main() {
match args.subcommand.unwrap_or_default() { match args.subcommand.unwrap_or_default() {
Operation::Install(install_args) => cmd_install(install_args, options).await, Operation::Install(install_args) => cmd_install(install_args, options).await,
Operation::Remove(remove_args) => cmd_remove(remove_args, options).await, Operation::Remove(remove_args) => cmd_remove(remove_args, options).await,
Operation::Search(search_args) => cmd_search(search_args, options).await, Operation::Search(search_args) => {
let args = InstallArgs {
search: true,
..search_args
};
cmd_install(args, options).await;
}
Operation::Query(query_args) => cmd_query(query_args).await, Operation::Query(query_args) => cmd_query(query_args).await,
Operation::Upgrade(upgrade_args) => { Operation::Upgrade(upgrade_args) => {
tracing::info!("Performing system upgrade"); tracing::info!("Performing system upgrade");
@ -59,7 +65,6 @@ async fn main() {
tracing::info!("Removing orphaned packages"); tracing::info!("Removing orphaned packages");
operations::clean(options).await; operations::clean(options).await;
} }
Operation::Info(info_args) => cmd_info(info_args).await,
Operation::GenComp(gen_args) => cmd_gencomp(&gen_args), Operation::GenComp(gen_args) => cmd_gencomp(&gen_args),
Operation::Diff => todo!(), Operation::Diff => todo!(),
} }
@ -69,37 +74,40 @@ async fn main() {
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]
async fn cmd_install(args: InstallArgs, options: Options) { async fn cmd_install(args: InstallArgs, options: Options) {
let packages = args.packages; let packages = &args.packages;
let both = !args.aur && !args.repo;
let arg1 = env::args().collect::<Vec<String>>()[1].clone();
let both = !args.aur && arg1 != "-Sa" && arg1 != "-Sr";
let aur = args.aur || arg1 == "-Sa";
let repo = args.repo || arg1 == "-Sr";
if repo {
operations::install(packages, options).await;
return;
}
if aur {
operations::aur_install(packages, options).await;
return;
}
if both { match args.search {
let sorted = sort(&packages, options).await; true => {
if !sorted.nf.is_empty() { cmd_search(args, options).await;
crash!(
AppExitCode::PacmanError,
"Couldn't find packages: {} in repos or the AUR",
sorted.nf.join(", ")
);
}
if !sorted.repo.is_empty() {
operations::install(sorted.repo, options).await;
} }
if !sorted.aur.is_empty() { false => {
operations::aur_install(sorted.aur, options).await; if args.repo && !args.aur {
operations::install(packages.to_vec(), options).await;
return;
}
if args.aur && !args.repo {
operations::aur_install(packages.to_vec(), options).await;
return;
}
if both {
let sorted = sort(packages, options).await;
if !sorted.nf.is_empty() {
crash!(
AppExitCode::PacmanError,
"Couldn't find packages: {} in repos or the AUR",
sorted.nf.join(", ")
);
}
if !sorted.repo.is_empty() {
operations::install(sorted.repo, options).await;
}
if !sorted.aur.is_empty() {
operations::aur_install(sorted.aur, options).await;
}
}
} }
}; };
} }
@ -112,22 +120,18 @@ async fn cmd_remove(args: RemoveArgs, options: Options) {
} }
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]
async fn cmd_search(args: SearchArgs, options: Options) { async fn cmd_search(args: InstallArgs, options: Options) {
let query_string = args.search; let query_string = args.packages.join(" ");
let both = !args.aur && !args.repo;
let mut results = Vec::new(); let mut results = Vec::new();
let arg1 = env::args().collect::<Vec<String>>()[1].clone(); if args.repo || both {
let both = !args.aur && arg1 != "-Ssa" && arg1 != "-Ssr";
let aur = args.aur || arg1 == "-Ssa" || both;
let repo = args.repo || arg1 == "-Ssr" || both;
if repo {
tracing::info!("Searching repos for {}", &query_string); tracing::info!("Searching repos for {}", &query_string);
let res = operations::search(&query_string, options).await; let res = operations::search(&query_string, options).await;
results.extend(res); results.extend(res);
} }
if aur { if args.aur || both {
tracing::info!("Searching AUR for {}", &query_string); tracing::info!("Searching AUR for {}", &query_string);
let res = operations::aur_search(&query_string, args.by, options).await; let res = operations::aur_search(&query_string, args.by, options).await;
results.extend(res); results.extend(res);
@ -158,12 +162,9 @@ async fn cmd_search(args: SearchArgs, options: Options) {
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]
async fn cmd_query(args: QueryArgs) { async fn cmd_query(args: QueryArgs) {
let arg1 = env::args().collect::<Vec<String>>()[1].clone(); let both = !args.aur && !args.repo && args.info.is_none();
let both = !args.aur && arg1 != "-Qa" && arg1 != "-Qr";
let aur = args.aur || arg1 == "-Qa" || both;
let repo = args.repo || arg1 == "-Qr" || both;
if repo { if args.repo {
tracing::info!("Installed Repo Packages: "); tracing::info!("Installed Repo Packages: ");
PacmanQueryBuilder::native() PacmanQueryBuilder::native()
.color(PacmanColor::Always) .color(PacmanColor::Always)
@ -171,7 +172,8 @@ async fn cmd_query(args: QueryArgs) {
.await .await
.silent_unwrap(AppExitCode::PacmanError); .silent_unwrap(AppExitCode::PacmanError);
} }
if aur {
if args.aur {
tracing::info!("Installed AUR Packages: "); tracing::info!("Installed AUR Packages: ");
PacmanQueryBuilder::foreign() PacmanQueryBuilder::foreign()
.color(PacmanColor::Always) .color(PacmanColor::Always)
@ -179,6 +181,7 @@ async fn cmd_query(args: QueryArgs) {
.await .await
.silent_unwrap(AppExitCode::PacmanError); .silent_unwrap(AppExitCode::PacmanError);
} }
if both { if both {
tracing::info!("Installed Packages: "); tracing::info!("Installed Packages: ");
PacmanQueryBuilder::all() PacmanQueryBuilder::all()
@ -187,15 +190,14 @@ async fn cmd_query(args: QueryArgs) {
.await .await
.silent_unwrap(AppExitCode::PacmanError); .silent_unwrap(AppExitCode::PacmanError);
} }
}
#[tracing::instrument(level = "trace")] if let Some(info) = args.info {
async fn cmd_info(args: InfoArgs) { PacmanQueryBuilder::info()
PacmanQueryBuilder::info() .package(info)
.package(args.package) .query()
.query() .await
.await .silent_unwrap(AppExitCode::PacmanError);
.silent_unwrap(AppExitCode::PacmanError); }
} }
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]

Loading…
Cancel
Save