Skip to content

Commit 6d5ab71

Browse files
happy-gameccmywish
authored andcommitted
fix(cargo): 修复 cargo getsrc
1. 如果存在配置文件,则过滤出 mirror 2. 如果不存在配置文件或配置文件未定义 source.mirror 则显示默认源
1 parent 885bb51 commit 6d5ab71

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/recipe/lang/Rust/Cargo.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,49 @@ pl_rust_cargo_prelude (void)
5151
void
5252
pl_rust_cargo_getsrc (char *option)
5353
{
54-
chsrc_view_file ("~/.cargo/config.toml");
54+
char *cargo_config_file = xy_normalize_path ("~/.cargo/config.toml");
55+
56+
if (xy_file_exist (cargo_config_file))
57+
{
58+
// 尝试提取 [source.mirror] 下的 registry URL
59+
char *grep_cmd = xy_str_gsub ("grep -A1 '\\[source\\.mirror\\]' '@f@' | grep 'registry' | cut -d'\"' -f2", "@f@", cargo_config_file);
60+
chsrc_ensure_program ("grep");
61+
chsrc_ensure_program ("cut");
62+
63+
char *mirror_url;
64+
int status = xy_run_get_stdout (grep_cmd, &mirror_url);
65+
char *stripped_url = (mirror_url) ? xy_str_strip(mirror_url) : "";
66+
67+
if (0 == status && stripped_url && strstr(stripped_url, "http"))
68+
{
69+
// 找到配置的镜像源,如果存在 sparse+ 前缀则去除
70+
char *clean_url = (strstr(stripped_url, "sparse+")) ?
71+
stripped_url + 7 : stripped_url;
72+
say (clean_url);
73+
}
74+
else
75+
{
76+
// 配置文件存在但没有找到镜像源配置,显示默认上游源
77+
if (ENGLISH)
78+
chsrc_note2 ("Config file exists but no mirror source found, showing default upstream source:");
79+
else
80+
chsrc_note2 ("配置文件存在但未找到镜像源配置,显示默认上游源:");
81+
82+
Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream");
83+
say (default_source.url);
84+
}
85+
}
86+
else
87+
{
88+
// 配置文件不存在,显示默认上游源
89+
if (ENGLISH)
90+
chsrc_note2 ("No source configured in Cargo, showing default upstream source:");
91+
else
92+
chsrc_note2 ("Cargo 中未配置源,显示默认上游源:");
93+
94+
Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream");
95+
say (default_source.url);
96+
}
5597
}
5698

5799

0 commit comments

Comments
 (0)