Fully support reset

[GitHub link #14]
This commit is contained in:
Aoran Zeng 2024-06-07 22:28:06 +08:00
parent e4c71aa56d
commit b3b7b4c355
2 changed files with 48 additions and 40 deletions

View File

@ -3,7 +3,7 @@
* License : GPLv3
* Authors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-08-29>
* Last modified : <2024-06-05>
* Last modified : <2024-06-07>
*
* chsrc:
*
@ -21,9 +21,9 @@
#define chsrc_error(str) xy_error(xy_2strjoin(App_Prefix, (str)))
int Cli_Option_IPv6 = 0;
int Cli_Optiion_Locally = 0;
int Cli_Option_InEnglish = 0;
bool Cli_Option_IPv6 = false;
bool Cli_Option_Locally = false;
bool Cli_Option_InEnglish = false;
/**
*
@ -156,7 +156,7 @@ test_speed_url (const char *url)
char *ipv6 = ""; // 默认不启用
if (Cli_Option_IPv6==1) {
if (Cli_Option_IPv6==true) {
ipv6 = "--ipv6";
}
@ -418,11 +418,17 @@ chsrc_backup (const char *path)
/* Target Info */
typedef struct {
void (*setfn)(char *option);
void (*getfn)(char *option);
typedef struct TargetInfo_t {
void (*getfn) (char *option);
void (*setfn) (char *option);
void (*resetfn) (char *option);
SourceInfo *sources;
size_t sources_n;
} TargetInfo;
#define def_target(t) TargetInfo t##_target = {t##_setsrc, t##_getsrc, t##_sources, t##_sources_n}
// 大部分target还不支持reset所以暂时先默认设置为NULL来过渡
#define def_target(t) TargetInfo t##_target = {t##_getsrc, t##_setsrc, NULL, t##_sources, t##_sources_n}
#define def_target_full(t) TargetInfo t##_target = {t##_getsrc, t##_setsrc, t##_resetsrc, t##_sources, t##_sources_n}
#define def_target_noget(t) TargetInfo t##_target = {NULL, t##_setsrc, NULL, t##_sources, t##_sources_n}

View File

@ -1778,6 +1778,7 @@ wr_winget_getsrc (char *option)
chsrc_run ("winget source list");
}
/* 参考https://mirrors.ustc.edu.cn/help/winget-source.html */
void
wr_winget_setsrc (char *option)
{
@ -1990,11 +1991,9 @@ def_target(pl_ruby); def_target(pl_python); def_target(pl_nodejs); def_target
def_target(pl_lua);
def_target(pl_rust); def_target(pl_go); def_target(pl_java); def_target(pl_dart); def_target(pl_ocaml);
def_target(pl_r); def_target(pl_julia);
TargetInfo
pl_clojure_target = {pl_clojure_setsrc, NULL, pl_clojure_sources, pl_clojure_sources_n},
pl_dotnet_target = {pl_dotnet_setsrc, NULL, pl_dotnet_sources, pl_dotnet_sources_n},
pl_haskell_target = {pl_haskell_setsrc, NULL, pl_haskell_sources, pl_haskell_sources_n};
def_target_noget (pl_clojure);
def_target_noget (pl_dotnet);
def_target_noget (pl_haskell);
#define t(a) (const char*)(a)
@ -2029,20 +2028,17 @@ def_target(os_alpine); def_target(os_void); def_target(os_trisquel); def_target(
def_target(os_netbsd); def_target(os_openbsd);
def_target(os_deepin); def_target(os_openkylin);
def_target(os_raspberrypi);
TargetInfo
os_fedora_target = {os_fedora_setsrc, NULL, os_fedora_sources, os_fedora_sources_n},
os_opensuse_target = {os_opensuse_setsrc, NULL, os_opensuse_sources, os_opensuse_sources_n},
os_msys2_target = {os_msys2_setsrc, NULL, os_msys2_sources, os_msys2_sources_n},
os_arch_target = {os_arch_setsrc, NULL, os_arch_sources, os_arch_sources_n},
os_manjaro_target = {os_manjaro_setsrc, NULL, NULL, 0},
os_gentoo_target = {os_gentoo_setsrc, NULL, os_gentoo_sources, os_gentoo_sources_n},
os_rocky_target = {os_rocky_setsrc, NULL, os_rocky_sources, os_rocky_sources_n},
os_solus_target = {os_solus_setsrc, NULL, os_solus_sources, os_solus_sources_n},
os_freebsd_target = {os_freebsd_setsrc, NULL, os_freebsd_sources, os_freebsd_sources_n},
os_openeuler_target= {os_openeuler_setsrc, NULL, os_openeuler_sources, os_openeuler_sources_n},
os_ros_target = {os_ros_setsrc, NULL, os_ros_sources, os_ros_sources_n};
def_target_noget(os_fedora);
def_target_noget(os_opensuse);
def_target_noget(os_arch);
def_target_noget(os_gentoo);
def_target_noget(os_rocky);
def_target_noget(os_solus);
def_target_noget(os_freebsd);
def_target_noget(os_openeuler);
def_target_noget(os_ros);
def_target_noget(os_msys2);
TargetInfo os_manjaro_target = {NULL, os_manjaro_setsrc, NULL, NULL, 0};
static const char
*os_ubuntu [] = {"ubuntu", NULL, t(&os_ubuntu_target)},
@ -2082,13 +2078,14 @@ static const char
};
def_target(wr_winget); def_target(wr_brew); def_target(wr_tex);
TargetInfo
wr_flathub_target = {wr_flathub_setsrc, NULL, wr_flathub_sources, wr_flathub_sources_n},
wr_nix_target = {wr_nix_setsrc, NULL, wr_nix_sources, wr_nix_sources_n},
wr_guix_target = {wr_guix_setsrc, NULL, wr_guix_sources, wr_guix_sources_n},
wr_emacs_target = {wr_emacs_setsrc, NULL, wr_emacs_sources, wr_emacs_sources_n},
wr_anaconda_target = {wr_anaconda_setsrc, NULL, wr_anaconda_sources, wr_anaconda_sources_n};
def_target_full(wr_winget);
def_target(wr_brew);
def_target_noget (wr_flathub);
def_target_noget (wr_nix);
def_target_noget (wr_guix);
def_target_noget (wr_emacs);
def_target_noget (wr_anaconda);
def_target(wr_tex);
static const char
*wr_winget [] = {"winget", NULL, t(&wr_winget_target)},
@ -2287,8 +2284,9 @@ iterate_targets_ (const char ***array, size_t size, const char *input, const cha
#define iterate_targets(ary, input, target) iterate_targets_(ary, xy_arylen(ary), input, target)
typedef enum {
TargetOp_Set_Source = 1,
TargetOp_Get_Source,
TargetOp_Get_Source = 1,
TargetOp_Set_Source,
TargetOp_Reset_Source,
TargetOp_Cesu_Source,
TargetOp_List_Source
} TargetOp;
@ -2322,6 +2320,12 @@ get_target (const char *input, TargetOp code, char *option)
if (target->setfn) target->setfn(option);
else xy_error (xy_strjoin (3, "chsrc: 暂未对 ", input, " 实现set功能欢迎贡献"));
}
else if (TargetOp_Reset_Source==code)
{
if (target->setfn) target->resetfn(option);
else xy_error (xy_strjoin (3, "chsrc: 暂未对 ", input, " 实现reset功能欢迎贡献"));
// puts ("将重置并恢复上游默认使用的源");
}
else if (TargetOp_Get_Source==code)
{
if (target->getfn) target->getfn("");
@ -2531,10 +2535,8 @@ main (int argc, char const *argv[])
return 1;
}
puts ("将重置并恢复上游默认使用的源");
target = argv[cli_arg_Target_pos];
char *mirror = "upstream";
matched = get_target (target, TargetOp_Set_Source, mirror);
matched = get_target (target, TargetOp_Reset_Source, NULL);
if (!matched) goto not_matched;
return 0;
}