4
4
#
5
5
# @author bnbong bbbong9@gmail.com
6
6
# --------------------------------------------------------------------------
7
- import re
8
7
import os
9
8
import click
10
9
11
- from typing import Union , Any
10
+ from typing import Union
12
11
13
12
from logging import getLogger
14
13
18
17
from rich .panel import Panel
19
18
20
19
from . import __version__
20
+ from .backend import validate_email , inject_project_metadata , read_template_stack
21
21
from fastapi_fastkit .utils .logging import setup_logging
22
22
from fastapi_fastkit .core .settings import FastkitConfig
23
- from fastapi_fastkit .core .exceptions import CLIExceptions , TemplateExceptions
23
+ from fastapi_fastkit .core .exceptions import CLIExceptions
24
24
from fastapi_fastkit .utils .transducer import copy_and_convert_template
25
25
from fastapi_fastkit .utils .inspector import delete_project
26
26
27
27
28
28
logger = getLogger (__name__ )
29
29
30
30
31
- # --------------------------------------------------------------------------
32
- # Backend operators
33
- # --------------------------------------------------------------------------
34
- REGEX = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
35
-
36
-
37
- def validate_email (ctx : Context , param : Any , value : Any ) -> Any :
38
- """
39
- Check if the provided email is in a valid format.
40
- This will recursively loop until a valid email input entry is given.
41
-
42
- :param ctx: context of passing configurations (NOT specify it at CLI)
43
- :type ctx: <Object click.Context>
44
- :param param: parameters from CLI
45
- :param value: values from CLI
46
- :return:
47
- """
48
- try :
49
- if not re .match (REGEX , value ):
50
- raise ValueError (value )
51
- else :
52
- return value
53
- except ValueError as e :
54
- click .echo ("Incorrect email address given: {}" .format (e ))
55
- value = click .prompt (param .prompt )
56
- return validate_email (ctx , param , value )
57
-
58
-
59
- def _inject_project_metadata (
60
- target_dir : str ,
61
- project_name : str ,
62
- author : str ,
63
- author_email : str ,
64
- description : str ,
65
- ) -> None :
66
- """
67
- Inject metadata into the main.py and setup.py files.
68
-
69
- :param target_dir: Directory for the new project to deploy
70
- :param project_name: new project name
71
- :param author: cli username
72
- :param author_email: cli user email
73
- :param description: new project description
74
- """
75
- main_py_path = os .path .join (target_dir , "main.py" )
76
- setup_py_path = os .path .join (target_dir , "setup.py" )
77
-
78
- try :
79
- with open (main_py_path , "r+" ) as f :
80
- content = f .read ()
81
- content = content .replace ("app_title" , f'"{ project_name } "' )
82
- content = content .replace ("app_description" , f'"{ description } "' )
83
- f .seek (0 )
84
- f .write (content )
85
- f .truncate ()
86
-
87
- with open (setup_py_path , "r+" ) as f :
88
- content = f .read ()
89
- content = content .replace ("<project_name>" , project_name , 1 )
90
- content = content .replace ("<description>" , description , 1 )
91
- content = content .replace ("<author>" , author , 1 )
92
- content = content .replace ("<author_email>" , author_email , 1 )
93
- f .seek (0 )
94
- f .write (content )
95
- f .truncate ()
96
- except Exception as e :
97
- click .echo (e )
98
- raise TemplateExceptions ("ERROR : Having some errors with injecting metadata" )
99
-
100
-
101
- # --------------------------------------------------------------------------
102
- # Click operator methods
103
- # --------------------------------------------------------------------------
104
31
@click .group ()
105
32
@click .option ("--debug/--no-debug" , default = False )
106
33
@click .version_option (__version__ , prog_name = "fastapi-fastkit" )
@@ -232,6 +159,7 @@ def startup(
232
159
click .echo (f"Author: { author } " )
233
160
click .echo (f"Author Email: { author_email } " )
234
161
click .echo (f"Description: { description } " )
162
+ read_template_stack ()
235
163
# click.echo("Project Stack: [FastAPI, Uvicorn, SQLAlchemy, Docker (optional)]") # TODO : impl this?
236
164
237
165
confirm = click .confirm (
@@ -249,7 +177,7 @@ def startup(
249
177
250
178
copy_and_convert_template (target_template , user_local , project_name )
251
179
252
- _inject_project_metadata (
180
+ inject_project_metadata (
253
181
project_dir , project_name , author , author_email , description
254
182
)
255
183
0 commit comments