From 61dcda97f682decb9184659f9ec1babd23855660 Mon Sep 17 00:00:00 2001 From: Cno Date: Fri, 3 May 2024 01:52:31 +0800 Subject: [PATCH] feat: add search cli --- src/entrances/mirror.rs | 32 ------------------------------- src/entrances/mod.rs | 2 ++ src/entrances/search.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 19 ++++++++++++++++++- src/types/cli/mod.rs | 6 ++++++ 5 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 src/entrances/search.rs diff --git a/src/entrances/mirror.rs b/src/entrances/mirror.rs index ec97cf01..15a622e7 100644 --- a/src/entrances/mirror.rs +++ b/src/entrances/mirror.rs @@ -80,32 +80,6 @@ pub fn mirror_remove(name: &String) -> Result<()> { try_recycle(p) } -pub fn mirror_search(text: &String) -> Result> { - // 扫描出所有的镜像源目录 - let root = get_path_mirror()?; - let mirror_dirs = read_sub_dir(&root)?; - if mirror_dirs.len() == 0 { - return Err(anyhow!("Error:No mirror added yet")); - } - - // 添加扫描结果 - let mut arr = Vec::new(); - for mirror_name in mirror_dirs { - let search_res = search_index_for_mirror(text, root.join(&mirror_name).join("index"))?; - let mut mapped: Vec = search_res - .iter() - .map(|raw| { - let mut node = raw.to_owned(); - node.from_mirror = Some(mirror_name.clone()); - node - }) - .collect(); - arr.append(&mut mapped); - } - - Ok(arr) -} - // #[test] // fn test_mirror_add() { // mirror_add(&"http://localhost:3000/api/hello".to_string(), None).unwrap(); @@ -120,9 +94,3 @@ pub fn mirror_search(text: &String) -> Result> { // fn test_mirror_remove() { // mirror_remove(&"official".to_string()).unwrap(); // } - -// #[test] -// fn test_mirror_search() { -// let res=mirror_search(&"code".to_string()).unwrap(); -// println!("{res:#?}"); -// } diff --git a/src/entrances/mod.rs b/src/entrances/mod.rs index 31e0482c..17bb03c1 100644 --- a/src/entrances/mod.rs +++ b/src/entrances/mod.rs @@ -6,6 +6,7 @@ mod list; mod meta; mod mirror; mod pack; +mod search; mod uninstall; mod update; mod utils; @@ -18,5 +19,6 @@ pub use self::list::list; pub use self::meta::meta; pub use self::mirror::{mirror_add, mirror_remove, mirror_update, mirror_update_all}; pub use self::pack::pack; +pub use self::search::search; pub use self::uninstall::uninstall; pub use self::update::update_using_package; diff --git a/src/entrances/search.rs b/src/entrances/search.rs new file mode 100644 index 00000000..359bb91c --- /dev/null +++ b/src/entrances/search.rs @@ -0,0 +1,42 @@ +use anyhow::{anyhow, Result}; + +use crate::{ + types::mirror::SearchResult, + utils::{fs::read_sub_dir, get_path_mirror, mirror::search_index_for_mirror}, +}; + +pub fn search(text: &String) -> Result> { + // 扫描出所有的镜像源目录 + let root = get_path_mirror()?; + let mirror_dirs = read_sub_dir(&root)?; + if mirror_dirs.len() == 0 { + return Err(anyhow!("Error:No mirror added yet")); + } + + // 添加扫描结果 + let mut arr = Vec::new(); + for mirror_name in mirror_dirs { + let search_res = search_index_for_mirror(text, root.join(&mirror_name).join("index"))?; + let mut mapped: Vec = search_res + .iter() + .map(|raw| { + let mut node = raw.to_owned(); + node.from_mirror = Some(mirror_name.clone()); + node + }) + .collect(); + arr.append(&mut mapped); + } + + if arr.len() == 0 { + Err(anyhow!("Error:No result found with keyword '{text}'")) + } else { + Ok(arr) + } +} + +// #[test] +// fn test_search() { +// let res = search(&"vscode".to_string()).unwrap(); +// println!("{res:#?}"); +// } diff --git a/src/main.rs b/src/main.rs index 6b4d314b..b1a9c3d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn router(action: Action) -> Result { use types::cli::ActionMirror; - use crate::entrances::{mirror_add, mirror_remove, mirror_update, mirror_update_all}; + use crate::entrances::{mirror_add, mirror_remove, mirror_update, mirror_update_all, search}; let verify_signature = envmnt::get_or("OFFLINE", "false") == String::from("false"); // 匹配入口 @@ -43,6 +43,23 @@ fn router(action: Action) -> Result { .map(|_| format!("Success:Package '{package}' updated successfully")), Action::Uninstall { package_name } => uninstall(&package_name) .map(|_| format!("Success:Package '{package_name}' uninstalled successfully")), + Action::Search { keyword } => search(&keyword).map(|results| { + let len = results.len(); + let res: String = + results + .into_iter() + .fold(format!("\nFound {len} results:\n"), |acc, node| { + return acc + + &format!( + " {scope}/{name} ({version}) mirror:{mirror}\n", + name = node.name, + version = node.version, + scope = node.scope, + mirror = node.from_mirror.unwrap_or("".to_string()) + ); + }); + res + }), Action::Info { package_name } => info(None, &package_name).map(|res| format!("{res:#?}")), Action::List => list().map(|list| { if list.len() == 0 { diff --git a/src/types/cli/mod.rs b/src/types/cli/mod.rs index 27f383ff..4c892ec0 100644 --- a/src/types/cli/mod.rs +++ b/src/types/cli/mod.rs @@ -52,6 +52,12 @@ pub enum Action { package_name: String, }, + /// Search a package + Search { + /// Keyword + keyword: String, + }, + /// Pack a directory content into nep Pack { /// Source directory ready to be packed