From d310dce76ebc8875fe5d25acda08a2efe8c27f54 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Fri, 9 Aug 2024 01:46:58 +0800 Subject: [PATCH] Add Dry Run mode --- include/chsrc.h | 78 ++++++++++++++++++++++++++++--------------------- src/chsrc.c | 8 ++++- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/include/chsrc.h b/include/chsrc.h index f413170..44b3886 100644 --- a/include/chsrc.h +++ b/include/chsrc.h @@ -8,7 +8,7 @@ * Contributors : Peng Gao * | * Created on : <2023-08-29> - * Last modified : <2024-08-08> + * Last modified : <2024-08-09> * * chsrc 头文件 * ------------------------------------------------------------*/ @@ -92,7 +92,7 @@ chsrc_log_cmd_result (bool result, int ret_code) bool Cli_Option_IPv6 = false; bool Cli_Option_Locally = false; bool Cli_Option_InEnglish = false; - +bool CliOpt_DryRun = false; bool is_url (const char *str) @@ -361,8 +361,13 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target) exit (Exit_MatinerIssue); } - bool onlyone = false; - if (2==size) onlyone = true; + if (CliOpt_DryRun) + { + return 1; // Dry Run 时,跳过测速 + } + + bool only_one = false; + if (2==size) only_one = true; char *check_curl = xy_str_to_quietcmd ("curl --version"); 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 speed = 0.0; - for (int i=0;i__bigfile_url; - if (NULL==url) - { - if (xy_streql ("upstream", src.mirror->code)) - { - continue; // 上游默认源不测速 - } - else - { - chsrc_warn (xy_strjoin (3, "开发者未提供 ", src.mirror->code, " 镜像站测速链接,跳过该站点")); - speed = 0; - } - } - else - { - printf ("%s", xy_strjoin (3, "测速 ", src.mirror->site , " ... ")); - fflush (stdout); - speed = test_speed_url (url); - } - speeds[i] = speed; - } - int fastidx = get_max_ele_idx_in_dbl_ary (speeds, size); + for (int i=0; i__bigfile_url; + if (NULL==url) + { + if (xy_streql ("upstream", src.mirror->code)) + { + continue; // 上游默认源不测速 + } + else + { + chsrc_warn (xy_strjoin (3, "开发者未提供 ", src.mirror->code, " 镜像站测速链接,跳过该站点")); + speed = 0; + } + } + else + { + printf ("%s", xy_strjoin (3, "测速 ", src.mirror->site , " ... ")); + fflush (stdout); + speed = test_speed_url (url); + } + speeds[i] = speed; + } - if (onlyone) - chsrc_succ (xy_strjoin (4, sources[fastidx].mirror->name, " 是 ", target, " 目前唯一可用镜像站,感谢他们的慷慨支持")); + int fast_idx = get_max_ele_idx_in_dbl_ary (speeds, size); + + if (only_one) + chsrc_succ (xy_strjoin (4, sources[fast_idx].mirror->name, " 是 ", target, " 目前唯一可用镜像站,感谢他们的慷慨支持")); 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) { xy_log_brkt (to_blue (App_Name), to_boldblue ("运行"), to_blue (cmd)); + + if (CliOpt_DryRun) + { + return; // Dry Run 此时立即结束,并不真正执行 + } + int status = system (cmd); if (0==status) { diff --git a/src/chsrc.c b/src/chsrc.c index 299dc94..6e28c93 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -11,7 +11,7 @@ * | BlockLune * | * Created on : <2023-08-28> - * Last modified : <2024-08-08> + * Last modified : <2024-08-09> * * chsrc: Change Source —— 全平台通用命令行换源工具 * ------------------------------------------------------------*/ @@ -2410,6 +2410,7 @@ Chsrc_Usage[] = { "reset 重置,使用上游默认使用的源\n", "选项:", + "-dry Dry Run,模拟换源过程,命令仅打印并不运行", "-ipv6 使用IPv6测速", "-local 仅对某项目而非全局换源 (通过issue命令查看支持情况)" }; @@ -2699,6 +2700,11 @@ main (int argc, char const *argv[]) { Cli_Option_InEnglish = true; } + else if (xy_streql (argv[i], "-dry")) + { + CliOpt_DryRun = true; + chsrc_log (to_boldyellow ("**开启Dry Run模式,模拟换源过程(跳过测速),命令仅打印并不运行**\n")); + } else { chsrc_error (xy_2strjoin ("未识别的命令行选项 ", argv[i])); return 1;