From ce0caf3259e6cedc73423db62d7a4fa1b023f34d Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:39 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- src/cli/commands/init.py | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/cli/commands/init.py diff --git a/src/cli/commands/init.py b/src/cli/commands/init.py new file mode 100644 index 0000000..2ab3fb0 --- /dev/null +++ b/src/cli/commands/init.py @@ -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)