diff --git a/README.md b/README.md index b6a4493fb52e96104dd718ae85d17b1f3e321c58..c4f8d767d2446836eb69bab1f476bc8497c014d8 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Generator features: - Different databases to choose from. - Alembic integration; - redis support; -- CI\CD (Currently only gitlab-ci); +- different CI\CD templates; - Kubernetes config. This project can handle arguments passed through command line. @@ -52,19 +52,21 @@ $ python -m fastapi_template --help usage: FastAPI template [-h] [--name PROJECT_NAME] [--description PROJECT_DESCRIPTION] - [--db {none,sqlite,mysql,postgresql}] [--ci] [--redis] - [--alembic] [--kube] [--force] + [--db {DatabaseType.none,DatabaseType.sqlite,DatabaseType.mysql,DatabaseType.postgresql}] + [--ci {CIType.none,CIType.gitlab_ci,CIType.github}] + [--redis] [--alembic] [--kube] [--force] optional arguments: -h, --help show this help message and exit --name PROJECT_NAME Name of your awesome project --description PROJECT_DESCRIPTION Project description - --db {none,sqlite,mysql,postgresql} + --db {DatabaseType.none,DatabaseType.sqlite,DatabaseType.mysql,DatabaseType.postgresql} Database - --ci Add CI/CD support + --ci {CIType.none,CIType.gitlab_ci,CIType.github} + Choose CI support --redis Add redis support --alembic Add alembic support --kube Add kubernetes configs - --force Owerrite directory if exists + --force Owerrite directory if it exists ``` diff --git a/fastapi_template/cli.py b/fastapi_template/cli.py index fc9054da8bf4144dbe12815fca665ccb100e519e..a5d3b461c4e3404a299ef37b8a913e54bfb77098 100644 --- a/fastapi_template/cli.py +++ b/fastapi_template/cli.py @@ -7,7 +7,7 @@ from prompt_toolkit.document import Document from prompt_toolkit.shortcuts import checkboxlist_dialog, radiolist_dialog from prompt_toolkit.validation import ValidationError, Validator -from fastapi_template.input_model import BuilderContext, Database, DatabaseType +from fastapi_template.input_model import BuilderContext, Database, DatabaseType, CIType class SnakeCaseValidator(Validator): @@ -68,10 +68,11 @@ def parse_args(): ) parser.add_argument( "--ci", - help="Add CI/CD support", - action="store_true", + help="Choose CI support", default=None, - dest="enable_ci", + type=CIType, + choices=list(CIType), + dest="ci_type", ) parser.add_argument( "--redis", @@ -111,10 +112,6 @@ def ask_features(current_context: BuilderContext) -> BuilderContext: "name": "enable_redis", "value": current_context.enable_redis, }, - "CI/CD": { - "name": "enable_ci", - "value": current_context.enable_ci, - }, "Kubernetes": { "name": "enable_kube", "value": current_context.enable_kube, @@ -160,6 +157,14 @@ def read_user_input(current_context: BuilderContext) -> BuilderContext: raise KeyboardInterrupt() if current_context.db == DatabaseType.none: current_context.enable_alembic = False + if current_context.ci_type is None: + current_context.ci_type = radiolist_dialog( + "CI", + text="Which CI/CD do you want?", + values=[(ci, ci.value) for ci in list(CIType)], + ).run() + if current_context.ci_type is None: + raise KeyboardInterrupt() ask_features(current_context) return current_context diff --git a/fastapi_template/input_model.py b/fastapi_template/input_model.py index 1d2eb939e1982b9b4e31c7259bf155788f8fc52c..1c7f3bbf7bc3b89e395b50ba39d7f7854a46cd3f 100644 --- a/fastapi_template/input_model.py +++ b/fastapi_template/input_model.py @@ -16,6 +16,7 @@ class DatabaseType(enum.Enum): class CIType(enum.Enum): none = "none" gitlab_ci = "gitlab" + github = "github" class Database(BaseModel): @@ -33,7 +34,7 @@ class BuilderContext(BaseModel): db: Optional[DatabaseType] db_info: Optional[Database] enable_redis: Optional[bool] - enable_ci: Optional[bool] + ci_type: Optional[CIType] enable_alembic: Optional[bool] enable_kube: Optional[bool] force: bool = False diff --git a/fastapi_template/template/cookiecutter.json b/fastapi_template/template/cookiecutter.json index 469ca15f85729dbc84fc17cc94c8370f39c8a8e5..e974c3d0d02864e7ab4458aef948bb720a62a8a7 100644 --- a/fastapi_template/template/cookiecutter.json +++ b/fastapi_template/template/cookiecutter.json @@ -11,8 +11,8 @@ "enable_redis": { "type": "bool" }, - "enable_ci": { - "type": "bool" + "ci_type": { + "type": "string" }, "enable_alembic": { "type": "bool" diff --git a/fastapi_template/template/hooks/post_gen_project.py b/fastapi_template/template/hooks/post_gen_project.py index a9bf5aa9b14c380a66c4bf8fdf0facde460c65fa..c53643f18c5ea4ae9a6ad05da8011268cae59d83 100644 --- a/fastapi_template/template/hooks/post_gen_project.py +++ b/fastapi_template/template/hooks/post_gen_project.py @@ -26,6 +26,7 @@ def delete_resources_for_disabled_features(): with open(MANIFEST) as manifest_file: manifest = json.load(manifest_file) for feature_name, feature in manifest.items(): + print(feature_name, feature) if feature['enabled'].lower() != "true": text = "{} resources for disabled feature {}...".format( colored("Removing", color="red"), diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/.github/workflows/pre-commit.yml b/fastapi_template/template/{{cookiecutter.project_name}}/.github/workflows/pre-commit.yml new file mode 100644 index 0000000000000000000000000000000000000000..9bdc910d235587a96b3f961df4751b8d9b73f537 --- /dev/null +++ b/fastapi_template/template/{{cookiecutter.project_name}}/.github/workflows/pre-commit.yml @@ -0,0 +1,19 @@ +name: Testing {{cookiecutter.project_name}} + +on: push + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + - name: Install deps + uses: knowsuchagency/poetry-install@v1 + env: + POETRY_VIRTUALENVS_CREATE: false + - name: Run pre-commit check + run: poetry run pre-commit run -a diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json b/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json index 3f5ca14c053073534a73410a9a50c411b837bf42..eac8acc160561cfe817dee286176a469caca37af 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json +++ b/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json @@ -30,10 +30,16 @@ "{{cookiecutter.project_name}}/db/migrations" ] }, - "CI support": { - "enabled": "{{cookiecutter.enable_ci}}", + "Gitlab CI": { + "enabled": "{{cookiecutter.ci_type == 'CIType.gitlab_ci'}}", "resources": [ ".gitlab-ci.yml" ] + }, + "Github CI": { + "enabled": "{{cookiecutter.ci_type == 'CIType.github'}}", + "resources": [ + ".github" + ] } } diff --git a/pyproject.toml b/pyproject.toml index 03035b1d4277a0bd4220e240e20f603410465fdc..2e1d98dc908ebb1d645bbc072ffd0f21eba5adef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastapi_template" -version = "2.0.0" +version = "2.1.0" description = "Feature-rich robust FastAPI template" authors = ["Pavel Kirilin <win10@list.ru>"] packages = [