Files
tools/common/remote_loader.sh
Wang Defa 1be07db276 fix: 避免 remote_loader.sh 中重复定义 readonly 变量
## 问题描述

当脚本使用远程加载时,出现警告:
```
/tmp/tmp.xxx: line 10: REMOTE_BASE_URL: readonly variable
```

## 根本原因

`REMOTE_BASE_URL` 被重复定义为 readonly 变量:

1. 调用脚本中定义(如 oci/create_instance.sh:16):
   ```bash
   readonly REMOTE_BASE_URL="..."
   ```

2. remote_loader.sh:10 中再次定义:
   ```bash
   readonly REMOTE_BASE_URL="..."
   ```

Bash 不允许重新赋值 readonly 变量,即使值相同也会产生警告。

## 修复方案

在 remote_loader.sh 中添加检查,只有在变量未定义时才设置:

```bash
# 旧代码
readonly REMOTE_BASE_URL="..."

# 新代码
if [[ -z "${REMOTE_BASE_URL:-}" ]]; then
    readonly REMOTE_BASE_URL="..."
fi
```

## 影响

-  消除警告信息
-  保持向后兼容
-  允许调用者自定义 REMOTE_BASE_URL
-  如果未定义则使用默认值
2025-12-26 15:23:48 +08:00

108 lines
3.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ============================================================================
# 文件名: remote_loader.sh
# 描述: 远程公共库加载器 - 从 Gitea 仓库下载并加载公共库
# 作者: Cloud Tools Project
# 版本: 1.0.0
# ============================================================================
# 远程仓库 URL 配置(如果调用者未定义,则使用默认值)
if [[ -z "${REMOTE_BASE_URL:-}" ]]; then
readonly REMOTE_BASE_URL="${REMOTE_LIB_URL:-https://gitea.bcde.io/wangdefa/tools/raw/branch/main}"
fi
# 临时目录用于存储下载的文件
readonly REMOTE_TMP_DIR="$(mktemp -d)"
# 清理函数 - 在脚本退出时删除临时文件
cleanup_remote_libs() {
if [[ -d "$REMOTE_TMP_DIR" ]]; then
rm -rf "$REMOTE_TMP_DIR"
fi
}
# 注册退出时的清理函数
trap cleanup_remote_libs EXIT INT TERM
#
# 从远程下载单个文件
#
# 参数:
# $1 - 相对路径例如common/logging.sh
#
# 返回:
# 下载文件的本地路径
#
download_remote_file() {
local relative_path="$1"
local remote_url="${REMOTE_BASE_URL}/${relative_path}"
local local_file="${REMOTE_TMP_DIR}/$(basename "$relative_path")"
# 尝试使用 curl 下载
if command -v curl &>/dev/null; then
if curl -fsSL "$remote_url" -o "$local_file" 2>/dev/null; then
echo "$local_file"
return 0
fi
# 尝试使用 wget 下载
elif command -v wget &>/dev/null; then
if wget -q "$remote_url" -O "$local_file" 2>/dev/null; then
echo "$local_file"
return 0
fi
else
echo "[ERROR] 未找到 curl 或 wget 命令,无法下载远程库" >&2
return 1
fi
echo "[ERROR] 无法下载远程库: $remote_url" >&2
return 1
}
#
# 加载远程公共库
#
# 此函数会下载并 source 所有必需的公共库
#
load_remote_libs() {
echo "[INFO] 正在从远程仓库加载公共库..."
echo "[INFO] 仓库地址: $REMOTE_BASE_URL"
# 必需的库文件列表
local required_libs=(
"common/logging.sh"
"common/error_handler.sh"
)
# 下载并加载每个库文件
for lib in "${required_libs[@]}"; do
echo "[INFO] 下载 $lib ..."
local local_path
if local_path=$(download_remote_file "$lib"); then
echo "[SUCCESS] 已下载: $lib"
# 加载库文件
# shellcheck disable=SC1090
if source "$local_path"; then
echo "[SUCCESS] 已加载: $lib"
else
echo "[ERROR] 无法加载远程库: $lib" >&2
echo "[ERROR] 脚本执行中止" >&2
exit 1
fi
else
echo "[ERROR] 无法下载远程库: $lib" >&2
echo "[ERROR] 脚本执行中止" >&2
echo "[ERROR] 请检查网络连接或仓库 URL: $REMOTE_BASE_URL" >&2
exit 1
fi
done
echo "[SUCCESS] 所有远程库加载完成"
echo ""
}
# 执行加载
load_remote_libs