add package search
This commit is contained in:
@@ -4,6 +4,7 @@ pub(crate) const GROUPS: &str = "groups";
|
||||
pub(crate) const IMPORT: &str = "import";
|
||||
pub(crate) const NEW: &str = "new";
|
||||
pub(crate) const REMOVE: &str = "remove";
|
||||
pub(crate) const SEARCH: &str = "search";
|
||||
pub(crate) const SHOW: &str = "show";
|
||||
pub(crate) const SYNC: &str = "sync";
|
||||
pub(crate) const UNMANAGED: &str = "unmanaged";
|
||||
|
||||
@@ -44,6 +44,12 @@ fn get_arg_parser() -> Command<'static> {
|
||||
.arg_required_else_help(true)
|
||||
.arg(Arg::new("groups").multiple_values(true)),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new(SEARCH)
|
||||
.about("search for packages which match a provided regex")
|
||||
.arg_required_else_help(true)
|
||||
.arg(Arg::new("string")),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new(SHOW)
|
||||
.about("show packages under an imported group")
|
||||
|
||||
+32
-1
@@ -5,6 +5,7 @@ use std::path::PathBuf;
|
||||
|
||||
use anyhow::{ensure, Context, Result};
|
||||
use clap::ArgMatches;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::action;
|
||||
use crate::args;
|
||||
@@ -28,7 +29,6 @@ impl Pacdef {
|
||||
|
||||
#[allow(clippy::unit_arg)]
|
||||
pub fn run_action_from_arg(self) -> Result<()> {
|
||||
// TODO new
|
||||
// TODO review
|
||||
// TODO search
|
||||
match self.args.subcommand() {
|
||||
@@ -45,6 +45,9 @@ impl Pacdef {
|
||||
Some((action::SHOW, groups)) => {
|
||||
self.show_group_content(groups).context("showing groups")
|
||||
}
|
||||
Some((action::SEARCH, args)) => {
|
||||
self.search_packages(args).context("searching packages")
|
||||
}
|
||||
Some((action::SYNC, _)) => Ok(self.install_packages()),
|
||||
Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
|
||||
Some((action::VERSION, _)) => Ok(self.show_version()),
|
||||
@@ -236,6 +239,34 @@ impl Pacdef {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn search_packages(&self, args: &ArgMatches) -> Result<()> {
|
||||
let search_string = args
|
||||
.get_one::<String>("string")
|
||||
.context("getting search string from arg")?;
|
||||
|
||||
let re = Regex::new(search_string)?;
|
||||
|
||||
let mut vec = vec![];
|
||||
|
||||
for group in &self.groups {
|
||||
for section in &group.sections {
|
||||
for package in §ion.packages {
|
||||
if re.is_match(&package.name) {
|
||||
vec.push((group, section, package))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec.sort_unstable();
|
||||
|
||||
for (g, s, p) in vec {
|
||||
println!("{}:{}:{}", g.name, s.name, p.name);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_assumed_group_file_names(arg_match: &ArgMatches) -> Result<Vec<PathBuf>> {
|
||||
|
||||
Reference in New Issue
Block a user