Skip to content

Commit

Permalink
feat: add mirror remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed May 1, 2024
1 parent d1e4a9b commit c6f1870
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/entrances/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -36,13 +36,20 @@ pub fn mirror_add(url: &String, should_match_name: Option<String>) -> 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();
Expand All @@ -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();
}
9 changes: 6 additions & 3 deletions src/utils/mirror.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use fs_extra::file::read_to_string;
use toml::from_str;
Expand All @@ -9,15 +11,16 @@ use crate::{
};

// 读取 meta
pub fn read_local_mirror_meta(name: &String) -> Result<MirrorHello> {
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 后的路径
Expand Down

0 comments on commit c6f1870

Please sign in to comment.