Skip to content

Commit

Permalink
feat: add search cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed May 2, 2024
1 parent 7e7c9e8 commit 61dcda9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
32 changes: 0 additions & 32 deletions src/entrances/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,6 @@ pub fn mirror_remove(name: &String) -> Result<()> {
try_recycle(p)
}

pub fn mirror_search(text: &String) -> Result<Vec<SearchResult>> {
// 扫描出所有的镜像源目录
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<SearchResult> = 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();
Expand All @@ -120,9 +94,3 @@ pub fn mirror_search(text: &String) -> Result<Vec<SearchResult>> {
// 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:#?}");
// }
2 changes: 2 additions & 0 deletions src/entrances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod list;
mod meta;
mod mirror;
mod pack;
mod search;
mod uninstall;
mod update;
mod utils;
Expand All @@ -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;
42 changes: 42 additions & 0 deletions src/entrances/search.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<SearchResult>> {
// 扫描出所有的镜像源目录
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<SearchResult> = 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:#?}");
// }
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn router(action: Action) -> Result<String> {

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");

// 匹配入口
Expand All @@ -43,6 +43,23 @@ fn router(action: Action) -> Result<String> {
.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 {
Expand Down
6 changes: 6 additions & 0 deletions src/types/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61dcda9

Please sign in to comment.