Metadata-Version: 2.4
Name: fastpluggy-cli
Version: 1.0.0
Summary: Scaffold FastPluggy projects and plugins, validate plugins and run a dev server
Author-email: FastPluggy <contact@fastpluggy.xyz>
License-Expression: MIT
Project-URL: Homepage, https://fastpluggy.xyz
Project-URL: Source, https://github.com/fastpluggy/cli
Project-URL: Issues, https://github.com/fastpluggy/cli/issues
Project-URL: Changelog, https://github.com/fastpluggy/cli/blob/main/CHANGELOG.md
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Code Generators
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click<9,>=8.5
Requires-Dist: jinja2
Requires-Dist: questionary
Requires-Dist: uvicorn[standard]
Requires-Dist: fastpluggy>=0.4.102
Requires-Dist: fastapi
Provides-Extra: e2e
Requires-Dist: playwright; extra == "e2e"
Provides-Extra: dev
Requires-Dist: pytest==9.2.0; extra == "dev"
Requires-Dist: pytest-cov==7.1.0; extra == "dev"
Requires-Dist: httpx; extra == "dev"
Provides-Extra: ssl
Requires-Dist: cryptography==50.0.2; extra == "ssl"
Dynamic: license-file

# FastPluggy CLI

A CLI tool to scaffold projects, plugins, and auto-fix compliance issues for the [FastPluggy](https://fastpluggy.xyz) framework.

## Installation

```bash
pip install fastpluggy-cli
fastpluggy --version
```

From a checkout, for development: `pip install -e ".[dev]"`.

## Available Commands

### Create a new project

```bash
fastpluggy new [my_app]
```

Generates a FastPluggy project skeleton in the `my_app` directory. If no name is provided, the CLI prompts interactively.

The generated project holds three files:
- `main.py` — a FastAPI app that starts FastPluggy (plugins are discovered through entry points)
- `.env` — `PORT` and a commented `DATABASE_URL`
- `pyproject.toml` — depends on `fastapi`, `uvicorn` and `fastpluggy`

### Create a new plugin

```bash
fastpluggy new-plugin [plugin_name]
```

Scaffolds an installable plugin that passes `fastpluggy validate`:

```
my_plugin/
├── pyproject.toml          # package fastpluggy_plugin.my_plugin mapped onto src/
├── src/
│   ├── __init__.py
│   ├── plugin.py           # the FastPluggyBaseModule class (entry point)
│   ├── router.py           # one page, served under /plugin-pkg/my_plugin/
│   └── templates/index.html.j2
├── tests/test_plugin.py
├── e2e/routes.json         # pages to screenshot in CI
├── docs/screenshots/e2e/
├── .github/workflows/ci.yml  # or .gitlab-ci.yml, see --ci
├── .gitignore
└── README.md
```

Options:
- `--label`, `--icon` — menu label and icon
- `--menu-type main|admin|no` — menu the plugin appears in (`no` hides it)
- `--with-config` — `src/config.py`, settings editable in the admin UI and stored in the database
- `--with-model` — `src/models.py` skeleton
- `--with-cli-commands` — `src/cli.py`, a `fastpluggy <plugin-name>` command group
- `--ci github|gitlab|none` — the CI file (asked interactively, `github` by default):
  - `github`: `.github/workflows/ci.yml`, a self-contained GitHub Actions workflow that runs the tests;
  - `gitlab`: `.gitlab-ci.yml` extending the FastPluggy shared GitLab CI templates (tests and e2e
    screenshots). It only runs on the GitLab instance that hosts those templates;
  - `none`: no CI file. `fastpluggy validate` then reports rule R5 until you add one.
- `--output-dir`, `--no-interactive`, `--dry-run`

Then install it and start the server:

```bash
pip install -e "./my_plugin[dev]"
fastpluggy start-dev-server
```

The generated page, as rendered by the CI of a freshly scaffolded plugin:

![Page of a freshly scaffolded plugin](https://raw.githubusercontent.com/fastpluggy/cli/main/docs/screenshots/new-plugin-page.png)

### Validate a plugin

```bash
fastpluggy validate [plugin_dir] [--json]
```

Checks a plugin directory against the compliance rules of core (docs, README, tests, entry point, CI file, e2e routes). Exits with 0 when every check passes, 1 otherwise. A plugin created by `new-plugin` passes as is, unless it was created with `--ci none`.

### Fix plugin compliance issues

```bash
fastpluggy fix-plugin [plugin_dir]
```

Runs compliance checks and interactively lets you select which issues to fix. Shows each failing rule with a description and lets you pick from a checkbox list.

Options:
- `--all` — apply all fixes without prompting
- `--rule R4 --rule R6` — only fix specific rules
- `--dry-run` — preview changes without writing files

Auto-fixable rules:

| Rule | What it fixes |
|------|---------------|
| R1   | Create `docs/` directory |
| R2   | Create `README.md` |
| R3   | Add `[e2e]` optional deps to `pyproject.toml` |
| R4   | Create `e2e/` directory with a `routes.json` skeleton |
| R6   | Create `docs/screenshots/e2e/` with `.gitkeep` |
| R9   | Create `tests/` with stub test |
| R10  | Create or update `.gitignore` |
| R12  | Create `e2e/routes.json` skeleton |

Example:

```bash
# Interactive — pick which fixes to apply
fastpluggy fix-plugin plugins/my_plugin

# Fix everything at once
fastpluggy fix-plugin --all plugins/my_plugin

# Preview only
fastpluggy fix-plugin --dry-run .
```

### Start dev server

```bash
fastpluggy start-dev-server [--port PORT] [--host HOST] [--reload] [--verbose] [--ssl]
```

Auto-discovers all installed plugins via entry points. Defaults `DATABASE_URL` to SQLite if not set.

Despite the name, this is also the production entrypoint for apps that run FastPluggy
as a host process, so it can express reverse-proxy settings:

```bash
fastpluggy start-dev-server --host 0.0.0.0 --port 8000 \
    --proxy-headers --forwarded-allow-ips 172.18.0.0/16
```

`--proxy-headers / --no-proxy-headers` controls whether `X-Forwarded-*` is honoured, so
that `request.url_for()` resolves against the public vhost rather than the socket — which
is what makes an OIDC `redirect_uri` come out right behind a proxy. `--forwarded-allow-ips`
controls *which* peers are trusted to set those headers.

uvicorn already honours proxy headers by default, but only from `127.0.0.1`; when the proxy
is a different container or host, they are dropped silently. Setting the allow-list is
therefore the part that actually fixes the URLs. Leave both flags off and nothing changes:
uvicorn keeps its own defaults, including its `FORWARDED_ALLOW_IPS` environment fallback.

Do not reach for `--forwarded-allow-ips '*'` unless the port is reachable *only* by the
proxy. A wildcard lets any client that can open a connection forge `X-Forwarded-*` and steer
every URL the app generates; the CLI warns when you use one.

#### ⚠ The CLI is not in the FastPluggy base image

`fast_pluggy/docker/Dockerfile` installs core (from repo source) plus `uvicorn[standard]`
and nothing else, and `fastpluggy-cli` is **not** a dependency of core's `pyproject.toml`.
Core never pulls it in.

So an app whose container boots via `fastpluggy start-dev-server` **must list
`fastpluggy-cli` in its own dependency manifest**. Leave it out and the image builds fine,
then the container's `CMD` dies at start with `fastpluggy: not found`.

Such an app pins the CLI in its `pyproject.toml` alongside core,

```toml
dependencies = [
    "FastPluggy>=0.4.37,!=0.4.32",
    "fastpluggy-cli>=0.1.1",               # `fastpluggy start-dev-server` / `tasks-worker`
    ...
]
```

and only then can its Dockerfile say

```dockerfile
CMD ["sh", "-c", "fastpluggy tasks-worker start --beat & exec fastpluggy start-dev-server --host 0.0.0.0 --port 8000"]
```

`--verbose` also turns on the debug logs of core and of the plugins (it sets `LOGURU_LEVEL=DEBUG`); `--ssl` serves HTTPS with a generated self-signed certificate.

### Show help

```bash
fastpluggy help        # same output as fastpluggy --help
```

## Dependencies

- Python 3.11+
- fastpluggy, fastapi, uvicorn
- click, questionary, jinja2
- cryptography (optional, for `start-dev-server --ssl`)

```bash
pip install -e .
# For `start-dev-server --ssl`
pip install -e ".[ssl]"
```

## Plugin CLI Extensions

Plugins can inject their own CLI commands under the `fastpluggy` group. Plugin commands appear in a separate **Plugin Commands** section in `fastpluggy --help`.

### How it works

At startup, the CLI discovers entry points registered under the `fastpluggy.cli` group and registers each as a subcommand. Each entry point **must** be a `click.Group` (not a flat command) to avoid name collisions.

### Registering a plugin command group

In your plugin's `pyproject.toml`:

```toml
[project.entry-points."fastpluggy.cli"]
my-plugin = "my_package.cli:my_group"
```

In your plugin's `cli.py`:

```python
import click

@click.group("my-plugin")
def my_group():
    """My plugin commands."""
    pass

@my_group.command()
def do_something():
    """Run my plugin task."""
    click.echo("Done!")
```

This gives the user `fastpluggy my-plugin do-something`.

### Contract

| Item | Requirement |
|------|-------------|
| Entry point group | `fastpluggy.cli` |
| Entry point value | A `click.Group` instance (or zero-arg callable returning one) |
| Naming convention | Use the plugin name with hyphens (e.g. `auth-user`, `tasks-worker`) |
| Collision handling | First registered wins; conflicts produce a stderr warning |
| Import errors | Swallowed gracefully with a stderr warning — the CLI never crashes |

### Help output

```
$ fastpluggy --help

Core Commands:
  fix-plugin        Interactive auto-fix for failing compliance checks
  help              Show available commands
  new               Create a new FastPluggy project
  new-plugin        Scaffold a standalone, installable FastPluggy plugin
  start-dev-server  Start a FastPluggy dev server with auto-discovered plugins
  validate          Validate a plugin directory against compliance rules

Plugin Commands:
  auth-user         Auth user management commands
  tasks-worker      Background task worker commands
```

## Migration: `--start-worker` removed

The `--start-worker` flag on `fastpluggy start-dev-server` has been **removed**.
Background workers are now started via the plugin CLI extension:

```bash
# Before (removed)
fastpluggy start-dev-server --start-worker

# After
fastpluggy tasks-worker start
# Or with a custom worker count:
fastpluggy tasks-worker start -n 4
```

The `tasks-worker` group is auto-discovered from the `fastpluggy-tasks-worker`
package when installed.

## Tests

```bash
# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=fastpluggy_cli
```

Tests verify CLI commands and run in CI on every push and pull request.

Each command has one end-to-end test that runs it for real: the scaffolded plugin is
installed in a throwaway virtualenv and loaded by an app (`tests/test_scaffold_e2e.py`,
needs network access for the build), the generated project is imported, and
`start-dev-server` is started and queried over HTTP.

## Layout

```
fastpluggy_cli/
├── cli.py                 # the `fastpluggy` group, plugin command discovery
├── commands/              # new / new-plugin, start-dev-server, validate / fix-plugin
├── scaffold.py            # content of every generated file
├── fixers.py              # one auto-fix per compliance rule
└── scaffold_templates/    # the file bodies, as Jinja templates
```

## Contributing

Issues and pull requests are welcome on [GitHub](https://github.com/fastpluggy/cli).
Accepted changes are applied upstream by the maintainers and come back to GitHub
through the mirror, so a merged pull request may show as closed rather than merged.

## Releases

Each `v*` tag builds the package, publishes it to [PyPI](https://pypi.org/project/fastpluggy-cli/)
and creates the matching GitHub release. See [CHANGELOG.md](CHANGELOG.md).

## License

MIT, see [LICENSE](LICENSE).
