30 lines
882 B
Python
30 lines
882 B
Python
"""Create the first local API user without exposing a public registration endpoint."""
|
|
|
|
import argparse
|
|
import getpass
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from app.db.repositories.repositories import UserRepository
|
|
from app.db.session import SessionLocal
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="创建本地 API 用户")
|
|
parser.add_argument("username")
|
|
args = parser.parse_args()
|
|
password = getpass.getpass("Password: ")
|
|
if len(password) < 8:
|
|
parser.error("密码至少需要 8 个字符")
|
|
with SessionLocal() as db:
|
|
try:
|
|
UserRepository(db).create(username=args.username, password=password, is_admin=True)
|
|
except IntegrityError:
|
|
db.rollback()
|
|
parser.error("用户名已存在")
|
|
print(f"Created user: {args.username}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|