diff --git a/update_local_appstore.sh b/update_local_appstore.sh index 4a97bb235..356fd1684 100644 --- a/update_local_appstore.sh +++ b/update_local_appstore.sh @@ -54,6 +54,15 @@ if $USE_ZH; then MSG_NOTE_UNKNOWN="注意: 未知参数 %s 被忽略" MSG_WARN_APP_MISSING="警告: 应用 '%s' 在仓库中不存在" MSG_TRY_CLONE="正在尝试克隆 %s" + MSG_ERR_CLONE_SOURCE_REQUIRE="错误: --clone-source 参数需要指定源名称" + MSG_ERR_UNKNOWN_SOURCE="错误: 未知的源 '%s'" + MSG_DRY_RUN_MODE="试运行模式已启用,不会执行任何实际更改" + MSG_DRY_RUN_URL_HEADER="将按以下顺序克隆 URL:" + MSG_DRY_RUN_URL="将克隆: %s" + MSG_DRY_RUN_TARGET_DIR_EXISTS="目标目录 %s 存在" + MSG_DRY_RUN_TARGET_DIR_NOT_EXISTS="目标目录 %s 不存在" + MSG_DRY_RUN_COPY="将复制应用 '%s' 到 %s" + MSG_DRY_RUN_COPY_ALL="将复制所有应用到 %s" else MSG_INTRO=" ########################################### # Note # @@ -85,10 +94,22 @@ else MSG_NOTE_UNKNOWN="Note: Unknown parameter %s ignored" MSG_WARN_APP_MISSING="WARNING: App '%s' does not exist in repository" MSG_TRY_CLONE="Trying to clone %s" + MSG_ERR_CLONE_SOURCE_REQUIRE="Error: --clone-source parameter requires a source name" + MSG_ERR_UNKNOWN_SOURCE="Error: Unknown source '%s'" + MSG_DRY_RUN_MODE="Dry run mode enabled, no changes will be made" + MSG_DRY_RUN_URL_HEADER="URLs to be cloned (in order):" + MSG_DRY_RUN_URL="Would clone: %s" + MSG_DRY_RUN_TARGET_DIR_EXISTS="Target directory %s exists" + MSG_DRY_RUN_TARGET_DIR_NOT_EXISTS="Target directory %s does not exist" + MSG_DRY_RUN_COPY="Would copy app '%s' to %s" + MSG_DRY_RUN_COPY_ALL="Would copy all apps to %s" fi apps_to_copy=() custom_base_dir="" +clone_sources=() +dry_run=false + while [[ $# -gt 0 ]]; do case $1 in --app) @@ -117,6 +138,19 @@ while [[ $# -gt 0 ]]; do exit 1 fi ;; + --clone-source) + if [[ -n "$2" && "$2" != --* ]]; then + clone_sources+=("$2") + shift 2 + else + echo "$MSG_ERR_CLONE_SOURCE_REQUIRE" + exit 1 + fi + ;; + --dry-run) + dry_run=true + shift + ;; *) printf "$MSG_NOTE_UNKNOWN\n" "$1" shift @@ -148,33 +182,135 @@ repo_prefixs=( 'https://ghproxy.net/https://github.com' ) -independent_repos=( - 'https://codeberg.org/pooneyy/1Panel-Appstore.git' - 'https://code.forgejo.org/pooneyy/1Panel-Appstore.git' - 'https://gitea.com/pooneyy/1Panel-Appstore.git' +declare -A mirror_sites=( + [codeberg]='https://codeberg.org/pooneyy/1Panel-Appstore.git' + [forgejo]='https://code.forgejo.org/pooneyy/1Panel-Appstore.git' + [gitea]='https://gitea.com/pooneyy/1Panel-Appstore.git' ) +mirror_names=("${!mirror_sites[@]}") repo_suffix="/pooneyy/1Panel-Appstore.git" + +if [ ${#clone_sources[@]} -eq 0 ]; then + clone_sources=("all") +fi + +declare -A source_map +unique_sources=() +for src in "${clone_sources[@]}"; do + if [[ -z "${source_map[$src]}" ]]; then + source_map[$src]=1 + unique_sources+=("$src") + fi +done + +if [[ " ${unique_sources[@]} " =~ " all " ]]; then + final_sources=("all") +else + has_mirror=false + for src in "${unique_sources[@]}"; do + if [[ "$src" == "mirror" ]]; then + has_mirror=true + break + fi + done + + if $has_mirror; then + final_sources=() + for src in "${unique_sources[@]}"; do + if [[ "$src" == "mirror" ]] || [[ "$src" == "github" ]]; then + final_sources+=("$src") + else + if [[ -n "${mirror_sites[$src]}" ]]; then + : + else + printf "$MSG_ERR_UNKNOWN_SOURCE\n" "$src" + exit 1 + fi + fi + done + else + final_sources=() + for src in "${unique_sources[@]}"; do + if [[ "$src" == "github" ]] || [[ "$src" == "mirror" ]]; then + final_sources+=("$src") + elif [[ -n "${mirror_sites[$src]}" ]]; then + final_sources+=("$src") + else + printf "$MSG_ERR_UNKNOWN_SOURCE\n" "$src" + exit 1 + fi + done + fi +fi + all_urls=() -for prefix in "${repo_prefixs[@]}"; do - all_urls+=("${prefix}${repo_suffix}") -done +shuffle_array() { + local arr=("$@") + local len=${#arr[@]} + for ((i=0; i