chsrc/chsrc.c

1367 lines
36 KiB
C
Raw Normal View History

2023-08-29 15:54:21 +08:00
/* --------------------------------------------------------------
* File : chsrc.c
* Authors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-08-28>
2023-09-05 09:28:42 +08:00
* Last modified : <2023-09-05>
2023-08-29 15:54:21 +08:00
*
* chsrc:
*
2023-09-01 17:17:45 +08:00
* Change Source
2023-08-29 15:54:21 +08:00
* -------------------------------------------------------------*/
2023-08-29 23:07:48 +08:00
#include "chsrc.h"
2023-08-28 22:21:33 +08:00
2023-08-30 23:13:30 +08:00
#define Chsrc_Version "v0.1.0.20230910.pre"
2023-08-28 22:21:33 +08:00
2023-08-30 17:10:23 +08:00
/**
*
*
* @param[in] check_cmd `progname` `progname`
* 使 python Windows上
* Microsoft Store
*
* @param[in] progname
2023-08-30 15:37:02 +08:00
*/
bool
2023-08-30 17:10:23 +08:00
does_the_program_exist (char* check_cmd, char* progname)
2023-08-30 15:37:02 +08:00
{
char* which = check_cmd;
int ret = system(which);
char buf[32] = {0};
2023-08-30 17:10:23 +08:00
sprintf(buf, "错误码: %d", ret);
2023-08-30 15:37:02 +08:00
if (0!=ret) {
2023-08-30 20:34:01 +08:00
xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf));
2023-08-30 15:37:02 +08:00
return false;
} else {
2023-08-30 20:34:01 +08:00
xy_success (xy_strjoin(3, "√ 命令 ", progname, " 存在"));
2023-08-30 15:37:02 +08:00
return true;
}
}
2023-09-03 17:57:45 +08:00
/**
* _setsrc codetarget可用源中
*
* default def
*/
int
does_the_mirror_exist (source_info* sources, size_t size, char* target, char* input)
{
int idx = 0;
source_info source = sources[0];
if (xy_streql("default", input) || xy_streql("def", input)) {
xy_info ("chsrc: 默认使用由维护团队排序第一的镜像(维护者测速第一)");
return idx;
}
bool exist_b = false;
2023-09-04 09:01:33 +08:00
for (int i=0; i<size; i++)
2023-09-03 17:57:45 +08:00
{
2023-09-04 09:01:33 +08:00
source = sources[i];
2023-09-03 17:57:45 +08:00
if (xy_streql(source.mirror->code, input)) {
idx = i;
exist_b = true;
break;
}
}
if (!exist_b) {
xy_error (xy_strjoin(3, "chsrc: 镜像站 ", input, " 不存在"));
xy_error (xy_2strjoin("chsrc: 查看可使用源,请使用 chsrc list ", target));
exit(1);
}
return idx;
}
2023-09-02 16:49:55 +08:00
char*
to_human_readable_speed (double speed)
{
char* scale[] = {"Byte/s", "KByte/s", "MByte/s", "GByte/s", "TByte/s"};
int i = 0;
while (speed > 1024.0)
{
i += 1;
speed /= 1024.0;
}
char* buf = xy_malloc0(64);
sprintf(buf, "%.2f %s", speed, scale[i]);
2023-09-02 21:36:54 +08:00
return buf;
2023-09-02 16:49:55 +08:00
}
2023-09-01 20:04:51 +08:00
/**
* https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py
* C语言
*
2023-09-02 21:36:54 +08:00
* @return -1
2023-09-01 20:04:51 +08:00
*/
double
2023-09-04 09:01:33 +08:00
test_speed (const char* url)
2023-09-01 20:04:51 +08:00
{
2023-09-02 22:23:49 +08:00
// 我们用 —L因为Ruby China源会跳转到其他地方
2023-09-04 21:44:05 +08:00
char* curl_cmd = xy_strjoin(4, "curl -qsL -o ", xy_os_devnull, " -w \"%{http_code} %{speed_download}\" -m6 -A chsrc/" Chsrc_Version
2023-09-02 21:36:54 +08:00
" ", url);
2023-09-01 20:04:51 +08:00
2023-09-04 14:48:13 +08:00
// xy_info (xy_2strjoin("chsrc: 测速 ", url));
2023-09-01 20:04:51 +08:00
FILE* fp = popen(curl_cmd, "r");
char buf[64] = {0};
2023-09-02 21:36:54 +08:00
while(NULL!=fgets(buf, 64, fp));
2023-09-02 22:23:49 +08:00
// puts(buf);
2023-09-02 21:36:54 +08:00
// 如果尾部有换行,删除
char* last_lf = strrchr(buf, '\n');
if (last_lf) *last_lf = '\0';
2023-09-01 20:04:51 +08:00
char* split = strchr(buf, ' ');
2023-09-02 21:36:54 +08:00
if (split) *split = '\0';
2023-09-02 22:23:49 +08:00
// puts(buf); puts(split+1);
2023-09-01 20:04:51 +08:00
int http_code = atoi(buf);
double speed = atof(split+1);
char* speedstr = to_human_readable_speed(speed);
if (200!=http_code) {
2023-09-02 22:23:49 +08:00
xy_warn (xy_strjoin(4, "chsrc: 速度 ", speedstr, " | HTTP码 " , http_code));
} else {
xy_info (xy_2strjoin("chsrc: 速度 ", speedstr));
2023-09-01 20:04:51 +08:00
}
2023-09-02 21:36:54 +08:00
return speed;
2023-09-01 20:04:51 +08:00
}
2023-09-04 09:01:33 +08:00
#define common_cesu(s) common_cesu_(s##_sources, s##_sources_n)
int
2023-09-04 09:01:33 +08:00
common_cesu_ (source_info* sources, size_t size)
{
double speeds[size];
2023-09-04 09:01:33 +08:00
double speed = 0.0;
for (int i=0;i<size;i++)
{
source_info src = sources[i];
2023-09-04 09:01:33 +08:00
const char* url = src.mirror->__bigfile_url;
if (NULL==url) {
xy_warn ("chsrc: 跳过该站点");
speed = 0;
} else {
2023-09-04 14:48:13 +08:00
xy_info (xy_2strjoin("chsrc: 测速 ", src.mirror->site));
2023-09-04 09:01:33 +08:00
speed = test_speed (url);
}
speeds[i] = speed;
}
int fastidx = dblary_maxidx (speeds, size);
2023-09-04 11:29:49 +08:00
xy_success (xy_2strjoin("chsrc: 最快镜像站: ", sources[fastidx].mirror->name));
return fastidx;
}
2023-09-02 16:49:55 +08:00
/***************************************** 换源 *********************************************/
void
pl_ruby_getsrc (char* option)
{
char* cmd = "gem sources";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
cmd = "bundle config get mirror.https://rubygems.org";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
}
/**
* Ruby换源https://gitee.com/RubyKids/rbenv-cn
*/
void
pl_ruby_setsrc (char* option)
{
2023-09-03 17:57:45 +08:00
int index = 0;
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("gem -v");
bool exist_b = does_the_program_exist (check_cmd, "gem");
if (!exist_b) {
2023-09-04 15:10:18 +08:00
xy_error ("chsrc: 未找到 gem 命令,请检查是否存在");
return;
}
if (NULL!=option) {
index = does_the_mirror_exist (pl_ruby_sources, pl_ruby_sources_n, "ruby", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu(pl_ruby);
}
2023-09-03 17:57:45 +08:00
source_info source = pl_ruby_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection (&source);
2023-09-04 15:39:49 +08:00
char* cmd = "gem source -r https://rubygems.org/";
chsrc_logcmd(cmd);
system(cmd);
cmd = xy_2strjoin("gem source -a ", source.url);
chsrc_logcmd(cmd);
system(cmd);
2023-08-31 14:41:48 +08:00
2023-09-04 15:10:18 +08:00
check_cmd = xy_str_to_quietcmd("bundle -v");
exist_b = does_the_program_exist (check_cmd, "bundle");
2023-08-31 14:41:48 +08:00
if (!exist_b) {
2023-09-04 15:10:18 +08:00
xy_error ("chsrc: 未找到 bundle 命令,请检查是否存在");
2023-08-31 14:41:48 +08:00
return;
}
2023-09-04 15:10:18 +08:00
cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source.url);
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-08-31 14:41:48 +08:00
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-08-31 14:41:48 +08:00
}
/**
* @param[out] prog Python NULL
2023-08-28 22:21:33 +08:00
*/
void
_pl_python_check_cmd (char** prog)
2023-08-28 22:21:33 +08:00
{
2023-09-04 15:10:18 +08:00
*prog = NULL;
2023-08-30 17:10:23 +08:00
// 不要调用 python 自己,而是使用 python --version避免Windows弹出Microsoft Store
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("python --version");
2023-08-30 17:10:23 +08:00
bool exist_b = does_the_program_exist (check_cmd, "python");
if (!exist_b) {
2023-09-04 15:10:18 +08:00
check_cmd = xy_str_to_quietcmd("python3 --version");
2023-08-30 17:10:23 +08:00
exist_b = does_the_program_exist (check_cmd, "python3");
if (exist_b) *prog = "python3";
2023-08-30 17:10:23 +08:00
}
else {
*prog = "python";
2023-08-30 17:10:23 +08:00
}
if (!exist_b) {
xy_error ("chsrc: 未找到 Python 相关命令,请检查是否存在");
exit(1);
2023-08-30 17:10:23 +08:00
}
}
2023-08-28 23:10:09 +08:00
void
pl_python_getsrc (char* option)
{
char* prog = NULL;
_pl_python_check_cmd (&prog);
char* cmd = xy_2strjoin(prog, " -m pip config get global.index-url");
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
}
/**
* Python换源https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
*
* Windows上调用换源命令 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini
*/
void
pl_python_setsrc (char* option)
{
int index = 0;
char* prog = NULL;
_pl_python_check_cmd (&prog);
if (NULL!=option) {
index = does_the_mirror_exist (pl_python_sources, pl_python_sources_n, "python", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_python);
}
2023-08-30 11:33:23 +08:00
source_info source = pl_python_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection(&source);
2023-08-28 22:21:33 +08:00
char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source.url));
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-08-28 22:21:33 +08:00
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-08-28 22:21:33 +08:00
}
2023-09-03 14:48:53 +08:00
void
_pl_nodejs_check_cmd ()
{
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("npm -v");
bool exist_b = does_the_program_exist (check_cmd, "npm");
if (!exist_b) {
xy_error ("chsrc: 未找到 npm 命令,请检查是否存在");
exit(1);
2023-09-03 14:48:53 +08:00
}
}
void
pl_nodejs_getsrc (char* option)
{
_pl_nodejs_check_cmd ();
char* cmd = "npm config get registry";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
2023-09-03 14:48:53 +08:00
}
2023-08-28 22:21:33 +08:00
/**
* NodeJS换源https://npmmirror.com/
2023-08-28 22:21:33 +08:00
*/
void
pl_nodejs_setsrc (char* option)
2023-08-28 22:21:33 +08:00
{
_pl_nodejs_check_cmd();
2023-08-28 23:10:09 +08:00
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_nodejs_sources, pl_nodejs_sources_n, "nodejs", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_nodejs);
}
2023-08-28 22:21:33 +08:00
source_info source = pl_nodejs_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection (&source);
2023-08-28 22:21:33 +08:00
2023-09-04 15:39:49 +08:00
char* cmd = xy_2strjoin("npm config set registry ", source.url);
chsrc_logcmd(cmd);
2023-08-28 22:21:33 +08:00
system(cmd);
}
void
_pl_perl_check_cmd ()
{
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("perl --version");
bool exist_b = does_the_program_exist (check_cmd, "perl");
if (!exist_b) {
xy_error ("chsrc: 未找到 perl 命令,请检查是否存在");
exit(1);
}
}
void
pl_perl_getsrc (char* option)
{
_pl_perl_check_cmd ();
2023-09-04 19:19:30 +08:00
// @ccmywish: 注意prettyprint 仅仅是一个内部实现,可能不稳定,如果需要更稳定的,
// 可以使用 CPAN::Shell->o('conf', 'urllist');
// 另外上述两种方法无论哪种都要首先load()
char* cmd = "perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->prettyprint('urllist')\" ";
chsrc_logcmd(cmd);
system(cmd);
}
/**
* Perl换源https://help.mirrors.cernet.edu.cn/CPAN/
*/
void
pl_perl_setsrc (char* option)
{
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_perl_sources, pl_perl_sources_n, "perl", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_perl);
}
source_info source = pl_perl_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection (&source);
char* cmd = xy_strjoin(3,
2023-09-04 19:19:30 +08:00
"perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('urllist', 'unshift', '", source.url, "'); CPAN::HandleConfig->commit()\"");
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-08-28 22:21:33 +08:00
system(cmd);
2023-09-04 19:19:30 +08:00
xy_warn ("chsrc: 请您使用 perl -v 以及 cpan -v若 Perl >= v5.36 或 CPAN >= 2.29,请额外手动调用下面的命令");
xy_warn (" perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('pushy_https', 0);; CPAN::HandleConfig->commit()\"");
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-08-28 22:21:33 +08:00
}
2023-09-02 15:45:37 +08:00
void
_pl_php_check_cmd()
2023-09-02 15:45:37 +08:00
{
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("composer --version");
bool exist_b = does_the_program_exist (check_cmd, "composer");
if (!exist_b) {
xy_error ("chsrc: 未找到 composer 命令,请检查是否存在");
exit(1);
}
}
2023-09-03 22:11:52 +08:00
/**
* Windows上测试通过PHP用户确认
*/
void
pl_php_getsrc (char* option)
{
_pl_php_check_cmd ();
2023-09-03 22:11:52 +08:00
char* cmd = "composer config -g repositories";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-09-02 15:45:37 +08:00
system(cmd);
}
/**
* PHP https://developer.aliyun.com/composer
*/
void
pl_php_setsrc (char* option)
{
_pl_php_check_cmd();
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_php_sources, pl_php_sources_n, "php", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_php);
}
source_info source = pl_php_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection (&source);
2023-09-03 22:11:52 +08:00
char* cmd = xy_2strjoin("composer config -g repo.packagist composer ", source.url);
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-09-02 15:45:37 +08:00
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-09-02 15:45:37 +08:00
}
void
_pl_go_check_cmd ()
{
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("go version");
bool exist_b = does_the_program_exist (check_cmd, "go");
if (!exist_b) {
xy_error ("chsrc: 未找到 go 相关命令,请检查是否存在");
exit(1);
}
}
void
pl_go_getsrc (char* option)
{
_pl_go_check_cmd ();
2023-09-04 11:29:49 +08:00
char* cmd = "go env GOPROXY";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
2023-09-04 11:29:49 +08:00
system(cmd);
}
/**
* Go换源https://goproxy.cn/
*/
void
pl_go_setsrc (char* option)
{
_pl_go_check_cmd();
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_go_sources, pl_go_sources_n, "go", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_go);
}
source_info source = pl_go_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection (&source);
char* cmd = "go env -w GO111MODULE=on";
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
cmd = xy_strjoin(3, "go env -w GOPROXY=", source.url, ",direct");
2023-09-04 15:39:49 +08:00
chsrc_logcmd(cmd);
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
}
/* TODO: 暂未实现 */
void
pl_rust_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/**
* Rust
*
* https://help.mirrors.cernet.edu.cn/crates.io-index.git
*/
void
2023-08-31 22:57:09 +08:00
pl_rust_setsrc (char* option)
{
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_rust_sources, pl_rust_sources_n, "rust", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_rust);
}
source_info source = pl_rust_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection(&source);
const char* file = xy_strjoin (3,
"[source.crates-io]\n"
"replace-with = 'mirror'\n\n"
"[source.mirror]\n"
"registry = \"", source.url, "\"");
char* cmd = NULL;
if (xy_on_windows)
cmd = xy_strjoin(3, "echo ", file, ">> \%USERPROFILE%\\.cargo");
else
cmd = xy_strjoin(3, "echo ", file, ">> $HOME/.cargo");
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
}
void
pl_dotnet_getsrc (char* option)
{
xy_error ("chsrc: 暂时无法查看NuGet源若有需求请您提交issue");
}
/**
* NuGet
*/
void
2023-08-31 22:57:09 +08:00
pl_dotnet_setsrc (char* option)
{
int index = 0; char* check_cmd = NULL;
xy_error ("chsrc: 暂时无法为NuGet换源若有需求请您提交issue");
}
/* TODO: 暂未实现 */
void
pl_java_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/**
* Java
*
* https://developer.aliyun.com/mirror/maven
*/
void
2023-08-31 22:57:09 +08:00
pl_java_setsrc (char* option)
{
2023-09-04 15:10:18 +08:00
int index = 0;
2023-09-04 15:10:18 +08:00
char* check_cmd = xy_str_to_quietcmd("mvn --version");
bool mvn_exist_b = does_the_program_exist (check_cmd, "mvn");
2023-09-04 15:10:18 +08:00
check_cmd = xy_str_to_quietcmd("gradle --version");
bool gradle_exist_b = does_the_program_exist (check_cmd, "gradle");
if (!mvn_exist_b && !gradle_exist_b) {
xy_error ("chsrc: maven 与 gradle 命令均未找到,请检查是否存在(其一)");
return;
}
if (NULL!=option) {
index = does_the_mirror_exist (pl_java_sources, pl_java_sources_n, "java", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_java);
}
source_info source = pl_java_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection(&source);
if (mvn_exist_b) {
const char* file = xy_strjoin(3,
"<mirror>\n"
" <id>aliyunmaven</id>\n"
" <mirrorOf>*</mirrorOf>\n"
" <name>阿里云公共仓库</name>\n"
" <url>", source.url, "</url>\n"
"</mirror>");
xy_info ("chsrc: 请在您的 maven安装目录/conf/settings.xml 中添加:\n");
puts (file);
}
if (gradle_exist_b) {
const char* file = xy_strjoin(3,
"allprojects {\n"
" repositories {\n"
" maven { url '", source.url, "' }\n"
" mavenLocal()\n"
" mavenCentral()\n"
" }\n"
"}");
xy_info ("chsrc: 请在您的 build.gradle 中添加:\n");
puts (file);
}
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
}
2023-09-04 20:27:37 +08:00
void
pl_r_getsrc (char* option)
{
2023-09-04 21:42:05 +08:00
// 或参考https://zhuanlan.zhihu.com/p/585036231
//
// options()$repos
// options()$BioC_mirror
//
2023-09-04 20:27:37 +08:00
char* cmd = NULL;
if(xy_on_windows) {
cmd = "type %USERPROFILE%\\Documents\\.Rprofile";
} else {
cmd = "cat ~/.Rprofile";
}
chsrc_logcmd(cmd);
system(cmd);
}
/**
* R https://help.mirrors.cernet.edu.cn/CRAN/
2023-08-31 16:21:42 +08:00
*/
void
2023-08-31 22:57:09 +08:00
pl_r_setsrc (char* option)
2023-08-31 16:21:42 +08:00
{
int index = 0;
2023-08-31 16:21:42 +08:00
if (NULL!=option) {
index = does_the_mirror_exist (pl_r_sources, pl_r_sources_n, "r", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_r);
2023-08-31 16:21:42 +08:00
}
source_info source = pl_r_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection(&source);
2023-08-31 16:21:42 +08:00
2023-09-04 21:42:05 +08:00
char* bioconductor_url = xy_str_delete_suffix(xy_str_delete_suffix(source.url, "cran/"), "CRAN/");
bioconductor_url = xy_2strjoin(bioconductor_url, "bioconductor");
const char* file = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source.url, "\"))" );
2023-08-31 16:21:42 +08:00
char* cmd = NULL;
2023-09-04 21:42:05 +08:00
// 或者我们调用 r.exe --slave -e 上面的内容
if (xy_on_windows)
cmd = xy_strjoin(3, "echo ", file, " >> %USERPROFILE%/Documents/.Rprofile");
else
cmd = xy_strjoin(3, "echo ", file, " >> ~/.Rprofile");
chsrc_logcmd(cmd);
system(cmd);
file = xy_strjoin (3, "options(BioC_mirror=\"", bioconductor_url, "\")" );
// 或者我们调用 r.exe --slave -e 上面的内容
2023-08-31 16:21:42 +08:00
if (xy_on_windows)
2023-09-04 20:27:37 +08:00
cmd = xy_strjoin(3, "echo ", file, " >> %USERPROFILE%/Documents/.Rprofile");
2023-08-31 16:21:42 +08:00
else
cmd = xy_strjoin(3, "echo ", file, " >> ~/.Rprofile");
2023-09-04 20:27:37 +08:00
chsrc_logcmd(cmd);
2023-08-31 16:21:42 +08:00
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-08-31 16:21:42 +08:00
}
2023-09-05 09:28:42 +08:00
/**
* Julia的换源可以通过两种方式
* 1. startup.jl
* 2. 使
*
*
*/
void
pl_julia_getsrc (char* option)
{
2023-09-05 09:28:42 +08:00
char* cmd = NULL;
if(xy_on_windows) {
cmd = "type %USERPROFILE%\\.julia\\config\\startup.jl";
} else {
cmd = "cat ~/.julia/config/startup.jl";
}
chsrc_logcmd(cmd);
system(cmd);
}
/**
2023-09-05 09:28:42 +08:00
* Julia
* 1. https://help.mirrors.cernet.edu.cn/julia/
* 2. https://docs.julialang.org/en/v1/manual/command-line-interface/#Startup-file
2023-08-31 16:21:42 +08:00
*/
void
2023-08-31 22:57:09 +08:00
pl_julia_setsrc (char* option)
2023-08-31 16:21:42 +08:00
{
int index = 0;
2023-08-31 16:21:42 +08:00
if (NULL!=option) {
index = does_the_mirror_exist (pl_julia_sources, pl_julia_sources_n, "julia", option);
} else {
2023-09-04 09:01:33 +08:00
index = common_cesu (pl_julia);
2023-08-31 16:21:42 +08:00
}
source_info source = pl_julia_sources[index];
2023-09-04 15:39:49 +08:00
chsrc_say_selection(&source);
2023-08-31 16:21:42 +08:00
const char* file = xy_strjoin (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source.url, "\"");
2023-08-31 16:21:42 +08:00
char* cmd = NULL;
if (xy_on_windows)
2023-09-05 09:28:42 +08:00
cmd = xy_strjoin(4, xy_str_to_quietcmd("md %USERPROFILE%\\.julia\\config"),
"& echo ", file, " >> %USERPROFILE%/.julia/config/startup.jl");
2023-08-31 16:21:42 +08:00
else
2023-09-05 09:28:42 +08:00
cmd = xy_strjoin(4, xy_str_to_quietcmd("mkdir -p ~/.julia/config"),
";echo ", file, " >> ~/.julia/config/startup.jl");
2023-08-31 16:21:42 +08:00
2023-09-05 09:28:42 +08:00
chsrc_logcmd(cmd);
system(cmd);
2023-09-04 15:39:49 +08:00
chsrc_say_thanks(&source);
2023-08-31 16:21:42 +08:00
}
/**
* ubuntu不同架构下的换源是不一样的,x86架构
*/
2023-08-30 20:05:03 +08:00
void
2023-08-31 22:57:09 +08:00
os_ubuntu_setsrc (char* option)
{
int selected = 0;
2023-08-30 20:05:03 +08:00
for (int i=0;i<sizeof(os_ubuntu_sources);i++) {
// 循环测速
}
2023-08-30 20:05:03 +08:00
const char* source_name = os_ubuntu_sources[selected].mirror->name;
const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr;
const char* source_url = os_ubuntu_sources[selected].url;
2023-08-30 20:34:01 +08:00
char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak";
system(backup);
xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak");
2023-09-01 22:23:03 +08:00
char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1",
source_url,
"@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
system(cmd);
free(cmd);
// char* rm = "rm -rf /etc/apt/source.list.bak";
// system(rm);
2023-08-31 16:21:42 +08:00
xy_info ("chsrc: 为 ubuntu 命令换源");
2023-08-30 20:34:01 +08:00
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
/**
* Debian Buster HTTPS HTTPS 使 HTTP
* sudo apt install apt-transport-https ca-certificates
*/
2023-09-01 22:23:03 +08:00
void
os_debian_setsrc (char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_ubuntu_sources);i++) {
// 循环测速
}
const char* source_name = os_ubuntu_sources[selected].mirror->name;
const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr;
const char* source_url = os_ubuntu_sources[selected].url;
char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak";
system(backup);
2023-09-01 22:23:03 +08:00
xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak");
char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/debian\\/@\\1",
source_url,
"@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
system(cmd);
free(cmd);
// char* rm = "rm -rf /etc/apt/source.list.bak";
// system(rm);
2023-09-01 22:23:03 +08:00
xy_info ("chsrc: 为 debian 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
/**
* fedora29版本及以下暂不支持
*/
void
os_fedora_setsrc (char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_fedora_sources);i++) {
// 循环测速
}
const char* source_name = os_fedora_sources[selected].mirror->name;
const char* source_abbr = os_fedora_sources[selected].mirror->abbr;
const char* source_url = os_fedora_sources[selected].url;
char* backup = "cp -rf /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak";
system(backup);
backup = "cp -rf /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak";
system(backup);
xy_info ("chsrc: 备份文件名:1. /etc/yum.repos.d/fedora.repo.bak");
xy_info ("chsrc: 备份文件名:2. /etc/yum.repos.d/fedora-updates.repo.bak");
char* cmd = xy_strjoin(9, "sed -e 's|^metalink=|#metalink=|g' ",
"-e 's|^#baseurl=http://download.example/pub/fedora/linux/|baseurl=",
source_url,
"|g' ",
"-i.bak ",
"/etc/yum.repos.d/fedora.repo ",
"/etc/yum.repos.d/fedora-modular.repo ",
"/etc/yum.repos.d/fedora-updates.repo ",
"/etc/yum.repos.d/fedora-updates-modular.repo");
system(cmd);
free(cmd);
xy_info("替换文件:/etc/yum.repos.d/fedora.repo");
xy_info("新增文件:/etc/yum.repos.d/fedora-modular.repo");
xy_info("替换文件:/etc/yum.repos.d/fedora-updates.repo");
xy_info("新增文件:/etc/yum.repos.d/fedora-updates-modular.repo");
// char* rm = "rm -rf /etc/yum.repos.d/fedora.repo.bak";
// system(rm);
// char* rm = "rm -rf /etc/yum.repos.d/fedora-updates.repo.bak";
// system(rm);
xy_info ("chsrc: 为 fedora 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
void
os_kali_setsrc(char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_kali_sources);i++) {
// 循环测速
}
const char* source_name = os_kali_sources[selected].mirror->name;
const char* source_abbr = os_kali_sources[selected].mirror->abbr;
const char* source_url = os_kali_sources[selected].url;
char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak";
system(backup);
xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak");
char* cmd = xy_strjoin(3, "sed -i \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/kali\\/@\\1",
source_url,
"@g\' /etc/apt/sources.list");
system(cmd);
free(cmd);
// char* rm = "rm -rf /etc/apt/source.list.bak";
// system(rm);
xy_info ("chsrc: 为 kali 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
void
os_openbsd_setsrc(char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_openbsd_sources);i++) {
// 循环测速
}
const char* source_name = os_openbsd_sources[selected].mirror->name;
const char* source_abbr = os_openbsd_sources[selected].mirror->abbr;
const char* source_url = os_openbsd_sources[selected].url;
char* backup = "cp -rf /etc/installurl /etc/installurl.bak";
system(backup);
xy_info ("chsrc: 备份文件名: /etc/installurl.bak");
char* cmd = xy_strjoin(3,"echo ",source_url," > /etc/installurl");
system(cmd);
free(cmd);
// char* rm = "rm -rf /etc/installurl.bak";
// system(rm);
xy_info ("chsrc: 为 openbsd 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
void
os_mysys2_setsrc(char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_mysys2_sources);i++) {
// 循环测速
}
const char* source_name = os_mysys2_sources[selected].mirror->name;
const char* source_abbr = os_mysys2_sources[selected].mirror->abbr;
const char* source_url = os_mysys2_sources[selected].url;
char* backup = "cp -rf /etc/pacman.d/mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32.bak";
system(backup);
backup = "cp -rf /etc/pacman.d/mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64.bak";
system(backup);
backup = "cp -rf /etc/pacman.d/mirrorlist.msys /etc/pacman.d/mirrorlist.msys.bak";
system(backup);
xy_info ("chsrc: 备份文件名:1. /etc/pacman.d/mirrorlist.mingw32.bak");
xy_info ("chsrc: 备份文件名:2. /etc/pacman.d/mirrorlist.mingw64.bak");
xy_info ("chsrc: 备份文件名:3. /etc/pacman.d/mirrorlist.msys.bak");
char* cmd = xy_strjoin(3,"sed -i \"s#https\?://mirror.msys2.org/#",source_url,"#g\" /etc/pacman.d/mirrorlist* ");
system(cmd);
free(cmd);
// char* rm = "rm -rf /etc/pacman.d/mirrorlist.mingw32.bak";
// system(rm);
// rm = "rm -rf /etc/pacman.d/mirrorlist.mingw64.bak";
// system(rm);
// rm = "rm -rf /etc/pacman.d/mirrorlist.msys.bak";
// system(rm);
xy_info ("chsrc: 为 mysys2 命令换源");
2023-09-01 22:23:03 +08:00
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
2023-08-30 20:34:01 +08:00
2023-08-30 22:27:08 +08:00
2023-09-02 19:07:30 +08:00
/************************************** Begin Target Matrix ****************************************/
def_target_info(pl_ruby);
2023-09-04 09:01:33 +08:00
def_target_info(pl_python);
2023-09-04 15:24:09 +08:00
def_target_info(pl_nodejs);
def_target_info(pl_perl);
2023-09-05 09:28:42 +08:00
def_target_info(pl_php);
def_target_info(pl_go);
def_target_info(pl_r);
def_target_info(pl_julia);
2023-09-02 17:18:44 +08:00
target_info
pl_rust_target = {pl_rust_setsrc, NULL, pl_rust_sources, pl_rust_sources_n},
pl_dotnet_target = {pl_dotnet_setsrc, NULL, pl_dotnet_sources, pl_dotnet_sources_n},
2023-09-05 09:28:42 +08:00
pl_java_target = {pl_java_setsrc, NULL, pl_java_sources, pl_java_sources_n};
2023-09-02 17:18:44 +08:00
#define targetinfo(t) (const char const*)t
2023-08-28 22:43:37 +08:00
static const char const
2023-09-02 17:18:44 +08:00
*pl_ruby [] = {"gem", "ruby", "rb", "rubygems", NULL, targetinfo(&pl_ruby_target)},
*pl_python[] = {"pip", "python", "py", "pypi", NULL, targetinfo(&pl_python_target)},
*pl_nodejs[] = {"npm", "node", "js", "nodejs", NULL, targetinfo(&pl_nodejs_target)},
*pl_perl [] = {"perl", "cpan", NULL, targetinfo(&pl_perl_target)},
*pl_rust [] = {"rust", "cargo", "crate", "crates", NULL, targetinfo(&pl_rust_target)},
*pl_go [] = {"go", "golang", "goproxy", NULL, targetinfo(&pl_go_target)} ,
*pl_dotnet[] = {"nuget", "net", ".net", "dotnet", NULL, targetinfo(&pl_dotnet_target)},
*pl_java [] = {"java", "maven", "gradle", NULL, targetinfo(&pl_java_target)},
*pl_php [] = {"php", "composer", NULL, targetinfo(&pl_php_target)},
*pl_r [] = {"r", "cran", NULL, targetinfo(&pl_r_target)},
*pl_julia [] = {"julia", NULL, targetinfo(&pl_julia_target)},
2023-08-31 22:57:09 +08:00
**pl_packagers[] =
{
2023-08-30 22:27:08 +08:00
pl_ruby, pl_python, pl_nodejs, pl_perl,
2023-09-02 17:18:44 +08:00
pl_rust, pl_go, pl_dotnet, pl_java, pl_php,
pl_r, pl_julia
};
2023-08-30 22:27:08 +08:00
2023-09-02 17:18:44 +08:00
target_info
2023-09-04 09:01:33 +08:00
os_ubuntu_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_debian_target = {os_debian_setsrc, NULL, os_debian_sources, 7},
os_fedora_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_kali_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_openbsd_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_mysys2_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7};
2023-09-02 17:18:44 +08:00
static const char const
*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)},
*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_fedora [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_kali [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_openbsd [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_mysys2 [] = {"debian", NULL, targetinfo(&os_debian_target)},
2023-08-31 22:57:09 +08:00
**os_systems[] =
{
2023-09-02 17:18:44 +08:00
os_ubuntu, os_debian
};
2023-08-31 16:38:58 +08:00
2023-09-02 17:18:44 +08:00
target_info
2023-09-04 09:01:33 +08:00
wr_anaconda_target = {NULL, NULL, NULL, 0},
wr_emacs_target = {NULL, NULL, NULL, 0},
wr_tex_target = {NULL, NULL, NULL, 0};
2023-09-02 17:18:44 +08:00
static const char const
*wr_anaconda[] = {"conda", "anaconda", NULL, targetinfo(&wr_anaconda_target)},
*wr_emacs [] = {"emacs", NULL, targetinfo(&wr_emacs_target)},
*wr_tex [] = {"latex", "ctan", "tex", NULL, targetinfo(&wr_tex_target) },
2023-08-31 22:57:09 +08:00
**wr_softwares[] =
{
2023-09-02 17:18:44 +08:00
wr_anaconda, wr_emacs, wr_tex
2023-08-31 22:57:09 +08:00
};
2023-09-02 17:18:44 +08:00
#undef targetinfo
2023-09-02 19:07:30 +08:00
/************************************** End Target Matrix ****************************************/
2023-08-30 22:27:08 +08:00
2023-08-28 22:21:33 +08:00
static const char const*
usage[] = {
2023-08-31 19:51:19 +08:00
"chsrc: Change Source \e[1;35m" Chsrc_Version "\e[0m by \e[4;31mRubyMetric\e[0m\n",
2023-08-30 11:33:23 +08:00
"维护: https://gitee.com/RubyMetric/chsrc\n",
"使用chsrc <command> [target] [mirror]",
"help 打印此帮助,或 h, -h, --help",
"list (或 ls, 或 l) 查看可用镜像源,和可换源软件",
"list mirror(s) 查看可用镜像源",
"list target(s) 查看可换源软件",
"list <target> 查看该软件可以使用哪些源",
"cesu <target> 对该软件所有源测速",
"get <target> 查看当前软件的源使用情况",
"set <target> 换源,自动测速后挑选最快源",
"set <target> def(ault) 换源,默认挑选经维护者测速排序第一的源",
"set <target> <mirror> 换源,指定使用某镜像站\n"
2023-08-28 22:21:33 +08:00
};
2023-08-28 22:43:37 +08:00
void
2023-08-29 15:54:21 +08:00
call_cmd (void* cmdptr, const char* arg)
2023-08-28 22:43:37 +08:00
{
2023-08-29 15:54:21 +08:00
void (*cmd_func)(const char*) = cmdptr;
2023-08-28 23:10:09 +08:00
if (NULL==arg) {
2023-08-30 11:33:23 +08:00
xy_info("chsrc: 将使用默认镜像");
2023-08-28 23:10:09 +08:00
}
2023-08-28 22:43:37 +08:00
cmd_func(arg);
}
2023-08-31 21:40:32 +08:00
void
print_available_mirrors ()
{
2023-09-02 20:02:59 +08:00
xy_info ("chsrc: 支持以下镜像站,荣耀均归属于这些站点,以及它们的开发/维护者们");
2023-09-03 16:42:17 +08:00
xy_warn ("chsrc: 下方code列可用于指定使用某镜像站请使用 chsrc set <target> <code>");
printf ("%-14s%-30s%-41s ", "code", "服务商缩写", "服务商URL"); puts("服务商名称");
puts ("-------------------------------------------------------------------------------------------------");
2023-08-31 21:48:05 +08:00
for (int i=0; i<xy_arylen(available_mirrors); i++)
2023-08-31 21:40:32 +08:00
{
mirror_info* mir = available_mirrors[i];
2023-09-03 16:42:17 +08:00
printf ("%-14s%-18s%-41s ", mir->code, mir->abbr, mir->site); puts(mir->name);
2023-08-31 21:40:32 +08:00
}
}
void
print_supported_targets_ (const char const*** array, size_t size)
{
for (int i=0; i<size; i++)
{
const char ** target = array[i];
const char* alias = target[0];
for (int k=1; alias!=NULL; k++)
{
printf ("%s\t", alias);
alias = target[k];
}
puts("");
}
puts("");
}
void
print_supported_targets ()
{
xy_info ("chsrc: 支持对以下目标换源 (同一行表示这几个命令兼容)");
xy_warn ("编程语言开发");
2023-08-31 21:48:05 +08:00
print_supported_targets_ (pl_packagers, xy_arylen(pl_packagers));
2023-08-31 21:40:32 +08:00
xy_warn ("操作系统");
2023-08-31 21:48:05 +08:00
print_supported_targets_ (os_systems, xy_arylen(os_systems));
2023-08-31 21:40:32 +08:00
xy_warn ("软件");
2023-08-31 21:48:05 +08:00
print_supported_targets_ (wr_softwares, xy_arylen(wr_softwares));
2023-08-31 21:40:32 +08:00
}
2023-08-31 22:57:09 +08:00
2023-09-02 16:49:55 +08:00
/**
* chsrc list <target>
*/
void
print_supported_sources_for_target (source_info sources[])
{
for (int i=0;i<4;i++)
{
source_info src = sources[i];
const mirror_info* mir = src.mirror;
2023-09-03 16:42:17 +08:00
printf ("%-14s%-18s%-50s ",mir->code, mir->abbr, src.url);
2023-09-02 16:49:55 +08:00
puts(mir->name);
}
}
2023-08-31 22:57:09 +08:00
2023-08-28 22:21:33 +08:00
int
2023-08-29 15:54:21 +08:00
print_help ()
2023-08-28 22:21:33 +08:00
{
2023-08-31 21:48:05 +08:00
for (int i=0; i<xy_arylen(usage); i++) {
2023-08-31 19:51:19 +08:00
puts (usage[i]);
2023-08-28 22:21:33 +08:00
}
}
2023-09-01 17:17:45 +08:00
/**
* targets列表`input`target匹配
*
2023-09-02 19:38:32 +08:00
* @param[out] target_info targets列表中最后的target_info信息
2023-09-01 17:17:45 +08:00
*
* @return truefalse
*/
bool
2023-09-02 19:38:32 +08:00
iterate_targets_(const char const*** array, size_t size, const char* input, const char const*** target_info)
2023-09-01 17:17:45 +08:00
{
int matched = 0;
const char const** target = NULL;
int k = 0;
const char* alias = NULL;
for (int i=0; i<size; i++) {
target = array[i];
alias = target[k];
while (NULL!=alias) {
if (xy_streql(input, alias)) {
matched = 1; break;
}
k++;
alias = target[k];
}
if (!matched) k = 0;
if (matched) break;
}
if(!matched) {
2023-09-02 19:38:32 +08:00
*target_info = NULL;
2023-09-01 17:17:45 +08:00
return false;
}
do {
k++;
alias = target[k];
} while (NULL!=alias);
2023-09-02 19:38:32 +08:00
*target_info = target + k + 1;
2023-09-01 17:17:45 +08:00
return true;
}
2023-09-02 19:38:32 +08:00
#define iterate_targets(ary, input, target) iterate_targets_(ary, xy_arylen(ary), input, target)
2023-09-01 17:17:45 +08:00
2023-09-02 20:02:59 +08:00
#define Target_Set_Source 1
#define Target_Get_Source 2
#define Target_Cesu_Source 3
#define Target_List_Source 4
2023-09-01 17:17:45 +08:00
/**
* target`code`
*
2023-09-03 17:57:45 +08:00
* @param[in] input
* @param[in] code target要执行的操作
* @param[in] option NULL
2023-09-01 17:17:45 +08:00
*
* @return truefalse
*/
bool
2023-09-03 17:57:45 +08:00
get_target (const char* input, int code, char* option)
2023-09-01 17:17:45 +08:00
{
2023-09-02 19:38:32 +08:00
const char const** target_tmp = NULL;
2023-09-01 17:17:45 +08:00
2023-09-02 19:38:32 +08:00
bool matched = iterate_targets(pl_packagers, input, &target_tmp);
if (!matched) matched = iterate_targets(os_systems, input, &target_tmp);
if (!matched) matched = iterate_targets(wr_softwares, input, &target_tmp);
2023-09-01 17:17:45 +08:00
if (!matched) {
return false;
}
2023-09-02 19:38:32 +08:00
target_info* target = (target_info*) *target_tmp;
2023-09-04 09:01:33 +08:00
if (Target_Set_Source==code)
{
2023-09-03 17:57:45 +08:00
if (target->setfn) target->setfn(option);
2023-09-02 20:02:59 +08:00
else xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现set功能欢迎贡献"));
2023-09-01 17:17:45 +08:00
}
2023-09-04 09:01:33 +08:00
else if (Target_Get_Source==code)
{
2023-09-02 20:02:59 +08:00
if (target->getfn) target->getfn("");
else xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现get功能欢迎贡献"));
2023-09-01 17:17:45 +08:00
}
2023-09-04 09:01:33 +08:00
else if (Target_List_Source==code)
{
2023-09-02 20:02:59 +08:00
xy_info (xy_strjoin(3,"chsrc: 对", input ,"支持以下镜像站,荣耀均归属于这些站点,以及它们的开发/维护者们"));
2023-09-03 16:42:17 +08:00
xy_warn (xy_strjoin(3, "chsrc: 下方code列可用于指定使用某源请使用 chsrc set ", input, " <code>"));
printf ("%-14s%-35s%-45s ", "code", "服务商缩写", "服务源URL"); puts("服务商名称");
puts ("--------------------------------------------------------------------------------------------------------");
2023-09-02 19:38:32 +08:00
print_supported_sources_for_target (target->sources);
2023-09-01 17:17:45 +08:00
}
2023-09-04 09:01:33 +08:00
else if (Target_Cesu_Source==code)
{
char* check_cmd = xy_str_to_quietcmd("curl --version");
bool exist_b = does_the_program_exist (check_cmd, "curl");
2023-09-04 19:19:30 +08:00
if (!exist_b) {
xy_error ("chsrc: 没有curl命令无法测速");
exit(1);
}
2023-09-04 09:01:33 +08:00
common_cesu_ (target->sources, target->sources_n);
return true;
2023-09-02 20:02:59 +08:00
}
2023-09-01 17:17:45 +08:00
return true;
}
2023-08-28 22:21:33 +08:00
int
2023-08-29 15:54:21 +08:00
main (int argc, char const *argv[])
2023-08-28 22:21:33 +08:00
{
2023-08-31 19:51:19 +08:00
xy_useutf8(); argc -= 1;
2023-08-29 21:58:51 +08:00
2023-08-31 19:51:19 +08:00
if (argc==0) {
2023-08-28 22:21:33 +08:00
print_help(); return 0;
}
const char* command = argv[1];
const char* target = NULL;
2023-09-01 17:17:45 +08:00
bool matched = false;
/* chsrc help */
2023-09-01 17:21:42 +08:00
if (xy_streql(command, "h") ||
xy_streql(command, "-h") ||
xy_streql(command, "help") ||
xy_streql(command, "--help"))
{
print_help();
return 0;
2023-08-28 23:10:09 +08:00
}
/* chsrc list */
2023-09-01 17:38:59 +08:00
else if (xy_streql(command, "list") ||
xy_streql(command, "l") ||
xy_streql(command, "ls"))
{
if (argc < 2) {
2023-08-31 21:40:32 +08:00
print_available_mirrors();
puts("");
print_supported_targets();
} else {
2023-09-01 17:38:59 +08:00
if (xy_streql(argv[2],"mirrors")) {
print_available_mirrors(); return 0;
}
if (xy_streql(argv[2],"mirror")) {
print_available_mirrors(); return 0;
}
if (xy_streql(argv[2],"targets")) {
print_supported_targets(); return 0;
}
if (xy_streql(argv[2],"target")) {
print_supported_targets(); return 0;
}
2023-09-03 17:57:45 +08:00
matched = get_target(argv[2], Target_List_Source, NULL);
2023-09-01 17:17:45 +08:00
if (!matched) goto not_matched;
}
return 0;
}
/* chsrc cesu */
2023-09-01 17:38:59 +08:00
else if (xy_streql(command, "cesu") ||
xy_streql(command, "ce") ||
xy_streql(command, "c"))
{
if (argc < 2) {
2023-09-01 17:38:59 +08:00
xy_error ("chsrc: 请您提供想要测速源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
2023-09-03 17:57:45 +08:00
matched = get_target(argv[2], Target_Cesu_Source, NULL);
2023-09-02 20:02:59 +08:00
if (!matched) goto not_matched;
return 0;
}
/* chsrc get */
2023-09-01 17:38:59 +08:00
else if (xy_streql(command, "get") ||
xy_streql(command, "g"))
{
if (argc < 2) {
2023-09-01 17:38:59 +08:00
xy_error ("chsrc: 请您提供想要查看源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
2023-09-03 17:57:45 +08:00
matched = get_target(argv[2], Target_Get_Source, NULL);
2023-09-01 17:17:45 +08:00
if (!matched) goto not_matched;
return 0;
}
/* chsrc set */
2023-09-01 17:38:59 +08:00
else if (xy_streql(command, "set") ||
xy_streql(command, "s"))
{
if (argc < 2) {
2023-09-01 17:38:59 +08:00
xy_error ("chsrc: 请您提供想要设置源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
2023-09-03 17:57:45 +08:00
char* option = NULL;
if (argc >= 3) {
option = (char*) argv[3]; // 暂时我们只接受最多三个参数
}
matched = get_target(argv[2], Target_Set_Source, option);
2023-09-01 17:17:45 +08:00
if (!matched) goto not_matched;
return 0;
}
/* 不支持的命令 */
else
{
xy_error ("chsrc: 不支持的命令,请使用 chsrc help 查看使用方式");
return 1;
}
2023-09-01 17:17:45 +08:00
not_matched:
2023-08-29 23:07:48 +08:00
if (!matched) {
2023-09-01 17:38:59 +08:00
xy_info("chsrc: 暂不支持的换源目标,请使用 chsrc list targets 查看可换源软件");
2023-09-01 17:17:45 +08:00
return 1;
2023-08-29 23:07:48 +08:00
}
2023-08-28 22:21:33 +08:00
}