from __future__ import annotations import sys from pathlib import Path from ad_user_creator.main import execute_command def _ask_yes_no(prompt: str, default: bool) -> bool: default_text = "Y/n" if default else "y/N" value = input(f"{prompt} [{default_text}]: ").strip().lower() if not value: return default return value in {"y", "yes", "1", "true"} def _clean_path(path_text: str) -> str: text = path_text.strip() if len(text) >= 2 and ((text[0] == "'" and text[-1] == "'") or (text[0] == '"' and text[-1] == '"')): text = text[1:-1] return text def _run_interactive(config_path: str) -> int: dry_run = _ask_yes_no("是否启用 dry-run(不写入 LDAP)", default=False) return execute_command(command="interactive", config_path=config_path, dry_run=dry_run) def _run_batch(config_path: str) -> int: input_path = _clean_path(input("请输入批量文件路径 (.csv/.xlsx): ")) if not input_path: print("未提供文件路径。") return 1 suffix = Path(input_path).suffix.lower() if suffix not in {".csv", ".xlsx"}: print(f"文件格式错误,仅支持 .csv 或 .xlsx: {input_path}") return 1 dry_run = _ask_yes_no("是否启用 dry-run(不写入 LDAP)", default=False) continue_on_error = _ask_yes_no("遇到错误是否继续处理后续记录", default=True) return execute_command( command="batch", config_path=config_path, dry_run=dry_run, input_path=input_path, continue_on_error=continue_on_error, ) def menu() -> int: config_path = "config/config.yaml" while True: print("\n=== AD User Creator ===") print(f"当前配置文件: {config_path}") print("1) 交互式创建") print("2) 批量导入") print("3) 修改配置文件路径") print("q) 退出") choice = input("请选择模式: ").strip().lower() if choice == "1": code = _run_interactive(config_path) print(f"执行完成,返回码: {code}") elif choice == "2": code = _run_batch(config_path) print(f"执行完成,返回码: {code}") elif choice == "3": new_path = _clean_path(input("请输入新的 config.yaml 路径: ")) if not new_path: print("配置路径未变更。") else: config_path = new_path print(f"已更新配置路径: {config_path}") elif choice in {"q", "quit", "exit"}: print("已退出。") return 0 else: print("无效选项,请输入 1/2/3/q。") if __name__ == "__main__": sys.exit(menu())