mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-17 20:57:28 +08:00
Rewrite Docker Hub
recipe using rawstr4c
This commit is contained in:
parent
686c0c0891
commit
87997aeeb6
@ -134,7 +134,7 @@
|
||||
#include "recipe/ware/Nix.c"
|
||||
#include "recipe/ware/Guix.c"
|
||||
#include "recipe/ware/Flathub.c"
|
||||
#include "recipe/ware/Docker-Hub.c"
|
||||
#include "recipe/ware/Docker-Hub/Docker-Hub.c"
|
||||
#include "recipe/ware/Anaconda/Anaconda.c"
|
||||
|
||||
#include "recipe/menu.c"
|
||||
|
@ -6,9 +6,12 @@
|
||||
* Contributors : Nil Null <nil@null.org>
|
||||
* |
|
||||
* Created On : <2024-06-08>
|
||||
* Major Revision : 2
|
||||
* Last Modified : <2025-07-14>
|
||||
* ------------------------------------------------------------*/
|
||||
|
||||
#include "rawstr4c.h"
|
||||
|
||||
static MirrorSite_t DaoCloud =
|
||||
{
|
||||
IS_DedicatedMirrorSite,
|
||||
@ -41,14 +44,14 @@ static Source_t wr_dockerhub_sources[] =
|
||||
|
||||
def_sources_n(wr_dockerhub);
|
||||
|
||||
#define WARE_DockerHub_SourceConfig "/etc/docker/daemon.json"
|
||||
#define WR_DockerHub_ConfigFile "/etc/docker/daemon.json"
|
||||
|
||||
void
|
||||
wr_dockerhub_getsrc (char *option)
|
||||
{
|
||||
if (xy_on_linux || xy_on_bsd)
|
||||
{
|
||||
chsrc_view_file (WARE_DockerHub_SourceConfig);
|
||||
chsrc_view_file (WR_DockerHub_ConfigFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -70,62 +73,53 @@ wr_dockerhub_setsrc (char *option)
|
||||
|
||||
if (xy_on_linux || xy_on_bsd)
|
||||
{
|
||||
char *to_add = xy_strjoin (3, "{\n"
|
||||
" \"registry-mirrors\": [\"", source.url, "\"]\n"
|
||||
"}");
|
||||
if (chsrc_check_file (WARE_DockerHub_SourceConfig))
|
||||
char *to_add = xy_str_gsub (RAWSTR_wr_dockerhub_insert_content, "@1@", source.url);
|
||||
|
||||
if (chsrc_check_file (WR_DockerHub_ConfigFile))
|
||||
{
|
||||
chsrc_note2 ("已找到Docker配置文件,将自动换源");
|
||||
chsrc_backup (WARE_DockerHub_SourceConfig);
|
||||
chsrc_backup (WR_DockerHub_ConfigFile);
|
||||
|
||||
if (chsrc_check_program_quietly ("jq"))
|
||||
{
|
||||
// 检查是否已经存在 source.url
|
||||
char *cmd = xy_strjoin (4, "jq '.[\"registry-mirrors\"] | index(\"",
|
||||
source.url,
|
||||
"\")' ",
|
||||
WARE_DockerHub_SourceConfig);
|
||||
char *ret = xy_run(cmd, 0);
|
||||
if (ret && !xy_streql(ret, "null"))
|
||||
/* 检查是否已经存在 source.url */
|
||||
char *cmd = xy_str_gsub (RAWSTR_wr_dockerhub_check_cmd, "@1@", source.url);
|
||||
cmd = xy_str_gsub (cmd, "@2@", WR_DockerHub_ConfigFile);
|
||||
|
||||
char *result = xy_run (cmd, 0);
|
||||
if (result && !xy_streql (result, "null"))
|
||||
{
|
||||
chsrc_note2 ("已存在源,无需重复添加");
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd = xy_strjoin (6, "jq '.[\"registry-mirrors\"] |= [\"",
|
||||
source.url,
|
||||
"\"] + .' ",
|
||||
xy_2strjoin(WARE_DockerHub_SourceConfig, ".bak"),
|
||||
" > ",
|
||||
WARE_DockerHub_SourceConfig);
|
||||
cmd = xy_str_gsub (RAWSTR_wr_dockerhub_insert_cmd, "@1@", source.url);
|
||||
cmd = xy_str_gsub (cmd, "@2@", WR_DockerHub_ConfigFile);
|
||||
chsrc_run (cmd, RunOpt_Default);
|
||||
chsrc_note2 ("源已添加");
|
||||
chsrc_succ2 ("源已添加");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
chsrc_note2("未找到 jq 命令, 将使用 sed 换源");
|
||||
char *cmd = xy_strjoin (5, "sed ",
|
||||
"-z -i 's|\"registry-mirrors\":[^]]*]|\"registry-mirrors\":[\"",
|
||||
source.url,
|
||||
"\"]|' ",
|
||||
WARE_DockerHub_SourceConfig);
|
||||
chsrc_note2 ("未找到 jq 命令, 将使用 sed 换源");
|
||||
char *cmd = xy_str_gsub (RAWSTR_wr_dockerhub_sed_command, "@1@", source.url);
|
||||
cmd = xy_str_gsub (cmd, "@2@", WR_DockerHub_ConfigFile);
|
||||
chsrc_run (cmd, RunOpt_Default);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 不存在 /etc/docker/daemon.json 时可以直接写入文件
|
||||
/* 不存在 /etc/docker/daemon.json 时可以直接写入文件 */
|
||||
chsrc_note2 ("未找到Docker配置文件, 将自动创建");
|
||||
chsrc_ensure_dir ("/etc/docker");
|
||||
chsrc_run ( xy_2strjoin("touch ", WARE_DockerHub_SourceConfig), RunOpt_Default);
|
||||
chsrc_run ( xy_2strjoin ("touch ", WR_DockerHub_ConfigFile), RunOpt_Default);
|
||||
|
||||
chsrc_append_to_file (to_add, WARE_DockerHub_SourceConfig);
|
||||
chsrc_append_to_file (to_add, WR_DockerHub_ConfigFile);
|
||||
}
|
||||
// chsrc_note2 ("请向 /etc/docker/daemon.json 中添加下述内容:");
|
||||
// println (to_add);
|
||||
|
||||
if (xy_on_linux)
|
||||
{
|
||||
// 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启
|
||||
/* 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启 */
|
||||
chsrc_note2 ("请自行运行: sudo systemctl restart docker");
|
||||
chsrc_note2 ("该命令会重启所有容器, 请在合适的时机执行");
|
||||
}
|
||||
@ -136,8 +130,8 @@ wr_dockerhub_setsrc (char *option)
|
||||
}
|
||||
else
|
||||
{
|
||||
chsrc_note2 ("请打开Docker Desktop设置");
|
||||
chsrc_note2 ("选择“Docker Engine”选项卡,在该选项卡中找到“registry-mirrors”一栏,添加镜像地址:");
|
||||
chsrc_note2 ("请打开 Docker Desktop 设置");
|
||||
chsrc_note2 ("选择 'Docker Engine' 选项卡,在该选项卡中找到 'registry-mirrors' 一栏,添加镜像地址:");
|
||||
println (source.url);
|
||||
}
|
||||
|
||||
|
16
src/recipe/ware/Docker-Hub/rawstr4c.h
Normal file
16
src/recipe/ware/Docker-Hub/rawstr4c.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* Generated by rawstr4c v0.1.0.1-2025/07/14
|
||||
*
|
||||
* Date: 2025-07-14T16:15:47.328642+08:00
|
||||
*/
|
||||
|
||||
char RAWSTR_wr_dockerhub_insert_content[] = "\x7b\x0a\x20\x20\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x20\x5b\x22\x40\x31\x40\x22\x5d\x0a\x7d\x0a";
|
||||
|
||||
char RAWSTR_wr_dockerhub_check_cmd[] = "\x6a\x71\x20\x27\x2e\x5b\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x5d\x20\x7c\x20\x69\x6e\x64\x65\x78\x28\x22\x40\x31\x40\x22\x29\x27\x20\x40\x32\x40\x0a";
|
||||
|
||||
char RAWSTR_wr_dockerhub_insert_cmd[] = "\x6a\x71\x20\x27\x2e\x5b\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x5d\x20\x7c\x3d\x20\x5b\x22\x40\x31\x40\x22\x5d\x20\x2b\x20\x2e\x27\x20\x40\x32\x40\x2e\x62\x61\x6b\x20\x3e\x20\x20\x40\x32\x40\x0a";
|
||||
|
||||
char RAWSTR_wr_dockerhub_sed_command[] = "\x73\x65\x64\x20\x2d\x7a\x20\x2d\x69\x20\x27\x73\x2f\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x5b\x5e\x5d\x5d\x2a\x5d\x2f\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x5b\x22\x40\x31\x40\x22\x5d\x2f\x27\x20\x40\x32\x40\x0a";
|
||||
|
67
src/recipe/ware/Docker-Hub/rawstr4c.md
Normal file
67
src/recipe/ware/Docker-Hub/rawstr4c.md
Normal file
@ -0,0 +1,67 @@
|
||||
<!-- -----------------------------------------------------------
|
||||
! SPDX-License-Identifier: GPL-3.0-or-later
|
||||
! -------------------------------------------------------------
|
||||
! Config Type : rawstr4c (Markdown)
|
||||
! Config Authors: Aoran Zeng <ccmywish@qq.com>
|
||||
! | happy game <happygame1024@gmail.com>
|
||||
! Contributors : Nil Null <nil@null.org>
|
||||
! Created On : <2025-07-14>
|
||||
! Last Modified : <2025-07-14>
|
||||
! ---------------------------------------------------------- -->
|
||||
|
||||
# rawstr4c input
|
||||
|
||||
- prefix = `RAWSTR_wr_dockerhub`
|
||||
- output = `:global-variable-only-header`
|
||||
- translate = `:hex`
|
||||
- keep-postfix = `false`
|
||||
|
||||
|
||||
|
||||
## auto insert content
|
||||
|
||||
- name = `insert_content`
|
||||
|
||||
```json
|
||||
{
|
||||
"registry-mirrors": ["@1@"]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## jq command1
|
||||
|
||||
检查配置文件中是否已存在即将要换的源
|
||||
|
||||
- name = `check_cmd`
|
||||
|
||||
```sh
|
||||
jq '.["registry-mirrors"] | index("@1@")' @2@
|
||||
```
|
||||
|
||||
|
||||
|
||||
## jq command2
|
||||
|
||||
插入新的源到配置文件中
|
||||
|
||||
- name = `insert_cmd`
|
||||
|
||||
```sh
|
||||
jq '.["registry-mirrors"] |= ["@1@"] + .' @2@.bak > @2@
|
||||
```
|
||||
|
||||
注释: `|=` 为赋值, `+ .` 表示把原数组加过来
|
||||
|
||||
|
||||
|
||||
## sed command
|
||||
|
||||
没有 `jq` 时,用 `sed` 换源,写入到配置文件中
|
||||
|
||||
```sh
|
||||
sed -z -i 's/"registry-mirrors":[^]]*]/"registry-mirrors":["@1@"]/' @2@
|
||||
```
|
||||
|
||||
注释: `[^]]*` 即不是 `]` 的所有字符, 后面再跟一个 `]` 由于没有前面的 `[`,因此可被解析为普通字符 `]`,没有特殊含义
|
Loading…
x
Reference in New Issue
Block a user