ad-user-creator/run.sh

97 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PYTHON_BIN="/opt/homebrew/Caskroom/miniconda/base/bin/python"
CONFIG_PATH="config/config.yaml"
INPUT_FILE=""
DRY_RUN="false"
CONTINUE_ON_ERROR="true"
usage() {
cat <<'EOF'
Usage:
./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
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
}
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 [[ -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[@]}"