コンテンツにスキップ

スクリプトや設定ファイルからプロジェクトを作成する

スクリプトや CI ジョブのようにターミナルがない環境では、fastkit init は質問をせずに実行されます。また、プロジェクトの選択内容をファイルに保存し、そのファイルから同じプロジェクトをもう一度作成することもできます。

ターミナルがない場合

ターミナルがないと、init は何も質問しません。プロジェクト名は必須で、指定しないと実行を拒否します。

$ fastkit init
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ Missing --project-name: there is no terminal to ask for it. Pass      │
│ --project-name NAME or --config FILE.                                    │
╰──────────────────────────────────────────────────────────────────────────╯

--project-name と必要なオプションを渡してください。それ以外の選択はすべて既定値になります。標準入力にパイプで渡した答えは読み込まれません。ターミナルがある場合は、-y で同じ動作になります。オプションで答えていない質問はすべて既定値のままにし、何も尋ねずにプロジェクトを作成します。

設定を保存する

--save-config PATH は、実行時の選択内容をファイルに書き出します。--dry-run を除くすべての init の実行で使えます。

$ fastkit init --project-name shop --level domain --database SQLite --feature authentication=JWT --no-venv --save-config shop.toml
...
╭──────────────────── Success ─────────────────────╮
│ ✨ Saved project configuration to 'shop.toml'    │
╰──────────────────────────────────────────────────╯
...
$ cat shop.toml
async_tasks = "None"
authentication = "JWT"
author = ""
author_email = ""
caching = "None"
custom_packages = []
database = "SQLite"
deployment = []
description = ""
level = "domain"
logging = "None"
migrations = "None"
monitoring = "None"
package_manager = "uv"
project_name = "shop"
testing = "Coverage"
tooling = []
utilities = []

ファイル形式は拡張子で決まります。.json と .toml は常に使え、.yaml は PyYAML がインストールされている場合に使えます。必須のキーは project_name だけです。1 つのファイルには、level と template のどちらか一方を指定します。

設定ファイルからプロジェクトを作成する

--config PATH は、何も質問せずにファイルの内容からプロジェクトを作成します。--project-name、--author、--author-email、--description を指定すると、ファイル内の値より優先されます。

$ fastkit init --config shop.toml --project-name shop2 --no-venv
╭─────────────────────── Info ────────────────────────╮
│ ℹ Loaded project configuration from 'shop.toml'     │
╰─────────────────────────────────────────────────────╯

...
│ Project name    │ shop2                                                      │
...
│ Database        │ SQLite                                                     │
│ Features        │ Authentication: JWT; Testing: Coverage                     │
...

プロジェクトのバージョンを設定する

設定ファイルでは、1.2.0 のような PEP 440 形式のバージョンを version に指定できます(YAML では引用符で囲んでください)。生成されるプロジェクトは、0.1.0 ではなくそのバージョンから始まります。次の例の release.toml は、shop.toml に version = "1.2.0" の行を加えたものです。

$ fastkit init --config release.toml --project-name shop3 --no-venv
...
$ head -4 shop3/pyproject.toml
[project]
name = "shop3"
version = "1.2.0"
description = "[FastAPI-fastkit templated]"

値が不正な場合はエラー(Config key 'version' must be a PEP 440 version such as 1.0.0, got 'one')が表示され、何も書き込まれません。

fastkit 1.x で保存した設定ファイル

fastkit 1.x で書き出したファイルも引き続き使えます。minimal プリセット(またはプリセットの指定なし)からは fastapi-single-module のプロジェクトが作成されます。また、現在はすべてのプロジェクトに含まれている CORS と Pagination のユーティリティを指定しても受け付けられます。"architecture_preset": "minimal" と "utilities": ["CORS", "Pagination"] を含む 1.x のファイル old.json の場合は、次のようになります。

$ fastkit init --config old.json --no-venv --dry-run
...
│ Template        │ fastapi-single-module                                      │
│ Database        │ None                                                       │
│ Features        │ Utilities: CORS, Pagination                                │
...

CI ジョブの例

次のジョブは一例であり、fastkit に同梱されているファイルではありません。コミット済みの設定ファイルからプロジェクトを作成し、そのテストを実行します。

name: generate
on: workflow_dispatch
jobs:
  generate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v6
      - run: uv tool install fastapi-fastkit
      - run: fastkit init --config shop.toml --no-venv
      - run: cd shop && uv sync && uv run pytest