Skip to content

Commit

Permalink
Merge pull request #23 from Mikachu2333/main
Browse files Browse the repository at this point in the history
feature: Add mirrors
  • Loading branch information
wtklbm authored Aug 27, 2024
2 parents 71ac9da + 44d1620 commit 5e6c171
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
[package]
name = "crm"
version = "0.2.1"
authors = ["wtklbm <wtklbm@gmail.com>"]
description = "crm can help you easy and fast switch between different cargo registries, now include: sjtu, tuna, ustc, rsproxy, bfsu, nju, hit."
homepage = "https://github.com/wtklbm/crm"
repository = "https://github.com/wtklbm/crm.git"
edition = "2018"
license = "MIT OR Apache-2.0"
keywords = ["cargo", "registry"]
exclude = [".vscode/**"]
name = "crm"
version = "0.2.2"
authors = ["wtklbm <wtklbm@gmail.com>"]
description = "crm can help you easy and fast switch between different cargo registries, now include: sjtu, tuna, ustc, rsproxy, bfsu, nju, hit, cqu, zju, CERNET."
homepage = "https://github.com/wtklbm/crm"
repository = "https://github.com/wtklbm/crm.git"
edition = "2018"
license = "MIT OR Apache-2.0"
keywords = ["cargo", "registry"]
exclude = [".vscode/**"]

[profile.release]
codegen-units = 1
opt-level = "z"
panic = "abort"
strip = true
opt-level = "z"
panic = "abort"
strip = true
#lto = "fat"

[profile.dev]
codegen-units = 512

[dependencies]
toml_edit = "0.2.1"
ureq = "2.1.1"
ureq = "2.1.1"
17 changes: 7 additions & 10 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ pub fn handle_command((command, args): Args) {
"default" => r.default(),

// 切换镜像
"use" => r.select(args.get(0)),
"use" => r.select(args.first()),

// 删除镜像
"remove" => r.remove(args.get(0)),
"remove" => r.remove(args.first()),

// 使用官方镜像执行 `cargo publish`
"publish" => r.publish(args.join(" ")),
Expand All @@ -89,7 +89,7 @@ pub fn handle_command((command, args): Args) {
"install" => r.install(args.join(" ")),

// 对镜像源网络延迟进行评估
"test" => r.test(&r.current().0, args.get(0)),
"test" => r.test(&r.current().0, args.first()),

// 获取当前镜像
"current" => {
Expand All @@ -108,20 +108,17 @@ pub fn handle_command((command, args): Args) {

// 检查版本更新
"check-update" => {
match get_newest_version() {
Some(newest) => {
if newest != APP_VERSION {
return println!(" 检测到新版本: {newest},请切换到官方镜像源以执行更新");
}
if let Some(newest) = get_newest_version() {
if newest != APP_VERSION {
return println!(" 检测到新版本: {newest},请切换到官方镜像源以执行更新");
}
None => {}
};

println!(" 暂无更新");
}

command => {
let name = args.get(0);
let name = args.first();
let addr = args.get(1);
let dl = args.get(2);

Expand Down
4 changes: 1 addition & 3 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ impl CargoConfig {
};

// 从配置文件中根据镜像名获取镜像地址
let addr = data[SOURCE][name][REGISTRY]
.as_str()
.map(|v| v.to_string());
let addr = data[SOURCE][name][REGISTRY].as_str().map(|v| v.to_string());

(name.to_string(), addr)
}
Expand Down
20 changes: 20 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,24 @@ dl = "https://crates.io/api/v1/crates"
[source.hit]
registry = "https://mirrors.hit.edu.cn/crates.io-index.git"
dl = "https://crates.io/api/v1/crates"
# 重庆大学 - sparse
[source.cqu-sparse]
registry = "sparse+https://mirrors.cqu.edu.cn/crates.io-index/"
dl = "https://crates.io/api/v1/crates"
# 浙江大学 - sparse
[source.zju-sparse]
registry = "sparse+https://mirrors.zju.edu.cn/crates.io-index/"
dl = "https://crates.io/api/v1/crates"
# CERNET聚合镜像
[source.cernet]
registry = "https://mirrors.cernet.edu.cn/crates.io-index.git"
dl = "https://crates.io/api/v1/crates"
# CERNET聚合镜像 - sparse
[source.cernet-sparse]
registry = "sparse+https://mirrors.cernet.edu.cn/crates.io-index/"
dl = "https://crates.io/api/v1/crates"
"#;
6 changes: 5 additions & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ impl RuntimeConfig {

/// 将运行时配置中的镜像列表转换为字符串
pub fn to_string(&self, current: &String, sep: Option<&str>) -> String {
let sep = if sep.is_none() { "" } else { Option::unwrap(sep) };
let sep = if sep.is_none() {
""
} else {
Option::unwrap(sep)
};

self.iter()
.fold(String::new(), |mut memo, (k, v)| {
Expand Down
9 changes: 6 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
env,
ffi::OsStr,
fmt::Display,
fs::{rename, read_to_string},
fs::{read_to_string, rename},
io,
path::{Path, PathBuf},
process::{self, Command, Output, Stdio},
Expand Down Expand Up @@ -49,7 +49,10 @@ pub fn cargo_config_path() -> PathBuf {
process::exit(20);
}
} else if obsolete_path.is_file() {
to_out(format!("检测到了 {:?} 配置文件 (不再被推荐使用),以后请使用 {:?} 配置文件", obsolete_path, path));
to_out(format!(
"检测到了 {:?} 配置文件 (不再被推荐使用),以后请使用 {:?} 配置文件",
obsolete_path, path
));
rename(obsolete_path, &path).unwrap();
}

Expand Down Expand Up @@ -245,7 +248,7 @@ pub fn exec_command(command: &str, cwd: Option<&String>) -> io::Result<Output> {

Command::new(program)
.current_dir(cwd)
.args(&[arg_c, command])
.args([arg_c, command])
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
Expand Down

0 comments on commit 5e6c171

Please sign in to comment.