Skip to content

Commit

Permalink
feat: add mirror cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed May 2, 2024
1 parent ef69ae4 commit 7e7c9e8
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 114 deletions.
23 changes: 17 additions & 6 deletions src/entrances/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ use crate::{
},
};

pub fn mirror_add(url: &String, should_match_name: Option<String>) -> Result<()> {
pub fn mirror_add(url: &String, should_match_name: Option<String>) -> Result<String> {
// 请求 url
let res: MirrorHello = get(url)?.json()?;
let mirror_name = res.name.clone();

// 检查名称是否符合
if let Some(n) = should_match_name {
if res.name != n {
return Err(anyhow!("Error:Mirror has changed its registry name (from '{n}' to '{m}'), use 'ept mirror remove {n}' to remove the old mirror first",m=res.name));
if mirror_name != n {
return Err(anyhow!("Error:Mirror has changed its registry name (from '{n}' to '{mirror_name}'), use 'ept mirror remove {n}' to remove the old mirror first"));
}
}

// 校验
res.verify_self(&"".to_string())?;

// 写 hello.toml
let p = get_path_mirror()?.join(&res.name);
let p = get_path_mirror()?.join(&mirror_name);
ensure_dir_exist(&p)?;
let value = Value::try_from(res.clone())?;
let text = to_string_pretty(&value)?;
Expand All @@ -50,10 +51,10 @@ pub fn mirror_add(url: &String, should_match_name: Option<String>) -> Result<()>
let text = to_string_pretty(&value)?;
write(p.join("pkg-software.toml"), text)?;

Ok(())
Ok(mirror_name)
}

pub fn mirror_update(name: &String) -> Result<()> {
pub fn mirror_update(name: &String) -> Result<String> {
// 读取 meta 文件
let (meta, _) = read_local_mirror_hello(name)?;
// 筛选出 hello 服务
Expand All @@ -62,6 +63,16 @@ pub fn mirror_update(name: &String) -> Result<()> {
mirror_add(&hello_path, Some(name.to_string()))
}

pub fn mirror_update_all() -> Result<Vec<String>> {
let p = get_path_mirror()?;
let mut names = Vec::new();
for name in read_sub_dir(&p)? {
let n = mirror_update(&name)?;
names.push(n);
}
Ok(names)
}

pub fn mirror_remove(name: &String) -> Result<()> {
// 获取目录路径
let (_, p) = read_local_mirror_hello(name)?;
Expand Down
43 changes: 22 additions & 21 deletions src/entrances/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
mod clean;
pub mod config;
mod info;
mod install;
mod list;
mod meta;
mod mirror;
mod pack;
mod uninstall;
mod update;
mod utils;
mod verify;

pub use self::clean::clean;
pub use self::info::{info, info_local};
pub use self::install::install_using_package;
pub use self::list::list;
pub use self::meta::meta;
pub use self::pack::pack;
pub use self::uninstall::uninstall;
pub use self::update::update_using_package;
mod clean;
pub mod config;
mod info;
mod install;
mod list;
mod meta;
mod mirror;
mod pack;
mod uninstall;
mod update;
mod utils;
mod verify;

pub use self::clean::clean;
pub use self::info::{info, info_local};
pub use self::install::install_using_package;
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::uninstall::uninstall;
pub use self::update::update_using_package;
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ use crate::entrances::{
#[cfg(not(tarpaulin_include))]
fn router(action: Action) -> Result<String> {
// 环境变量读取

use types::cli::ActionMirror;

use crate::entrances::{mirror_add, mirror_remove, mirror_update, mirror_update_all};
let verify_signature = envmnt::get_or("OFFLINE", "false") == String::from("false");

// 匹配入口
Expand Down Expand Up @@ -87,6 +91,27 @@ fn router(action: Action) -> Result<String> {
.map(|location| format!("Success:Initial config stored at '{location}'")),
ActionConfig::Which => config_which(),
},
Action::Mirror { operation } => match operation {
ActionMirror::Add { url } => {
mirror_add(&url, None).map(|name| format!("Success:Mirror '{name}' has been added"))
}
ActionMirror::Update { name } => {
if let Some(n) = name {
mirror_update(&n)
.map(|name| format!("Success:Index of mirror '{name}' has been updated"))
} else {
mirror_update_all().map(|names| {
format!(
"Success:Index of mirrors '({name})' has been updated",
name = names.join(", ")
)
})
}
}
ActionMirror::Remove { name } => {
mirror_remove(&name).map(|_| format!("Success:Mirror '{name}' has been removed"))
}
},
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/types/cli/mirror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap::Subcommand;

#[derive(Subcommand, Debug)]
pub enum ActionMirror {
/// Add mirror
Add {
/// Mirror url
url: String,
},
/// Update mirror index
Update {
/// (Optional) Mirror name, update all if not provided
name: Option<String>,
},
/// Remove mirror
Remove {
/// Mirror name
name: String,
},
}
181 changes: 94 additions & 87 deletions src/types/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,87 +1,94 @@
mod config;

pub use self::config::ActionConfig;
use clap::{Parser, Subcommand};

/// [Alpha] Edgeless Package Tool (ept) for Edgeless Next-Generation Packages (nep)
#[derive(Parser, Debug)]
#[command(version)]
pub struct Args {
#[command(subcommand)]
pub action: Action,
/// Confirm each "Yes or No" question
#[arg(short, long)]
pub yes: bool,

/// Strict mode, throw immediately when a workflow step goes wrong
#[arg(short, long)]
pub strict: bool,

/// (Dangerous) Disable online Edgeless CA to skip signature signing or verifying
#[arg(long)]
pub offline: bool,

/// Tweaking certain behavior when running in Edgeless QA
#[arg(long)]
pub qa: bool,

/// Run commands in debug mode
#[arg(short, long)]
pub debug: bool,
// TODO:支持日志写入文件,并检查 println!
}

#[derive(Subcommand, Debug)]
pub enum Action {
/// Install a package with path (locally in current version)
Install {
/// Package path (or package name in future versions)
package: String,
},

/// Update a package with path (locally in current version)
Update {
/// Package path (or package name in future versions)
package: String,
},

/// Uninstall a package with package name
Uninstall {
/// Package name
package_name: String,
},

/// Pack a directory content into nep
Pack {
/// Source directory ready to be packed
source_dir: String,
/// (Optional) Store packed nep at
into_file: Option<String>,
},

/// Query package information (locally in current version)
Info {
/// Package name
package_name: String,
},

/// List information of installed packages
List,

/// Get meta data of given package
Meta {
/// Source package
source_package: String,
/// (Optional) Save meta report at
save_at: Option<String>,
},

/// Clean temporary or illegal files
Clean,

/// Manage ept config files
Config {
#[command(subcommand)]
operation: ActionConfig,
},
}
mod config;
mod mirror;
pub use self::config::ActionConfig;
pub use self::mirror::ActionMirror;
use clap::{Parser, Subcommand};

/// [Alpha] Edgeless Package Tool (ept) for Edgeless Next-Generation Packages (nep)
#[derive(Parser, Debug)]
#[command(version)]
pub struct Args {
#[command(subcommand)]
pub action: Action,
/// Confirm each "Yes or No" question
#[arg(short, long)]
pub yes: bool,

/// Strict mode, throw immediately when a workflow step goes wrong
#[arg(short, long)]
pub strict: bool,

/// (Dangerous) Disable online Edgeless CA to skip signature signing or verifying
#[arg(long)]
pub offline: bool,

/// Tweaking certain behavior when running in Edgeless QA
#[arg(long)]
pub qa: bool,

/// Run commands in debug mode
#[arg(short, long)]
pub debug: bool,
// TODO:支持日志写入文件,并检查 println!
}

#[derive(Subcommand, Debug)]
pub enum Action {
/// Install a package with path (locally in current version)
Install {
/// Package path (or package name in future versions)
package: String,
},

/// Update a package with path (locally in current version)
Update {
/// Package path (or package name in future versions)
package: String,
},

/// Uninstall a package with package name
Uninstall {
/// Package name
package_name: String,
},

/// Pack a directory content into nep
Pack {
/// Source directory ready to be packed
source_dir: String,
/// (Optional) Store packed nep at
into_file: Option<String>,
},

/// Query package information (locally in current version)
Info {
/// Package name
package_name: String,
},

/// List information of installed packages
List,

/// Get meta data of given package
Meta {
/// Source package
source_package: String,
/// (Optional) Save meta report at
save_at: Option<String>,
},

/// Clean temporary or illegal files
Clean,

/// Manage ept config
Config {
#[command(subcommand)]
operation: ActionConfig,
},

/// Manage nep mirrors
Mirror {
#[command(subcommand)]
operation: ActionMirror,
},
}

0 comments on commit 7e7c9e8

Please sign in to comment.