Add Dry Run mode

This commit is contained in:
Aoran Zeng 2024-08-09 01:46:58 +08:00
parent a8e8b2481c
commit d310dce76e
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
2 changed files with 52 additions and 34 deletions

View File

@ -8,7 +8,7 @@
* Contributors : Peng Gao <gn3po4g@outlook.com> * Contributors : Peng Gao <gn3po4g@outlook.com>
* | * |
* Created on : <2023-08-29> * Created on : <2023-08-29>
* Last modified : <2024-08-08> * Last modified : <2024-08-09>
* *
* chsrc * chsrc
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -92,7 +92,7 @@ chsrc_log_cmd_result (bool result, int ret_code)
bool Cli_Option_IPv6 = false; bool Cli_Option_IPv6 = false;
bool Cli_Option_Locally = false; bool Cli_Option_Locally = false;
bool Cli_Option_InEnglish = false; bool Cli_Option_InEnglish = false;
bool CliOpt_DryRun = false;
bool bool
is_url (const char *str) is_url (const char *str)
@ -361,8 +361,13 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target)
exit (Exit_MatinerIssue); exit (Exit_MatinerIssue);
} }
bool onlyone = false; if (CliOpt_DryRun)
if (2==size) onlyone = true; {
return 1; // Dry Run 时,跳过测速
}
bool only_one = false;
if (2==size) only_one = true;
char *check_curl = xy_str_to_quietcmd ("curl --version"); char *check_curl = xy_str_to_quietcmd ("curl --version");
bool exist_curl = query_program_exist (check_curl, "curl"); bool exist_curl = query_program_exist (check_curl, "curl");
@ -374,38 +379,39 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target)
double speeds[size]; double speeds[size];
double speed = 0.0; double speed = 0.0;
for (int i=0;i<size;i++) for (int i=0; i<size; i++)
{ {
SourceInfo src = sources[i]; SourceInfo src = sources[i];
const char* url = src.mirror->__bigfile_url; const char* url = src.mirror->__bigfile_url;
if (NULL==url) if (NULL==url)
{ {
if (xy_streql ("upstream", src.mirror->code)) if (xy_streql ("upstream", src.mirror->code))
{ {
continue; // 上游默认源不测速 continue; // 上游默认源不测速
} }
else else
{ {
chsrc_warn (xy_strjoin (3, "开发者未提供 ", src.mirror->code, " 镜像站测速链接,跳过该站点")); chsrc_warn (xy_strjoin (3, "开发者未提供 ", src.mirror->code, " 镜像站测速链接,跳过该站点"));
speed = 0; speed = 0;
} }
} }
else else
{ {
printf ("%s", xy_strjoin (3, "测速 ", src.mirror->site , " ... ")); printf ("%s", xy_strjoin (3, "测速 ", src.mirror->site , " ... "));
fflush (stdout); fflush (stdout);
speed = test_speed_url (url); speed = test_speed_url (url);
} }
speeds[i] = speed; speeds[i] = speed;
} }
int fastidx = get_max_ele_idx_in_dbl_ary (speeds, size);
if (onlyone) int fast_idx = get_max_ele_idx_in_dbl_ary (speeds, size);
chsrc_succ (xy_strjoin (4, sources[fastidx].mirror->name, "", target, " 目前唯一可用镜像站,感谢他们的慷慨支持"));
if (only_one)
chsrc_succ (xy_strjoin (4, sources[fast_idx].mirror->name, "", target, " 目前唯一可用镜像站,感谢他们的慷慨支持"));
else else
puts (xy_2strjoin ("最快镜像站: ", to_green (sources[fastidx].mirror->name))); puts (xy_2strjoin ("最快镜像站: ", to_green (sources[fast_idx].mirror->name)));
return fastidx; return fast_idx;
} }
@ -623,6 +629,12 @@ static void
chsrc_run (const char *cmd, int run_option) chsrc_run (const char *cmd, int run_option)
{ {
xy_log_brkt (to_blue (App_Name), to_boldblue ("运行"), to_blue (cmd)); xy_log_brkt (to_blue (App_Name), to_boldblue ("运行"), to_blue (cmd));
if (CliOpt_DryRun)
{
return; // Dry Run 此时立即结束,并不真正执行
}
int status = system (cmd); int status = system (cmd);
if (0==status) if (0==status)
{ {

View File

@ -11,7 +11,7 @@
* | BlockLune <blocklune@gmail.com> * | BlockLune <blocklune@gmail.com>
* | * |
* Created on : <2023-08-28> * Created on : <2023-08-28>
* Last modified : <2024-08-08> * Last modified : <2024-08-09>
* *
* chsrc: Change Source * chsrc: Change Source
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -2410,6 +2410,7 @@ Chsrc_Usage[] = {
"reset <target> 重置,使用上游默认使用的源\n", "reset <target> 重置,使用上游默认使用的源\n",
"选项:", "选项:",
"-dry Dry Run,模拟换源过程,命令仅打印并不运行",
"-ipv6 使用IPv6测速", "-ipv6 使用IPv6测速",
"-local 仅对某项目而非全局换源 (通过issue命令查看支持情况)" "-local 仅对某项目而非全局换源 (通过issue命令查看支持情况)"
}; };
@ -2699,6 +2700,11 @@ main (int argc, char const *argv[])
{ {
Cli_Option_InEnglish = true; Cli_Option_InEnglish = true;
} }
else if (xy_streql (argv[i], "-dry"))
{
CliOpt_DryRun = true;
chsrc_log (to_boldyellow ("**开启Dry Run模式,模拟换源过程(跳过测速),命令仅打印并不运行**\n"));
}
else else
{ {
chsrc_error (xy_2strjoin ("未识别的命令行选项 ", argv[i])); return 1; chsrc_error (xy_2strjoin ("未识别的命令行选项 ", argv[i])); return 1;