mirror of
https://github.com/RubyMetric/chsrc.git
synced 2025-03-10 12:35:17 +08:00
Add Dry Run mode
This commit is contained in:
parent
a8e8b2481c
commit
d310dce76e
@ -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,7 +379,7 @@ 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;
|
||||||
@ -398,14 +403,15 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target)
|
|||||||
}
|
}
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user