From 16b5dd671c09d6402fefa902b12a8173213572e1 Mon Sep 17 00:00:00 2001 From: CrispyBaccoon <67917529+CrispyBaccoon@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:44:36 +0100 Subject: [PATCH] feat(cli/search): implement search command --- saku-cli/src/lib.rs | 1 + saku-cli/src/search.rs | 22 ++++++++++++++++++++++ src/main.rs | 12 ++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 saku-cli/src/search.rs diff --git a/saku-cli/src/lib.rs b/saku-cli/src/lib.rs index 0652b2c..35ed05a 100644 --- a/saku-cli/src/lib.rs +++ b/saku-cli/src/lib.rs @@ -1,6 +1,7 @@ pub mod env; pub mod config; pub mod show; +pub mod search; pub mod install; pub mod add; pub mod remove; diff --git a/saku-cli/src/search.rs b/saku-cli/src/search.rs new file mode 100644 index 0000000..46ae4a0 --- /dev/null +++ b/saku-cli/src/search.rs @@ -0,0 +1,22 @@ +use saku_lib::pkg::data; +use saku_lib::prelude::*; +use saku_lib::util::path; + +pub fn search(pattern: &str) -> Result<()> { + let pkgs: Vec<[String;2]> = path::pkg_match(&pattern)?; + if pkgs.len() == 0 { + return Err(make_err!(NotFound, "no packages matching that name were found")) + } + + pkgs.iter().map( + |p| { + let (group, name) = (p[0].clone(), p[1].clone()); + debug!("found pkg {}/{}", &group, &name); + let path = path::path_pkg(&group, &name); + let pkg = data::get_pkg_from_path(&path)?; + pkg.show()?; + Ok(()) + }).collect::>()?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index febb99e..c6a5bd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,12 @@ fn get_commands() -> Command { .arg_required_else_help(true) .arg(arg!( ... "Package to show")), ) + .subcommand( + Command::new("search") + .about("search for a package") + .arg_required_else_help(true) + .arg(arg!( ... "Pattern to search for")), + ) .subcommand( Command::new("list") .about("List flasks") @@ -259,6 +265,12 @@ fn main() -> Result<()> { } paths.iter().map(|name| cli::show::show(name)).collect() } + Some(("search", sub_matches)) => { + let pattern = sub_matches.get_one::("NAME").ok_or(make_err!(Missing, "pattern missing"))?; + saku_cli::search::search(&pattern)?; + + Ok(()) + } Some(("list", sub_matches)) => { let flag: Option<&bool> = sub_matches.get_one("installed"); if flag.is_some() {