Initial upload: API Mock CLI v0.1.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-01-29 13:53:39 +00:00
parent 9e302dfb8b
commit ce0caf3259

133
src/cli/commands/init.py Normal file
View File

@@ -0,0 +1,133 @@
from pathlib import Path
import click
from src.cli.config import ConfigLoader
from src.utils.file import FileUtils
@click.command("init")
@click.option(
"--name",
type=str,
default="my-mock-api",
help="Project name"
)
@click.option(
"--description",
type=str,
default=None,
help="Project description"
)
@click.option(
"--template",
type=click.Choice(["basic", "full", "minimal"]),
default="basic",
help="Project template"
)
@click.option(
"--path",
type=click.Path(),
default=".",
help="Project directory"
)
def init_command(name: str, description: str, template: str, path: str):
project_path = Path(path) / name
project_path.mkdir(parents=True, exist_ok=True)
config_path = project_path / "config.yaml"
endpoints_path = project_path / "endpoints.yaml"
config_loader = ConfigLoader()
config_loader.create_default_config(str(config_path))
template_content = _get_template_content(template)
FileUtils.write_yaml(str(endpoints_path), template_content)
click.echo(f"Created mock API project: {name}")
click.echo(f" - Config: {config_path}")
click.echo(f" - Endpoints: {endpoints_path}")
click.echo("Run 'api-mock start' to start the mock server")
def _get_template_content(template: str) -> dict:
templates = {
"minimal": {
"version": "1.0.0",
"endpoints": []
},
"basic": {
"version": "1.0.0",
"endpoints": [
{
"path": "/api/hello",
"method": "GET",
"name": "Hello World",
"description": "Returns a greeting message",
"response": {
"status_code": 200,
"body": {
"message": "Hello, World!"
}
}
}
]
},
"full": {
"version": "1.0.0",
"endpoints": [
{
"path": "/api/users/{id}",
"method": "GET",
"name": "Get User",
"description": "Retrieve a user by ID",
"response": {
"status_code": 200,
"body": {
"id": "{{request.path.id}}",
"name": "John Doe",
"email": "john@example.com"
}
}
},
{
"path": "/api/users",
"method": "GET",
"name": "List Users",
"description": "List all users",
"response": {
"status_code": 200,
"body": {
"users": [
{"id": 1, "name": "John"},
{"id": 2, "name": "Jane"}
],
"total": 2
}
}
},
{
"path": "/api/users",
"method": "POST",
"name": "Create User",
"description": "Create a new user",
"response": {
"status_code": 201,
"body": {
"message": "User created",
"id": "{{uuid}}"
}
},
"validators": {
"body": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {"type": "string", "minLength": 1},
"email": {"type": "string", "format": "email"}
}
}
}
}
]
}
}
return templates.get(template, templates["basic"])
def _get_endpoints_content(template: str) -> dict:
return _get_template_content(template)