Skip to content

Commit 0b03dc8

Browse files
author
Jeff Panasuik
authored
Create sys_preload_cli.py
1 parent 768ac37 commit 0b03dc8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

etc/sys_preload_cli.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import click
3+
import subprocess
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
@click.group()
9+
def cli():
10+
"""Django CLI Tool"""
11+
pass
12+
13+
def get_project_root():
14+
project_root = os.getenv('PROJECT_ROOT')
15+
if not project_root:
16+
project_root = click.prompt("Enter the project root directory").strip()
17+
if not os.path.isdir(project_root):
18+
click.echo(f"The provided directory '{project_root}' is not valid.")
19+
return None
20+
with open('.env', 'a') as env_file:
21+
env_file.write(f"\nPROJECT_ROOT={project_root}")
22+
os.environ['PROJECT_ROOT'] = project_root
23+
click.echo(f"PROJECT_ROOT set to {project_root}")
24+
else:
25+
click.echo(f"PROJECT_ROOT is already set to {project_root}")
26+
return project_root
27+
28+
@cli.command()
29+
@click.option('--app-name', prompt='Enter the app name', help='The app name for your Django project.')
30+
def create_app(app_name):
31+
"""Create a new Django app."""
32+
project_root = get_project_root()
33+
if project_root:
34+
try:
35+
subprocess.run(["python", os.path.join(project_root, "manage.py"), "startapp", app_name], check=True)
36+
click.echo(f"App '{app_name}' created successfully.")
37+
except Exception as e:
38+
click.echo(f"Error creating app '{app_name}': {e}")
39+
40+
@cli.command()
41+
def check_project():
42+
"""Check for project root directory and prompt user if not set."""
43+
get_project_root()
44+
45+
if __name__ == "__main__":
46+
cli()

0 commit comments

Comments
 (0)