diff --git a/src/entrances/mirror.rs b/src/entrances/mirror.rs index 10ee046f..c48bd5a5 100644 --- a/src/entrances/mirror.rs +++ b/src/entrances/mirror.rs @@ -7,7 +7,7 @@ use toml::{to_string_pretty, Value}; use crate::{ types::mirror::{MirrorHello, ServiceKeys}, utils::{ - fs::ensure_dir_exist, + fs::{ensure_dir_exist, try_recycle}, get_path_mirror, mirror::{filter_service_from_meta, read_local_mirror_meta}, }, @@ -36,13 +36,20 @@ pub fn mirror_add(url: &String, should_match_name: Option) -> Result<()> pub fn mirror_update(name: &String) -> Result<()> { // 读取 meta 文件 - let meta = read_local_mirror_meta(name)?; + let (meta, _) = read_local_mirror_meta(name)?; // 筛选出 hello 服务 let (hello_path, _) = filter_service_from_meta(meta, ServiceKeys::Hello)?; // 调用 add mirror_add(&hello_path, Some(name.to_string())) } +pub fn mirror_remove(name: &String) -> Result<()> { + // 获取 meta.toml 路径 + let (_, p) = read_local_mirror_meta(name)?; + // 移除目录 + try_recycle(p) +} + #[test] fn test_mirror_add() { mirror_add(&"http://localhost:3000/api/hello".to_string(), None).unwrap(); @@ -52,3 +59,8 @@ fn test_mirror_add() { fn test_mirror_update() { mirror_update(&"official".to_string()).unwrap(); } + +#[test] +fn test_mirror_remove() { + mirror_remove(&"official".to_string()).unwrap(); +} diff --git a/src/utils/mirror.rs b/src/utils/mirror.rs index dfac862e..22b16afd 100644 --- a/src/utils/mirror.rs +++ b/src/utils/mirror.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use anyhow::{anyhow, Result}; use fs_extra::file::read_to_string; use toml::from_str; @@ -9,15 +11,16 @@ use crate::{ }; // 读取 meta -pub fn read_local_mirror_meta(name: &String) -> Result { - let p = get_path_mirror()?.join(name).join("meta.toml"); +pub fn read_local_mirror_meta(name: &String) -> Result<(MirrorHello, PathBuf)> { + let dir_path = get_path_mirror()?.join(name); + let p = dir_path.join("meta.toml"); if !p.exists() { return Err(anyhow!("Error:Mirror '{name}' hasn't been added")); } let text = read_to_string(&p)?; let meta: MirrorHello = from_str(&text) .map_err(|e| anyhow!("Error:Invalid meta content at '{fp}' : {e}", fp = p2s!(p)))?; - Ok(meta) + Ok((meta, dir_path)) } // 从 meta 中筛选出服务,返回的第一个参数是拼接了 root_url 后的路径