ad-user-creator/run.sh

118 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PYTHON_BIN="python3"
CONFIG_PATH="config/config.yaml"
INPUT_FILE=""
DRY_RUN="false"
CONTINUE_ON_ERROR="true"
COMMAND=""
usage() {
cat <<'EOF'
Usage:
./run.sh ui
./run.sh
./run.sh -f users.csv
./run.sh -f users.xlsx
./run.sh -f users.csv --dry-run
./run.sh -f users.xlsx --continue-on-error false
./run.sh -c config/config.yaml
Commands:
ui Start the Web UI in the background on port 9999
Options:
-f <file> Batch input file (.csv/.xlsx). If omitted, interactive mode is used.
-c <path> YAML config path (default: config/config.yaml)
--dry-run Dry run mode
--continue-on-error true|false Batch only, default true
-h, --help Show this help
EOF
}
# 检查是否为独立的 ui 命令
if [[ "${1:-}" == "ui" ]]; then
COMMAND="ui"
shift
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-f)
INPUT_FILE="${2:-}"
shift 2
;;
-c)
CONFIG_PATH="${2:-}"
shift 2
;;
--dry-run)
DRY_RUN="true"
shift
;;
--continue-on-error)
CONTINUE_ON_ERROR="${2:-}"
if [[ "${CONTINUE_ON_ERROR}" != "true" && "${CONTINUE_ON_ERROR}" != "false" ]]; then
echo "错误: --continue-on-error 仅支持 true 或 false" >&2
exit 2
fi
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "未知参数: $1" >&2
usage
exit 2
;;
esac
done
if [[ "${COMMAND}" == "ui" ]]; then
mkdir -p state
LOG_FILE="state/web.log"
nohup "${PYTHON_BIN}" -m ad_user_creator.main --config "${CONFIG_PATH}" web --port 9999 > "${LOG_FILE}" 2>&1 &
echo "UI 服务已在后台启动,监听 http://0.0.0.0:9999"
echo "PID: $!"
echo "日志: ${LOG_FILE}"
exit 0
fi
if [[ -z "${INPUT_FILE}" ]]; then
CMD=("${PYTHON_BIN}" -m ad_user_creator.main --config "${CONFIG_PATH}" interactive)
if [[ "${DRY_RUN}" == "true" ]]; then
CMD+=(--dry-run)
fi
exec "${CMD[@]}"
fi
if [[ ! -f "${INPUT_FILE}" ]]; then
echo "错误: 输入文件不存在: ${INPUT_FILE}" >&2
exit 2
fi
case "${INPUT_FILE##*.}" in
csv|CSV|xlsx|XLSX)
;;
*)
echo "错误: 仅支持 .csv 或 .xlsx 文件: ${INPUT_FILE}" >&2
exit 2
;;
esac
CMD=(
"${PYTHON_BIN}" -m ad_user_creator.main
--config "${CONFIG_PATH}"
batch
--input "${INPUT_FILE}"
--continue-on-error "${CONTINUE_ON_ERROR}"
)
if [[ "${DRY_RUN}" == "true" ]]; then
CMD+=(--dry-run)
fi
exec "${CMD[@]}"