Coverage for src/kwai/cli/commands/identity.py: 0%
31 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Identity contains all subcommands for managing identity in kwai.
3Note:
4 Make sure the environment variable KWAI_SETTINGS_FILE is set!
6"""
8import os
10from asyncio import run
12import inject
13import typer
15from rich import print
16from typer import Typer
18from kwai.core.db.database import Database
19from kwai.core.db.uow import UnitOfWork
20from kwai.core.domain.exceptions import UnprocessableException
21from kwai.core.settings import ENV_SETTINGS_FILE
22from kwai.modules.identity.create_user import CreateUser, CreateUserCommand
23from kwai.modules.identity.users.user_account_db_repository import (
24 UserAccountDbRepository,
25)
28def check():
29 """Check if the environment variable is set. If not, stop the cli."""
30 if ENV_SETTINGS_FILE not in os.environ:
31 print(
32 f"[bold red]Please set env variable {ENV_SETTINGS_FILE} to "
33 f"the configuration file.[/bold red]"
34 )
35 raise typer.Exit(code=1) from None
38app = Typer(pretty_exceptions_short=True, callback=check)
41@app.command(help="Create a user account.")
42def create(
43 email: str = typer.Option(
44 ..., help="The email address of the new user", prompt=True
45 ),
46 first_name: str = typer.Option(
47 ..., help="The first name of the new user", prompt=True
48 ),
49 last_name: str = typer.Option(
50 ..., help="The last name of the new user", prompt=True
51 ),
52 password: str = typer.Option(
53 ..., prompt=True, confirmation_prompt=True, hide_input=True
54 ),
55):
56 """Create a user account.
58 Use this command to create a new user account (for the root user for example).
60 Args:
61 email: The email address of the new user
62 first_name: The firstname of the new user
63 last_name: The lastname of the new user
64 password: The password of the new user
65 """
67 @inject.autoparams()
68 async def _main(database: Database):
69 """Closure for handling the async code."""
70 command = CreateUserCommand(
71 email=email,
72 first_name=first_name,
73 last_name=last_name,
74 password=password,
75 remark="This user was created using the CLI",
76 )
77 async with UnitOfWork(database):
78 try:
79 await CreateUser(UserAccountDbRepository(database)).execute(command)
80 print(
81 f"[bold green]Success![/bold green] "
82 f"User created with email address {email}"
83 )
84 except UnprocessableException as ex:
85 print("[bold red]Failed![/bold red] User could not created:")
86 print(ex)
87 raise typer.Exit(code=1) from None
89 run(_main())