2023-09-09 16:49:06 +08:00
|
|
|
/** ------------------------------------------------------------
|
2024-06-11 20:18:02 +08:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2024-07-31 12:38:52 +08:00
|
|
|
* Copyright © 2023-2024 Aoran Zeng, Heng Guo
|
2024-06-11 20:18:02 +08:00
|
|
|
* -------------------------------------------------------------
|
2024-08-15 20:51:36 +08:00
|
|
|
* Project Authors : Aoran Zeng <ccmywish@qq.com>
|
2024-08-16 20:54:37 +08:00
|
|
|
* | Heng Guo <2085471348@qq.com>
|
2024-08-15 20:51:36 +08:00
|
|
|
* Contributors : Aaron Ruan <aaron212cn@outlook.com>
|
2024-08-16 20:54:37 +08:00
|
|
|
* | Rui Chen <rui@chenrui.dev>
|
2024-08-15 20:51:36 +08:00
|
|
|
* | Shengwei Chen <414685209@qq.com>
|
2024-08-16 20:54:37 +08:00
|
|
|
* | BlockLune <blocklune@gmail.com>
|
2024-08-15 20:51:36 +08:00
|
|
|
* |
|
|
|
|
* Created On : <2023-08-28>
|
2024-08-17 06:46:06 +08:00
|
|
|
* Last Modified : <2024-08-17>
|
2023-09-09 16:49:06 +08:00
|
|
|
*
|
2024-06-11 20:18:02 +08:00
|
|
|
* chsrc: Change Source —— 全平台通用命令行换源工具
|
2023-09-09 16:49:06 +08:00
|
|
|
* ------------------------------------------------------------*/
|
2023-08-29 15:54:21 +08:00
|
|
|
|
2024-08-09 03:30:30 +08:00
|
|
|
#define Chsrc_Version "v0.1.7.2.dev1-2024/08/09"
|
2024-07-24 18:42:08 +08:00
|
|
|
#define Chsrc_Maintain_URL "https://github.com/RubyMetric/chsrc"
|
|
|
|
#define Chsrc_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc"
|
2023-09-21 14:34:16 +08:00
|
|
|
|
2023-08-29 23:07:48 +08:00
|
|
|
#include "chsrc.h"
|
2023-08-28 22:21:33 +08:00
|
|
|
|
2024-08-09 02:16:09 +08:00
|
|
|
#include "recipe/lang/ruby.c"
|
2024-08-08 19:08:47 +08:00
|
|
|
#include "recipe/lang/python.c"
|
2024-08-09 02:33:10 +08:00
|
|
|
#include "recipe/lang/nodejs.c"
|
2024-08-09 02:52:42 +08:00
|
|
|
#include "recipe/lang/perl.c"
|
2024-08-09 02:55:15 +08:00
|
|
|
#include "recipe/lang/php.c"
|
2024-08-09 02:20:15 +08:00
|
|
|
#include "recipe/lang/lua.c"
|
2024-08-09 02:27:53 +08:00
|
|
|
#include "recipe/lang/go.c"
|
2024-08-09 02:58:24 +08:00
|
|
|
#include "recipe/lang/java.c"
|
2024-08-09 03:04:04 +08:00
|
|
|
#include "recipe/lang/rust.c"
|
2023-08-31 15:54:47 +08:00
|
|
|
|
2024-08-16 03:54:12 +08:00
|
|
|
#include "recipe/lang/dart.c"
|
|
|
|
#include "recipe/lang/haskell.c"
|
|
|
|
#include "recipe/lang/ocaml.c"
|
|
|
|
#include "recipe/lang/r.c"
|
|
|
|
#include "recipe/lang/julia.c"
|
2024-08-16 04:38:46 +08:00
|
|
|
#include "recipe/lang/NuGet.c"
|
2024-08-16 04:43:02 +08:00
|
|
|
#include "recipe/lang/Clojure.c"
|
2023-09-10 18:44:34 +08:00
|
|
|
|
|
|
|
|
2024-08-16 02:48:26 +08:00
|
|
|
|
2024-08-16 02:00:27 +08:00
|
|
|
#include "recipe/os/apt-family/common.h"
|
|
|
|
#include "recipe/os/apt-family/debian.c"
|
|
|
|
#include "recipe/os/apt-family/ubuntu.c"
|
2024-08-16 02:48:26 +08:00
|
|
|
// Debian-based
|
2024-08-16 02:07:38 +08:00
|
|
|
#include "recipe/os/apt-family/armbian.c"
|
2024-08-16 02:25:45 +08:00
|
|
|
#include "recipe/os/apt-family/raspberrypi.c"
|
2024-08-16 03:23:21 +08:00
|
|
|
#include "recipe/os/apt-family/Kali-Linux.c"
|
2024-08-16 02:48:26 +08:00
|
|
|
// Ubuntu-based
|
2024-08-16 03:21:51 +08:00
|
|
|
#include "recipe/os/apt-family/Linux-Mint.c"
|
2024-08-16 02:48:26 +08:00
|
|
|
#include "recipe/os/apt-family/trisquel.c"
|
2024-08-16 03:19:31 +08:00
|
|
|
#include "recipe/os/apt-family/Linux-Lite.c"
|
2024-08-16 02:48:26 +08:00
|
|
|
// Independent
|
|
|
|
#include "recipe/os/apt-family/ros.c"
|
2024-08-16 03:39:18 +08:00
|
|
|
#include "recipe/os/apt-family/openKylin.c"
|
2024-08-16 02:48:26 +08:00
|
|
|
#include "recipe/os/apt-family/deepin.c"
|
2023-09-17 11:26:31 +08:00
|
|
|
|
2024-08-16 03:34:27 +08:00
|
|
|
#include "recipe/os/yum-family/common.h"
|
2024-08-16 03:07:51 +08:00
|
|
|
#include "recipe/os/yum-family/Fedora-Linux.c"
|
2024-08-16 03:02:58 +08:00
|
|
|
#include "recipe/os/yum-family/AlmaLinux.c"
|
2024-08-16 03:13:56 +08:00
|
|
|
#include "recipe/os/yum-family/Rocky-Linux.c"
|
2024-08-16 03:34:27 +08:00
|
|
|
#include "recipe/os/yum-family/openEuler.c"
|
2024-08-16 03:50:03 +08:00
|
|
|
#include "recipe/os/yum-family/Anolis-OS.c"
|
2023-09-17 11:26:31 +08:00
|
|
|
|
2024-08-16 04:00:36 +08:00
|
|
|
#include "recipe/os/pacman-family/Arch-Linux.c"
|
|
|
|
#include "recipe/os/pacman-family/Manjaro-Linux.c"
|
|
|
|
#include "recipe/os/pacman-family/MSYS2.c"
|
2024-06-12 21:47:29 +08:00
|
|
|
|
2024-08-16 04:43:02 +08:00
|
|
|
#include "recipe/os/opensuse.c"
|
2024-08-16 03:54:12 +08:00
|
|
|
#include "recipe/os/gentoo.c"
|
2024-08-16 00:51:46 +08:00
|
|
|
#include "recipe/os/alpine.c"
|
2024-08-16 00:54:09 +08:00
|
|
|
#include "recipe/os/void.c"
|
2024-08-16 00:56:39 +08:00
|
|
|
#include "recipe/os/solus.c"
|
2024-08-16 02:40:09 +08:00
|
|
|
#include "recipe/os/openwrt.c"
|
2024-08-08 18:30:28 +08:00
|
|
|
|
2024-08-16 04:02:25 +08:00
|
|
|
#include "recipe/os/BSD/FreeBSD.c"
|
|
|
|
#include "recipe/os/BSD/NetBSD.c"
|
|
|
|
#include "recipe/os/BSD/OpenBSD.c"
|
2023-09-24 21:07:39 +08:00
|
|
|
|
|
|
|
|
2024-08-16 04:43:02 +08:00
|
|
|
|
2024-08-16 04:31:48 +08:00
|
|
|
#include "recipe/ware/TeX-Live.c"
|
2024-08-09 02:46:25 +08:00
|
|
|
#include "recipe/ware/emacs.c"
|
2024-08-16 04:11:38 +08:00
|
|
|
#include "recipe/ware/WinGet.c"
|
2024-08-16 04:07:54 +08:00
|
|
|
#include "recipe/ware/Homebrew.c"
|
2024-08-16 04:15:05 +08:00
|
|
|
#include "recipe/ware/CocoaPods.c"
|
2024-08-16 04:18:27 +08:00
|
|
|
#include "recipe/ware/Nix.c"
|
2024-08-16 04:20:48 +08:00
|
|
|
#include "recipe/ware/Guix.c"
|
2024-08-16 04:28:50 +08:00
|
|
|
#include "recipe/ware/Flathub.c"
|
2024-08-16 21:13:21 +08:00
|
|
|
#include "recipe/ware/Docker-Hub.c"
|
2024-08-16 04:28:50 +08:00
|
|
|
#include "recipe/ware/Anaconda.c"
|
2023-09-10 22:42:59 +08:00
|
|
|
|
|
|
|
|
2024-08-15 20:51:36 +08:00
|
|
|
#include "recipe/catalog.c"
|
2023-08-30 22:27:08 +08:00
|
|
|
|
|
|
|
|
2024-06-07 21:17:29 +08:00
|
|
|
static const char *
|
2024-06-08 13:26:52 +08:00
|
|
|
Chsrc_Usage[] = {
|
2024-07-24 18:42:08 +08:00
|
|
|
"维护: " Chsrc_Maintain_URL "\n",
|
2023-08-30 11:33:23 +08:00
|
|
|
|
2024-06-05 13:51:24 +08:00
|
|
|
"使用: chsrc <command> [options] [target] [mirror]",
|
2023-09-03 20:49:55 +08:00
|
|
|
"help 打印此帮助,或 h, -h, --help",
|
2024-06-08 13:26:52 +08:00
|
|
|
"issue 查看相关issue",
|
2023-09-17 13:33:41 +08:00
|
|
|
"list (或 ls, 或 l) 列出可用镜像源,和可换源软件",
|
|
|
|
"list mirror/target 列出可用镜像源,或可换源软件",
|
|
|
|
"list os/lang/ware 列出可换源的操作系统/编程语言/软件",
|
2024-08-17 06:46:06 +08:00
|
|
|
"list <target> 查看该软件的可用源与支持功能",
|
2023-09-03 20:49:55 +08:00
|
|
|
"cesu <target> 对该软件所有源测速",
|
|
|
|
"get <target> 查看当前软件的源使用情况",
|
|
|
|
"set <target> 换源,自动测速后挑选最快源",
|
2024-06-05 13:38:04 +08:00
|
|
|
"set <target> first 换源,使用维护团队测速第一的源",
|
2024-06-05 15:07:13 +08:00
|
|
|
"set <target> <mirror> 换源,指定使用某镜像站 (通过list命令查看)",
|
2024-06-14 17:00:38 +08:00
|
|
|
"set <target> https://abc 换源,用户自定义源URL",
|
2024-06-05 16:13:46 +08:00
|
|
|
"reset <target> 重置,使用上游默认使用的源\n",
|
2024-06-05 13:51:24 +08:00
|
|
|
|
|
|
|
"选项:",
|
2024-08-09 01:46:58 +08:00
|
|
|
"-dry Dry Run,模拟换源过程,命令仅打印并不运行",
|
2024-06-05 13:51:24 +08:00
|
|
|
"-ipv6 使用IPv6测速",
|
2024-06-11 22:21:18 +08:00
|
|
|
"-local 仅对某项目而非全局换源 (通过issue命令查看支持情况)"
|
2023-08-28 22:21:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-31 21:40:32 +08:00
|
|
|
|
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_available_mirrors ()
|
2023-08-31 21:40:32 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_info ("支持以下镜像站");
|
2024-07-24 18:54:20 +08:00
|
|
|
chsrc_info ("下方 code 列,可用于指定使用某镜像站,请使用 chsrc set <target> <code>\n");
|
2024-08-15 23:04:56 +08:00
|
|
|
printf (" %-13s%-35s%-38s", "code", "镜像站简写", "镜像站URL"); puts ("镜像站名称");
|
|
|
|
puts ("------- ----------- ------------------------------------- ---------------------");
|
|
|
|
// puts ("-------------------------------------------------------------------------------------------------");
|
2024-07-24 18:54:20 +08:00
|
|
|
for (int i = 0; i < xy_arylen (available_mirrors); i++)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-05 14:42:31 +08:00
|
|
|
MirrorSite* mir = available_mirrors[i];
|
2024-07-24 18:54:20 +08:00
|
|
|
printf ("%-14s%-18s%-41s ", mir->code, mir->abbr, mir->site); puts (mir->name);
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2023-08-31 21:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (const char ***array, size_t size)
|
2023-08-31 21:40:32 +08:00
|
|
|
{
|
|
|
|
for (int i=0; i<size; i++)
|
|
|
|
{
|
2024-04-25 16:33:17 +08:00
|
|
|
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 ("");
|
2023-08-31 21:40:32 +08:00
|
|
|
}
|
2024-04-25 16:33:17 +08:00
|
|
|
puts ("");
|
2023-08-31 21:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets ()
|
2023-08-31 21:40:32 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_info ("支持对以下目标换源 (同一行表示这几个命令兼容)"); puts("");
|
2024-06-08 15:13:37 +08:00
|
|
|
puts (xy_str_to_blue ("编程语言开发"));
|
2024-06-05 12:50:43 +08:00
|
|
|
puts ("-------------------------");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (pl_packagers, xy_arylen(pl_packagers));
|
2024-06-08 15:13:37 +08:00
|
|
|
puts (xy_str_to_blue ("操作系统"));
|
2024-06-05 12:50:43 +08:00
|
|
|
puts ("-------------------------");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (os_systems, xy_arylen(os_systems));
|
2024-06-08 15:13:37 +08:00
|
|
|
puts (xy_str_to_blue ("软件"));
|
2024-06-05 12:50:43 +08:00
|
|
|
puts ("-------------------------");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (wr_softwares, xy_arylen(wr_softwares));
|
2023-08-31 21:40:32 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 13:28:50 +08:00
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_pl ()
|
2023-09-17 13:28:50 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_info ("支持对以下编程语言生态换源 (同一行表示这几个命令兼容)");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (pl_packagers, xy_arylen(pl_packagers));
|
2023-09-17 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_os ()
|
2023-09-17 13:28:50 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_info ("支持对以下操作系统换源 (同一行表示这几个命令兼容)");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (os_systems, xy_arylen(os_systems));
|
2023-09-17 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_wr ()
|
2023-09-17 13:28:50 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_info ("支持对以下软件换源 (同一行表示这几个命令兼容)");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets_ (wr_softwares, xy_arylen(wr_softwares));
|
2023-09-17 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
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
|
2024-08-16 20:54:37 +08:00
|
|
|
cli_print_target_available_sources (SourceInfo sources[], size_t size)
|
2023-09-02 16:49:55 +08:00
|
|
|
{
|
2023-09-09 16:56:03 +08:00
|
|
|
for (int i=0;i<size;i++)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
|
|
|
SourceInfo src = sources[i];
|
2024-06-05 14:42:31 +08:00
|
|
|
const MirrorSite *mir = src.mirror;
|
2024-07-24 18:54:20 +08:00
|
|
|
if (NULL == src.url)
|
|
|
|
{
|
|
|
|
src.url = "Please help to add the upstream url!";
|
|
|
|
}
|
2024-04-25 16:33:17 +08:00
|
|
|
printf ("%-14s%-18s%-50s ", mir->code, mir->abbr, src.url);
|
|
|
|
puts (mir->name);
|
|
|
|
}
|
2023-09-02 16:49:55 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 20:54:37 +08:00
|
|
|
void
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_target_features (FeatInfo f, const char *input_target_name)
|
2024-08-16 20:54:37 +08:00
|
|
|
{
|
|
|
|
printf (to_boldpurple("\nFeatures:\n\n"));
|
|
|
|
|
2024-08-17 06:46:06 +08:00
|
|
|
char *get_msg = xy_2strjoin (" Get: 查看当前源状态 | chsrc get ", input_target_name);
|
2024-08-17 06:50:20 +08:00
|
|
|
if (f.can_get) printf (" %s%s\n", to_boldgreen(YesMark), to_purple(get_msg));
|
|
|
|
else printf (" %s%s\n", to_boldred(NoMark), get_msg);puts("");
|
2024-08-17 06:46:06 +08:00
|
|
|
|
|
|
|
char *reset_msg = xy_2strjoin (" Reset: 重置回默认源 | chsrc reset ", input_target_name);
|
2024-08-17 06:50:20 +08:00
|
|
|
if (f.can_reset) printf (" %s%s\n", to_boldgreen(YesMark), to_purple(reset_msg));
|
|
|
|
else printf (" %s%s\n", to_boldred(NoMark), reset_msg);puts("");
|
2024-08-17 06:46:06 +08:00
|
|
|
|
|
|
|
char *user_define_msg = xy_strjoin (4, " UserDefine: 用户自定义换源URL | chsrc set ", input_target_name, " https://user-define-url.org/", input_target_name);
|
2024-08-17 06:50:20 +08:00
|
|
|
if (f.can_user_define) printf (" %s%s\n", to_boldgreen(YesMark), to_purple(user_define_msg));
|
|
|
|
else printf (" %s%s\n", to_boldred(NoMark), user_define_msg);puts("");
|
2024-08-17 06:46:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
char *locally_msg = xy_2strjoin (" Locally: 仅对本项目换源 | chsrc set -local ", input_target_name);
|
|
|
|
|
2024-08-17 06:50:20 +08:00
|
|
|
switch (f.stcan_locally)
|
|
|
|
{
|
|
|
|
case CanNotFully:
|
|
|
|
printf (" %s%s\n", to_boldred(NoMark), locally_msg);puts("");
|
|
|
|
break;
|
|
|
|
case CanFully:
|
|
|
|
printf (" %s%s\n", to_boldgreen(YesMark), to_purple(locally_msg));puts("");
|
|
|
|
break;
|
|
|
|
case CanSemi:
|
|
|
|
printf (" %s%s\n\n %s\n", to_boldgreen(SemiYesMark), to_purple(locally_msg), f.locally);puts("");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xy_unreach;
|
2024-08-17 06:46:06 +08:00
|
|
|
}
|
2024-08-16 20:54:37 +08:00
|
|
|
|
2024-08-17 06:46:06 +08:00
|
|
|
char *english_msg = xy_2strjoin (" English: 英文输出 | chsrc set -en ", input_target_name);
|
|
|
|
if (f.can_english) printf (" %s%s\n", to_boldgreen(YesMark), to_purple(english_msg));
|
|
|
|
else printf (" %s%s\n", to_boldred(NoMark), english_msg);
|
2024-08-16 20:54:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (f.note)
|
|
|
|
{
|
|
|
|
printf ("\n%s%s\n", to_boldyellow ("备注: "), to_boldyellow (f.note));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:49:55 +08:00
|
|
|
|
2023-09-06 17:06:09 +08:00
|
|
|
void
|
2024-06-05 09:24:39 +08:00
|
|
|
cli_print_version ()
|
2023-08-28 22:21:33 +08:00
|
|
|
{
|
2024-07-09 14:38:52 +08:00
|
|
|
puts (xy_strjoin (3, "chsrc: Change Source (GPL-3.0) ",
|
2024-06-07 20:24:00 +08:00
|
|
|
xy_str_to_magenta (Chsrc_Version), " by RubyMetric"));
|
2024-06-05 09:24:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_print_help ()
|
|
|
|
{
|
2024-07-24 18:42:08 +08:00
|
|
|
puts ("");
|
|
|
|
for (int i=0; i<xy_arylen (Chsrc_Usage); i++)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-08 13:26:52 +08:00
|
|
|
puts (Chsrc_Usage[i]);
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
}
|
|
|
|
|
2024-06-08 13:14:14 +08:00
|
|
|
void
|
|
|
|
cli_print_issues ()
|
|
|
|
{
|
|
|
|
puts (
|
|
|
|
"chsrc issues: Gitee和GitHub两边同时接受issue\n\n"
|
|
|
|
"- https://gitee.com/RubyMetric/chsrc/issues\n"
|
|
|
|
"- https://github.com/RubyMetric/chsrc/issues\n\n"
|
|
|
|
|
|
|
|
"目前支持reset命令的源: https://gitee.com/RubyMetric/chsrc/issues/I9V6EV\n"
|
|
|
|
"目前支持-local选项的源: https://gitee.com/RubyMetric/chsrc/issues/I9V5I0\n"
|
|
|
|
"已收录的镜像站: https://gitee.com/RubyMetric/chsrc/wikis\n"
|
|
|
|
);
|
2024-06-08 13:26:52 +08:00
|
|
|
|
2024-06-14 01:02:23 +08:00
|
|
|
if (chsrc_check_program ("gh"))
|
2024-06-08 13:26:52 +08:00
|
|
|
{
|
2024-06-14 01:02:23 +08:00
|
|
|
char *cmd = xy_str_to_quietcmd ("gh browse --repo RubyMetric/chsrc");
|
|
|
|
system (cmd);
|
2024-06-08 13:26:52 +08:00
|
|
|
}
|
2024-06-08 13:14:14 +08:00
|
|
|
}
|
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 匹配到则返回true,未匹配到则返回false
|
|
|
|
*/
|
|
|
|
bool
|
2024-04-25 16:33:17 +08:00
|
|
|
iterate_targets_ (const char ***array, size_t size, const char *input, const char ***target_info)
|
2023-09-01 17:17:45 +08:00
|
|
|
{
|
|
|
|
int matched = 0;
|
|
|
|
|
2024-04-25 16:33:17 +08:00
|
|
|
const char **target = NULL;
|
2023-09-01 17:17:45 +08:00
|
|
|
int k = 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
const char *alias = NULL;
|
2023-09-01 17:17:45 +08:00
|
|
|
|
2024-04-25 16:33:17 +08:00
|
|
|
for (int i=0; i<size; i++)
|
|
|
|
{
|
|
|
|
target = array[i];
|
2023-09-01 17:17:45 +08:00
|
|
|
alias = target[k];
|
2024-04-25 16:33:17 +08:00
|
|
|
while (NULL!=alias)
|
|
|
|
{
|
|
|
|
if (xy_streql(input, alias))
|
|
|
|
{
|
|
|
|
matched = 1; break;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
alias = target[k];
|
|
|
|
}
|
|
|
|
if (!matched) k = 0;
|
|
|
|
if (matched) break;
|
2023-09-01 17:17:45 +08:00
|
|
|
}
|
|
|
|
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched)
|
|
|
|
{
|
|
|
|
*target_info = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-01 17:17:45 +08:00
|
|
|
|
|
|
|
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-10-04 18:48:35 +08:00
|
|
|
typedef enum {
|
2024-06-07 22:28:06 +08:00
|
|
|
TargetOp_Get_Source = 1,
|
|
|
|
TargetOp_Set_Source,
|
|
|
|
TargetOp_Reset_Source,
|
2024-06-05 12:50:43 +08:00
|
|
|
TargetOp_Cesu_Source,
|
|
|
|
TargetOp_List_Source
|
2023-10-04 18:48:35 +08:00
|
|
|
} TargetOp;
|
2023-09-01 17:17:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 寻找target,并根据`code`执行相应的操作
|
|
|
|
*
|
2023-09-05 14:17:31 +08:00
|
|
|
* @param input 用户输入的目标
|
|
|
|
* @param code 对target要执行的操作
|
|
|
|
* @param option 额外的指示,可为NULL
|
2023-09-01 17:17:45 +08:00
|
|
|
*
|
|
|
|
* @return 找到目标返回true,未找到返回false
|
|
|
|
*/
|
|
|
|
bool
|
2024-04-25 16:33:17 +08:00
|
|
|
get_target (const char *input, TargetOp code, char *option)
|
2023-09-01 17:17:45 +08:00
|
|
|
{
|
2024-04-25 16:33:17 +08:00
|
|
|
const char **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
|
|
|
|
2024-08-16 20:54:37 +08:00
|
|
|
if (!matched) return false;
|
2023-09-01 17:17:45 +08:00
|
|
|
|
2024-04-25 16:33:17 +08:00
|
|
|
TargetInfo *target = (TargetInfo*) *target_tmp;
|
2023-09-02 19:38:32 +08:00
|
|
|
|
2024-06-05 12:50:43 +08:00
|
|
|
if (TargetOp_Set_Source==code)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
|
|
|
if (target->setfn) target->setfn(option);
|
2024-06-08 13:26:52 +08:00
|
|
|
else chsrc_error (xy_strjoin (3, "暂未对 ", input, " 实现set功能,邀您帮助: chsrc issue"));
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-06-07 22:28:06 +08:00
|
|
|
else if (TargetOp_Reset_Source==code)
|
|
|
|
{
|
2024-06-07 23:56:42 +08:00
|
|
|
if (target->resetfn) target->resetfn(option);
|
2024-06-08 13:26:52 +08:00
|
|
|
else chsrc_error (xy_strjoin (3, "暂未对 ", input, " 实现reset功能,邀您帮助: chsrc issue"));
|
2024-06-07 22:28:06 +08:00
|
|
|
}
|
2024-06-05 12:50:43 +08:00
|
|
|
else if (TargetOp_Get_Source==code)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
|
|
|
if (target->getfn) target->getfn("");
|
2024-06-08 13:26:52 +08:00
|
|
|
else chsrc_error (xy_strjoin (3, "暂未对 ", input, " 实现get功能,邀您帮助: chsrc issue"));
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-06-05 12:50:43 +08:00
|
|
|
else if (TargetOp_List_Source==code)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-16 21:06:44 +08:00
|
|
|
say (to_boldblue(xy_strjoin (3, "指定使用某源,请使用 chsrc set ", input, " <code>\n")));
|
|
|
|
say (to_boldgreen("Available Sources: \n"));
|
|
|
|
// chsrc_info (xy_strjoin (3, "下方 code 列,可用于指定使用某源,请使用 chsrc set ", input, " <code>\n"));
|
2024-08-15 23:04:56 +08:00
|
|
|
printf (" %-14s%-35s%-43s ", "code", "镜像站简写", "换源URL"); puts ("镜像站名称");
|
|
|
|
puts ("--------- -------------- ----------------------------------------------- ---------------------");
|
2024-08-16 20:54:37 +08:00
|
|
|
cli_print_target_available_sources (target->sources, target->sources_n);
|
|
|
|
if (target->featfn)
|
|
|
|
{
|
|
|
|
FeatInfo fi = target->featfn("");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_target_features (fi, input);
|
2024-08-16 20:54:37 +08:00
|
|
|
}
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-06-05 12:50:43 +08:00
|
|
|
else if (TargetOp_Cesu_Source==code)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-16 19:06:16 +08:00
|
|
|
auto_select_ (target->sources, target->sources_n, input);
|
2024-04-25 16:33:17 +08:00
|
|
|
return true;
|
2023-09-04 19:19:30 +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
|
|
|
{
|
2024-04-25 16:33:17 +08:00
|
|
|
xy_useutf8 (); argc -= 1;
|
2023-08-29 21:58:51 +08:00
|
|
|
|
2024-08-16 20:54:37 +08:00
|
|
|
if (0==argc)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-07 20:24:00 +08:00
|
|
|
cli_print_version ();
|
|
|
|
cli_print_help ();
|
|
|
|
return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
|
2024-04-25 16:33:17 +08:00
|
|
|
const char *command = argv[1];
|
2023-08-31 20:24:06 +08:00
|
|
|
|
2024-06-05 09:18:48 +08:00
|
|
|
// chsrc set target mirror
|
|
|
|
// 1 2 3
|
|
|
|
int cli_arg_Target_pos = 2;
|
|
|
|
int cli_arg_Mirror_pos = cli_arg_Target_pos + 1;
|
|
|
|
const char *target = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// chsrc set -ipv6 target mirror
|
|
|
|
// 1 2 3 4
|
|
|
|
// argc = 4
|
2024-08-16 20:54:37 +08:00
|
|
|
for (int i=2; i<=argc; i++)
|
2024-06-05 09:18:48 +08:00
|
|
|
{
|
|
|
|
if (xy_str_start_with (argv[i], "-"))
|
|
|
|
{
|
|
|
|
if (xy_streql (argv[i], "-ipv6"))
|
|
|
|
{
|
2024-08-09 01:49:14 +08:00
|
|
|
CliOpt_IPv6 = true;
|
2024-06-05 09:18:48 +08:00
|
|
|
}
|
|
|
|
else if (xy_streql (argv[i], "-local"))
|
|
|
|
{
|
2024-08-09 01:49:14 +08:00
|
|
|
CliOpt_Locally = true;
|
2024-06-05 09:18:48 +08:00
|
|
|
}
|
|
|
|
else if (xy_streql (argv[i], "-en") || xy_streql (argv[i], "-english"))
|
|
|
|
{
|
2024-08-09 01:49:14 +08:00
|
|
|
CliOpt_InEnglish = true;
|
2024-06-05 09:18:48 +08:00
|
|
|
}
|
2024-08-09 01:46:58 +08:00
|
|
|
else if (xy_streql (argv[i], "-dry"))
|
|
|
|
{
|
|
|
|
CliOpt_DryRun = true;
|
|
|
|
chsrc_log (to_boldyellow ("**开启Dry Run模式,模拟换源过程(跳过测速),命令仅打印并不运行**\n"));
|
|
|
|
}
|
2024-06-05 09:18:48 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
chsrc_error (xy_2strjoin ("未识别的命令行选项 ", argv[i])); return 1;
|
|
|
|
}
|
|
|
|
cli_arg_Target_pos++;
|
|
|
|
cli_arg_Mirror_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-01 17:17:45 +08:00
|
|
|
bool matched = false;
|
|
|
|
|
2023-08-31 20:24:06 +08:00
|
|
|
/* chsrc help */
|
2024-04-25 16:33:17 +08:00
|
|
|
if (xy_streql (command, "h") ||
|
|
|
|
xy_streql (command, "-h") ||
|
|
|
|
xy_streql (command, "help") ||
|
|
|
|
xy_streql (command, "--help"))
|
|
|
|
{
|
2024-06-05 09:24:39 +08:00
|
|
|
cli_print_version ();
|
2024-06-05 09:18:48 +08:00
|
|
|
cli_print_help ();
|
2024-04-25 16:33:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2023-08-28 23:10:09 +08:00
|
|
|
|
2024-06-05 09:24:39 +08:00
|
|
|
/* chsrc -v */
|
|
|
|
else if (xy_streql (command, "-v") ||
|
|
|
|
xy_streql (command, "-version") ||
|
|
|
|
xy_streql (command, "--version")||
|
|
|
|
xy_streql (command, "ver") ||
|
|
|
|
xy_streql (command, "version"))
|
|
|
|
{
|
|
|
|
cli_print_version ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-31 20:24:06 +08:00
|
|
|
/* chsrc list */
|
2024-04-25 16:33:17 +08:00
|
|
|
else if (xy_streql (command, "list") ||
|
|
|
|
xy_streql (command, "l") ||
|
|
|
|
xy_streql (command, "ls"))
|
|
|
|
{
|
2024-06-05 09:18:48 +08:00
|
|
|
if (argc < cli_arg_Target_pos)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_available_mirrors ();
|
2024-04-25 16:33:17 +08:00
|
|
|
puts ("");
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets ();
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-05 09:18:48 +08:00
|
|
|
target = argv[cli_arg_Target_pos];
|
2024-07-31 12:38:52 +08:00
|
|
|
if (xy_streql (target, "mirrors") || xy_streql (target, "mirror"))
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_available_mirrors (); return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-07-31 12:38:52 +08:00
|
|
|
else if (xy_streql (target, "targets") || xy_streql (target, "target"))
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_targets (); return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-07-31 12:38:52 +08:00
|
|
|
else if (xy_streql (target, "os"))
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_os (); return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-07-31 12:38:52 +08:00
|
|
|
else if (xy_streql (target, "lang") || xy_streql (target, "pl") || xy_streql (target, "language"))
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_pl(); return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
2024-07-31 12:38:52 +08:00
|
|
|
else if (xy_streql (target, "ware") || xy_streql (target, "software"))
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-08-17 06:46:06 +08:00
|
|
|
cli_print_supported_wr (); return 0;
|
2024-04-25 16:33:17 +08:00
|
|
|
}
|
|
|
|
|
2024-07-31 12:38:52 +08:00
|
|
|
matched = get_target (target, TargetOp_List_Source, NULL);
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched) goto not_matched;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-08-31 20:24:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* chsrc cesu */
|
2024-04-25 16:33:17 +08:00
|
|
|
else if (xy_streql (command, "cesu") ||
|
|
|
|
xy_streql (command, "ce") ||
|
|
|
|
xy_streql (command, "c"))
|
|
|
|
{
|
2024-06-05 09:18:48 +08:00
|
|
|
if (argc < cli_arg_Target_pos)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error ("请您提供想要测速源的软件名; 使用 chsrc list targets 查看所有支持的软件");
|
2024-04-25 16:33:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2024-06-05 09:18:48 +08:00
|
|
|
target = argv[cli_arg_Target_pos];
|
2024-06-05 12:50:43 +08:00
|
|
|
matched = get_target (target, TargetOp_Cesu_Source, NULL);
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched) goto not_matched;
|
|
|
|
return 0;
|
2023-08-31 20:24:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* chsrc get */
|
2024-04-25 16:33:17 +08:00
|
|
|
else if (xy_streql (command, "get") ||
|
|
|
|
xy_streql (command, "g"))
|
|
|
|
{
|
2024-06-05 09:18:48 +08:00
|
|
|
if (argc < cli_arg_Target_pos)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error ("请您提供想要查看源的软件名; 使用 chsrc list targets 查看所有支持的软件");
|
2024-04-25 16:33:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2024-06-05 09:18:48 +08:00
|
|
|
target = argv[cli_arg_Target_pos];
|
2024-06-05 12:50:43 +08:00
|
|
|
matched = get_target (target, TargetOp_Get_Source, NULL);
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched) goto not_matched;
|
|
|
|
return 0;
|
2023-08-31 20:24:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* chsrc set */
|
2024-04-25 16:33:17 +08:00
|
|
|
else if (xy_streql (command, "set") ||
|
|
|
|
xy_streql (command, "s"))
|
|
|
|
{
|
2024-06-05 09:18:48 +08:00
|
|
|
if (argc < cli_arg_Target_pos)
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error ("请您提供想要设置源的软件名; 使用 chsrc list targets 查看所有支持的软件");
|
2024-04-25 16:33:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-03 17:57:45 +08:00
|
|
|
|
2024-06-05 09:18:48 +08:00
|
|
|
target = argv[cli_arg_Target_pos];
|
2024-06-13 23:27:59 +08:00
|
|
|
char *mirrorCode_or_url = NULL;
|
2024-07-31 12:38:52 +08:00
|
|
|
if (argc >= cli_arg_Mirror_pos)
|
|
|
|
{
|
|
|
|
mirrorCode_or_url = xy_strdup (argv[cli_arg_Mirror_pos]);
|
|
|
|
}
|
2024-06-05 12:50:43 +08:00
|
|
|
|
2024-06-13 23:27:59 +08:00
|
|
|
matched = get_target (target, TargetOp_Set_Source, mirrorCode_or_url);
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched) goto not_matched;
|
|
|
|
return 0;
|
2023-09-03 17:57:45 +08:00
|
|
|
}
|
2023-08-31 20:24:06 +08:00
|
|
|
|
2024-06-05 15:07:13 +08:00
|
|
|
/* chsrc reset */
|
|
|
|
else if (xy_streql (command, "reset") ||
|
|
|
|
xy_streql (command, "rest") ||
|
|
|
|
xy_streql (command, "r"))
|
|
|
|
{
|
|
|
|
if (argc < cli_arg_Target_pos)
|
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error ("请您提供想要重置源的软件名; 使用 chsrc list targets 查看所有支持的软件");
|
2024-06-05 15:07:13 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = argv[cli_arg_Target_pos];
|
2024-06-07 22:28:06 +08:00
|
|
|
matched = get_target (target, TargetOp_Reset_Source, NULL);
|
2024-06-05 15:07:13 +08:00
|
|
|
if (!matched) goto not_matched;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-08 13:14:14 +08:00
|
|
|
/* chsrc issue */
|
|
|
|
else if (xy_streql (command, "issue") ||
|
|
|
|
xy_streql (command, "issues") ||
|
|
|
|
xy_streql (command, "isue") ||
|
|
|
|
xy_streql (command, "i"))
|
|
|
|
{
|
|
|
|
cli_print_issues ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-31 20:24:06 +08:00
|
|
|
/* 不支持的命令 */
|
|
|
|
else
|
2024-04-25 16:33:17 +08:00
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error (xy_strjoin (3, "不支持的命令 ", command, ". 请使用 chsrc help 查看使用方式"));
|
2024-04-25 16:33:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-08-31 20:24:06 +08:00
|
|
|
|
2023-09-01 17:17:45 +08:00
|
|
|
not_matched:
|
2024-04-25 16:33:17 +08:00
|
|
|
if (!matched)
|
|
|
|
{
|
2024-06-08 08:35:07 +08:00
|
|
|
chsrc_error ("暂不支持的换源目标,请使用 chsrc list targets 查看可换源软件");
|
2024-04-25 16:33:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-08-28 22:21:33 +08:00
|
|
|
}
|