-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
367 lines (305 loc) Β· 9.56 KB
/
Copy pathcli.py
File metadata and controls
367 lines (305 loc) Β· 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
FCube CLI - Main CLI Application
The main Typer application for FCube - a FastAPI project and module generator
that follows the industry-standard architecture patterns.
Commands:
- startproject: Create a new FastAPI project with core module (no user by default)
- adduser: Add user module with configurable authentication (email, phone, both)
- addplugin: Add pre-built plugin modules (referral, notifications, etc.)
- startmodule: Create a new module with complete folder structure
- addentity: Add a new entity to an existing module
- listmodules: List all existing modules
- version: Show CLI version
"""
import typer
from rich.console import Console
from typing import Optional
from .commands.startproject import startproject_command
from .commands.startmodule import startmodule_command
from .commands.addentity import addentity_command
from .commands.adduser import adduser_command, AuthType
from .commands.addplugin import addplugin_command
from .commands.listmodules import listmodules_command
# Initialize Typer app
app = typer.Typer(
name="fcube",
help="π§ FCube CLI - Modern FastAPI Project & Module Generator",
add_completion=True,
rich_markup_mode="rich",
)
console = Console()
@app.command("startproject")
def startproject(
project_name: str = typer.Argument(..., help="Name of the project to create"),
directory: str = typer.Option(
".",
"--dir",
"-d",
help="Directory where the project will be created"
),
with_celery: bool = typer.Option(
True,
"--celery/--no-celery",
help="Include Celery for background tasks"
),
with_docker: bool = typer.Option(
True,
"--docker/--no-docker",
help="Include Docker configuration"
),
force: bool = typer.Option(
False,
"--force",
"-f",
help="Overwrite existing files"
),
):
"""
π Create a new FastAPI project with complete infrastructure.
This command generates a production-ready FastAPI project including:
β’ Core module (database, settings, logging, exceptions)
β’ Docker & docker-compose configuration
β’ Alembic migrations setup
β’ Celery for background tasks
β’ Redis for caching
β’ PostgreSQL database
NOTE: User module is NOT created by default.
Use 'fcube adduser' to add authentication.
[bold cyan]Examples:[/bold cyan]
$ python fcube.py startproject MyProject
$ python fcube.py startproject api-backend --dir projects
$ python fcube.py startproject simple-api --no-celery --no-docker
"""
startproject_command(
project_name=project_name,
directory=directory,
with_celery=with_celery,
with_docker=with_docker,
force=force
)
@app.command("adduser")
def adduser(
directory: str = typer.Option(
"app",
"--dir",
"-d",
help="Directory where the user module will be created"
),
auth_type: AuthType = typer.Option(
AuthType.EMAIL,
"--auth-type",
"-a",
help="Authentication type: email, phone, or both"
),
force: bool = typer.Option(
False,
"--force",
"-f",
help="Overwrite existing files"
),
):
"""
π€ Add user module with configurable authentication.
Creates a complete user module with authentication system.
Choose from different authentication strategies:
β’ email: Email + Password with JWT tokens
β’ phone: Phone + OTP with SMS verification
β’ both: Combined email and phone authentication
[bold cyan]Examples:[/bold cyan]
$ python fcube.py adduser --auth-type email
$ python fcube.py adduser --auth-type phone
$ python fcube.py adduser --auth-type both
$ python fcube.py adduser -a email --force
"""
adduser_command(
directory=directory,
auth_type=auth_type,
force=force
)
@app.command("addplugin")
def addplugin(
plugin_name: str = typer.Argument(
None,
help="Name of the plugin to add (e.g., referral)"
),
directory: str = typer.Option(
"app",
"--dir",
"-d",
help="Directory where the plugin will be created"
),
force: bool = typer.Option(
False,
"--force",
"-f",
help="Overwrite existing files"
),
list_plugins: bool = typer.Option(
False,
"--list",
"-l",
help="List all available plugins"
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Preview files without creating them"
),
):
"""
π Add a pre-built plugin module to your project.
Plugins are self-contained feature modules that can be added
to any FCube-generated project. Available plugins:
β’ referral: User referral system with completion strategies
β’ (more coming soon...)
[bold cyan]Examples:[/bold cyan]
$ python fcube.py addplugin --list
$ python fcube.py addplugin referral
$ python fcube.py addplugin referral --dry-run
$ python fcube.py addplugin referral --force
"""
addplugin_command(
plugin_name=plugin_name,
directory=directory,
force=force,
list_plugins=list_plugins,
dry_run=dry_run
)
@app.command("startmodule")
def startmodule(
module_name: str = typer.Argument(..., help="Name of the module to create"),
directory: str = typer.Option(
"app",
"--dir",
"-d",
help="Directory where the module will be created"
),
with_admin: bool = typer.Option(
True,
"--admin/--no-admin",
help="Include admin routes with permissions"
),
with_public: bool = typer.Option(
True,
"--public/--no-public",
help="Include public routes (no auth required)"
),
force: bool = typer.Option(
False,
"--force",
"-f",
help="Overwrite existing files"
),
):
"""
Create a new modular FastAPI module with complete folder structure.
This command generates a production-ready module following the Korab Backend
architecture patterns including:
β’ Layered Architecture (Models β Schemas β CRUD β Services β Routes)
β’ Dependency Injection with singleton services
β’ Role-based route organization (public/, admin/)
β’ Permission-based access control
β’ HTTPException-based error handling
β’ Transaction management (Service layer owns commits)
β’ Integration facades for cross-module communication
[bold cyan]Examples:[/bold cyan]
$ python fcube.py startmodule product
$ python fcube.py startmodule inventory --dir app
$ python fcube.py startmodule order --no-admin --force
"""
startmodule_command(
module_name=module_name,
directory=directory,
with_admin=with_admin,
with_public=with_public,
force=force
)
@app.command("addentity")
def addentity(
module_name: str = typer.Argument(..., help="Name of the existing module"),
entity_name: str = typer.Argument(..., help="Name of the entity to add"),
directory: str = typer.Option(
"app",
"--dir",
"-d",
help="Directory where modules are located"
),
force: bool = typer.Option(
False,
"--force",
"-f",
help="Overwrite existing files"
),
):
"""
Add a new entity to an existing module.
Creates model, schema, and CRUD files for a new entity within
an existing module.
[bold cyan]Examples:[/bold cyan]
$ python fcube.py addentity service_provider availability
$ python fcube.py addentity booking payment --force
"""
addentity_command(
module_name=module_name,
entity_name=entity_name,
directory=directory,
force=force
)
@app.command("listmodules")
def listmodules(
directory: str = typer.Option(
"app",
"--dir",
"-d",
help="Directory to scan for modules"
),
):
"""
List all existing modular FastAPI modules.
Scans the app directory and displays all modules that follow
the modular structure pattern with their components.
[bold cyan]Examples:[/bold cyan]
$ python fcube.py listmodules
$ python fcube.py listmodules --dir app
"""
listmodules_command(directory)
@app.command("version")
def version():
"""how FCube CLI version."""
from . import __version__
console.print(
f"[bold cyan] FCube CLI[/bold cyan] version [green]{__version__}[/green]"
)
@app.callback(invoke_without_command=True)
def main(
ctx: typer.Context,
version: Optional[bool] = typer.Option(
None,
"--version",
"-v",
help="Show version and exit",
is_flag=True,
),
):
"""
FCube CLI - Modern FastAPI Module Generator
A powerful code generation tool for creating production-ready
FastAPI modules following clean architecture principles.
[bold]Architecture Features:[/bold]
β’ Layered Design (Models β CRUD β Services β Routes)
β’ Dependency Injection with @lru_cache singletons
β’ Role-based routes (public/, admin/)
β’ Permission-based access control
β’ Integration facades for cross-module communication
"""
if version:
from . import __version__
console.print(
f"[bold cyan] FCube CLI[/bold cyan] version [green]{__version__}[/green]"
)
raise typer.Exit()
if ctx.invoked_subcommand is None:
console.print(ctx.get_help())
# Entry point for running the CLI
if __name__ == "__main__":
app()