From 07c0f8e07a3f9647676f2118d59669d9377d9192 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:42 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- src/cli/commands/generate.py | 84 ++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/cli/commands/generate.py diff --git a/src/cli/commands/generate.py b/src/cli/commands/generate.py new file mode 100644 index 0000000..daafc82 --- /dev/null +++ b/src/cli/commands/generate.py @@ -0,0 +1,84 @@ +from pathlib import Path +import click +from src.utils.file import FileUtils + + +@click.command("generate") +@click.argument("name") +@click.option( + "--method", + type=click.Choice(["GET", "POST", "PUT", "DELETE", "PATCH"]), + default="GET", + help="HTTP method" +) +@click.option( + "--path", + type=str, + default=None, + help="Endpoint path" +) +@click.option( + "--status-code", + type=int, + default=200, + help="Response status code" +) +@click.option( + "--output", + type=click.Path(), + default="endpoints.yaml", + help="Output file" +) +@click.option( + "--interactive/--no-interactive", + default=True, + help="Interactive mode for response template" +) +def generate_command( + name: str, + method: str, + path: str, + status_code: int, + output: str, + interactive: bool +): + endpoint_path = path or f"/api/{name.lower().replace(' ', '-')}" + endpoint = { + "path": endpoint_path, + "method": method, + "name": name, + "response": { + "status_code": status_code, + "body": { + "message": "Success", + "data": {} + } + } + } + response_body = {} + if interactive: + key = click.prompt("Response body key (or 'done' to finish)", default="done") + while key != "done": + value_type = click.prompt("Value type (string, number, boolean, array, object)", default="string") + if value_type == "string": + response_body[key] = "sample_value" + elif value_type == "number": + response_body[key] = 123 + elif value_type == "boolean": + response_body[key] = True + elif value_type == "array": + response_body[key] = [] + elif value_type == "object": + response_body[key] = {} + response_body[key] = endpoint["response"]["body"].get(key, {}) + key = click.prompt("Response body key (or 'done' to finish)", default="done") + existing_data = {"endpoints": []} + if Path(output).exists(): + existing_data = FileUtils.load_yaml(output) + if "endpoints" not in existing_data: + existing_data["endpoints"] = [] + endpoint["response"]["body"] = response_body if interactive else endpoint["response"]["body"] + existing_data["endpoints"].append(endpoint) + FileUtils.write_yaml(output, existing_data) + click.echo(f"Generated endpoint: {method} {endpoint_path}") + click.echo(f"Saved to: {output}")