4
4
#
5
5
# @author bnbong bbbong9@gmail.com
6
6
# --------------------------------------------------------------------------
7
+ import re
7
8
import os
8
9
import click
9
10
29
30
# --------------------------------------------------------------------------
30
31
# Backend operators
31
32
# --------------------------------------------------------------------------
33
+ REGEX = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
34
+
35
+
36
+ def validate_email (ctx , param , value ):
37
+ """
38
+ Check if the provided email is in a valid format.
39
+ This will recursively loop until a valid email input entry is given.
40
+
41
+ :param ctx: context of passing configurations (NOT specify it at CLI)
42
+ :type ctx: <Object click.Context>
43
+ :param param: parameters from CLI
44
+ :param value: values from CLI
45
+ :return:
46
+ """
47
+ try :
48
+ if not re .match (REGEX , value ):
49
+ raise ValueError (value )
50
+ else :
51
+ return value
52
+ except ValueError as e :
53
+ click .echo ("Incorrect email address given: {}" .format (e ))
54
+ value = click .prompt (param .prompt )
55
+ return validate_email (ctx , param , value )
56
+
57
+
32
58
def _inject_project_metadata (
33
59
target_dir : str ,
34
60
project_name : str ,
@@ -71,11 +97,27 @@ def _inject_project_metadata(
71
97
raise TemplateExceptions ("ERROR : Having some errors with injecting metadata" )
72
98
73
99
100
+ def _print_version (ctx : Context , value : click .Option , * args , ** kwargs ) -> None :
101
+ # TODO : apply this at fastapi-cli group
102
+ """
103
+ print current version of fastapi-fastkit
104
+
105
+ :param ctx: context of passing configurations (NOT specify it at CLI)
106
+ :type ctx: <Object click.Context>
107
+ """
108
+ if value :
109
+ version_info = f"fastapi-fastkit version { __version__ } "
110
+ click .echo (print (version_info ))
111
+ ctx .exit ()
112
+
113
+
74
114
# --------------------------------------------------------------------------
75
115
# Click operator methods
76
116
# --------------------------------------------------------------------------
77
117
@click .group ()
78
118
@click .option ("--debug/--no-debug" , default = False )
119
+ # @click.option('--version', callback=_print_version,
120
+ # expose_value=False)
79
121
@click .pass_context
80
122
def fastkit_cli (ctx : Context , debug : bool ) -> Union ["BaseCommand" , None ]:
81
123
"""
@@ -138,7 +180,7 @@ def echo(ctx: Context) -> None:
138
180
139
181
140
182
@fastkit_cli .command (context_settings = {"ignore_unknown_options" : True })
141
- @click .argument ("template" )
183
+ @click .argument ("template" , default = "fastapi-default" )
142
184
@click .option (
143
185
"--project-name" ,
144
186
prompt = "Enter the project name" ,
@@ -151,6 +193,8 @@ def echo(ctx: Context) -> None:
151
193
"--author-email" ,
152
194
prompt = "Enter the author email" ,
153
195
help = "The email of the project author." ,
196
+ type = str ,
197
+ callback = validate_email ,
154
198
)
155
199
@click .option (
156
200
"--description" ,
@@ -166,6 +210,7 @@ def startproject(
166
210
author_email : str ,
167
211
description : str ,
168
212
) -> None :
213
+ # TODO : add a feature - name project folder name to project_name
169
214
"""
170
215
Create a new FastAPI project from templates and inject metadata.
171
216
@@ -179,22 +224,36 @@ def startproject(
179
224
settings = ctx .obj ["settings" ]
180
225
181
226
template_dir = settings .FASTKIT_TEMPLATE_ROOT
227
+ click .echo (f"Deploying FastAPI project using '{ template } ' template" )
182
228
target_template = os .path .join (template_dir , template )
183
229
print (f"Template path: { target_template } " )
184
230
185
231
if not os .path .exists (target_template ):
186
232
raise CLIExceptions (
187
233
f"Error: Template '{ template } ' does not exist in '{ template_dir } '."
188
234
)
235
+ # TODO : add confirm step : checking template stack & name & metadata, confirm it y/n
236
+ # click.echo("Project Stack: [FastAPI, Uvicorn, SQLAlchemy, Docker (optional)]")
237
+
238
+ confirm = click .confirm (
239
+ "\n Do you want to proceed with project creation?" , default = False
240
+ )
241
+ if not confirm :
242
+ click .echo ("Project creation aborted!" )
243
+ return
189
244
190
245
try :
191
246
user_local = settings .USER_WORKSPACE
192
247
click .echo (f"FastAPI template project will deploy at '{ user_local } '" )
193
248
194
249
click .echo (f"Project Name: { project_name } " )
195
250
196
- # TODO : add confirm step : checking template stack & name & metadata, confirm it y/n
197
- # click.echo("Project Stack: [FastAPI, Uvicorn, SQLAlchemy, Docker (optional)]")
251
+ confirm = click .confirm (
252
+ "\n Do you want to proceed with project creation?" , default = False
253
+ )
254
+ if not confirm :
255
+ click .echo ("Project creation aborted!" )
256
+ return
198
257
199
258
copy_and_convert_template (target_template , user_local )
200
259
0 commit comments