Coverage for kwai/cli/identity.py: 0%

28 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Identity contains all subcommands for managing identity in kwai. 

2 

3Note: 

4 Make sure the environment variable KWAI_SETTINGS_FILE is set! 

5 

6""" 

7import os 

8from asyncio import run 

9 

10import typer 

11from rich import print 

12from typer import Typer 

13 

14from kwai.core.db.database import Database 

15from kwai.core.dependencies import container 

16from kwai.core.domain.exceptions import UnprocessableException 

17from kwai.core.settings import ENV_SETTINGS_FILE 

18from kwai.modules.identity.create_user import CreateUser, CreateUserCommand 

19from kwai.modules.identity.users.user_account_db_repository import ( 

20 UserAccountDbRepository, 

21) 

22 

23 

24def check(): 

25 """Check if the environment variable is set. If not, stop the cli.""" 

26 if ENV_SETTINGS_FILE not in os.environ: 

27 print( 

28 f"[bold red]Please set env variable {ENV_SETTINGS_FILE} to " 

29 f"the configuration file.[/bold red]" 

30 ) 

31 raise typer.Exit(code=1) from None 

32 

33 

34app = Typer(pretty_exceptions_short=True, callback=check) 

35 

36 

37@app.command(help="Create a user account.") 

38def create( 

39 email: str = typer.Option( 

40 ..., help="The email address of the new user", prompt=True 

41 ), 

42 first_name: str = typer.Option( 

43 ..., help="The first name of the new user", prompt=True 

44 ), 

45 last_name: str = typer.Option( 

46 ..., help="The last name of the new user", prompt=True 

47 ), 

48 password: str = typer.Option( 

49 ..., prompt=True, confirmation_prompt=True, hide_input=True 

50 ), 

51): 

52 """Create a user account. 

53 

54 Use this command to create a new user account (for the root user for example). 

55 

56 Args: 

57 email: The email address of the new user 

58 first_name: The firstname of the new user 

59 last_name: The lastname of the new user 

60 password: The password of the new user 

61 """ 

62 

63 async def _main(): 

64 """Closure for handling the async code.""" 

65 command = CreateUserCommand( 

66 email=email, 

67 first_name=first_name, 

68 last_name=last_name, 

69 password=password, 

70 remark="This user was created using the CLI", 

71 ) 

72 try: 

73 await CreateUser(UserAccountDbRepository(container[Database])).execute( 

74 command 

75 ) 

76 print( 

77 f"[bold green]Success![/bold green] " 

78 f"User created with email address {email}" 

79 ) 

80 except UnprocessableException as ex: 

81 print("[bold red]Failed![/bold red] User could not created:") 

82 print(ex) 

83 raise typer.Exit(code=1) from None 

84 

85 run(_main())