|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import re |
| 4 | +import time |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import typer |
| 8 | +from rich import print |
| 9 | + |
| 10 | +from fastapi_ccli import __version__ |
| 11 | +from fastapi_ccli.cloner import GREEN, RED, github_fba_src, github_ftm_src, github_fsm_src, gitee_fba_src, gitee_ftm_src |
| 12 | +from fastapi_ccli.utils.get_country import get_current_country |
| 13 | +from fastapi_ccli.utils.get_ip import get_net_ip |
| 14 | +from fastapi_ccli.utils.get_path import get_project_path |
| 15 | + |
| 16 | +app_en = typer.Typer(rich_markup_mode="rich") |
| 17 | + |
| 18 | + |
| 19 | +def is_china(dns: bool) -> str: |
| 20 | + """ |
| 21 | + Whether to use dns |
| 22 | +
|
| 23 | + :param dns: |
| 24 | + :return: |
| 25 | + """ |
| 26 | + with typer.progressbar(range(5), label="Analyzing") as progress: |
| 27 | + for i in progress: |
| 28 | + ip = get_net_ip() |
| 29 | + if ip: |
| 30 | + progress.update(5) |
| 31 | + break |
| 32 | + else: |
| 33 | + time.sleep(0.3) |
| 34 | + progress.update(i) |
| 35 | + continue |
| 36 | + rp = get_current_country(ip) |
| 37 | + if "CN" in rp: |
| 38 | + ending = GREEN if dns else RED |
| 39 | + else: |
| 40 | + ending = RED if dns else GREEN |
| 41 | + return ending |
| 42 | + |
| 43 | + |
| 44 | +def exec_clone(orm: str, country: str, project: str, path: str) -> None: |
| 45 | + """ |
| 46 | + Perform clone |
| 47 | +
|
| 48 | + :param orm: |
| 49 | + :param country: |
| 50 | + :param project: |
| 51 | + :param path: |
| 52 | + :return: |
| 53 | + """ |
| 54 | + typer.echo("Project name: " + typer.style(project, fg="blue", bold=True)) |
| 55 | + typer.echo("Select orm: " + orm) |
| 56 | + if orm == "sqlalchemy": |
| 57 | + source = github_fba_src if "True" in country else gitee_fba_src |
| 58 | + elif orm == "tortoise": |
| 59 | + source = github_ftm_src if "True" in country else gitee_ftm_src |
| 60 | + elif orm == "sqlmodel": |
| 61 | + source = github_fsm_src |
| 62 | + try: |
| 63 | + print(f"⏳ Start clone {source.split('/')[-1].split('.')[0]} project...") # noqa |
| 64 | + # out = os.system(f"git clone {source} {path}") |
| 65 | + # if out != 0: |
| 66 | + # raise RuntimeError(out) |
| 67 | + print(orm, country, project, path) |
| 68 | + except Exception as e: |
| 69 | + print(f"❌ Clone project failed: {e}") |
| 70 | + raise typer.Exit(1) |
| 71 | + else: |
| 72 | + print("✅ The project was cloned successfully") |
| 73 | + typer.echo(f"Please go to the directory {typer.style(path, fg='green', bold=True)} to view") |
| 74 | + |
| 75 | + |
| 76 | +@app_en.command(epilog="Made by :beating_heart: wu-clan") |
| 77 | +def cloner( |
| 78 | + version: Optional[bool] = typer.Option( |
| 79 | + None, |
| 80 | + "--version", |
| 81 | + "-V", |
| 82 | + help="Print version information.", |
| 83 | + ), |
| 84 | + orm: Optional[str] = typer.Option( |
| 85 | + "sqlalchemy", |
| 86 | + "--orm", |
| 87 | + "-o", |
| 88 | + metavar="<ORM>", |
| 89 | + help="Select the orm to use, the default is sqlalchemy, support 'sqlalchemy' / 'tortoise' / 'sqlmodel'.", |
| 90 | + ), |
| 91 | + project_path: Optional[str] = typer.Option( |
| 92 | + None, |
| 93 | + "--path", |
| 94 | + "-p", |
| 95 | + metavar="<PATH>", |
| 96 | + show_default=False, |
| 97 | + help="Project clone path, the default is '../fastapi_project', supports absolute path or relative path.", |
| 98 | + ), |
| 99 | +): |
| 100 | + """ |
| 101 | + FastAPI project cloner |
| 102 | + """ |
| 103 | + if version: |
| 104 | + typer.secho("\n🔥 FastAPI CCLI " + __version__, fg="green", bold=True) |
| 105 | + if orm: |
| 106 | + if orm not in ["sqlalchemy", "tortoise", "sqlmodel"]: |
| 107 | + raise typer.BadParameter("Enter unknown parameters, only allowed 'sqlalchemy' / 'tortoise' / 'sqlmodel'") |
| 108 | + if project_path: |
| 109 | + if not isinstance(project_path, str): |
| 110 | + raise typer.BadParameter("Wrong parameter input, please enter the correct path") |
| 111 | + use_project_name = project_path or "../fastapi_project" |
| 112 | + path = get_project_path(use_project_name) |
| 113 | + project_name = re.split(r"/|\'|\\|\\\\", use_project_name)[-1] |
| 114 | + _country = typer.confirm("Is your region China?", default=False) |
| 115 | + country = is_china(_country) |
| 116 | + exec_clone(orm, country, project_name, path) |
0 commit comments