From 91e1bbb4ce638bd4bb4e34e050e1dd3c5bb4d8ec Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:40 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- src/cli/commands/start.py | 104 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/cli/commands/start.py diff --git a/src/cli/commands/start.py b/src/cli/commands/start.py new file mode 100644 index 0000000..6dfae69 --- /dev/null +++ b/src/cli/commands/start.py @@ -0,0 +1,104 @@ +import os +import click +from src.cli.config import ConfigLoader +from src.core.server import MockServer +from src.models.config import ServerConfig +from src.models.endpoint import Endpoint +from src.utils.file import FileUtils + + +@click.command("start") +@click.option( + "--port", + type=int, + default=None, + help="Port to run the server on" +) +@click.option( + "--host", + type=str, + default=None, + help="Host to bind to" +) +@click.option( + "--reload/--no-reload", + default=None, + help="Enable auto-reload" +) +@click.option( + "--offline/--online", + default=None, + help="Run in offline mode (no tunnel)" +) +@click.option( + "--tunnel/--no-tunnel", + default=None, + help="Enable/disable tunnel" +) +@click.option( + "--config", + type=click.Path(exists=True), + default=None, + help="Path to configuration file" +) +@click.option( + "--endpoints", + type=click.Path(exists=True), + default=None, + help="Path to endpoints file" +) +@click.option( + "--verbose", + "-v", + is_flag=True, + default=False, + help="Enable verbose output" +) +def start_command( + port: int, + host: str, + reload: bool, + offline: bool, + tunnel: bool, + config: str, + endpoints: str, + verbose: bool +): + config_path = ConfigLoader.find_config_file(config) + if config_path: + config_data = ConfigLoader.load(config_path) + else: + config_data = ConfigLoader.load() + server_config = config_data.server + if port: + server_config.port = port + if host: + server_config.host = host + if reload is not None: + server_config.reload = reload + if offline is not None: + server_config.offline = offline + if tunnel is not None: + server_config.tunnel = tunnel + endpoint_path = endpoints or server_config.endpoints_file + if not os.path.exists(endpoint_path): + click.echo(f"Error: Endpoints file not found: {endpoint_path}", err=True) + return + endpoints_data = FileUtils.load_yaml(endpoint_path) + endpoints_list = [] + for ep_data in endpoints_data.get("endpoints", []): + try: + endpoint = Endpoint(**ep_data) + endpoints_list.append(endpoint) + except Exception as e: + click.echo(f"Warning: Invalid endpoint definition: {e}") + server = MockServer(server_config) + server.register_endpoints(endpoints_list) + if verbose: + click.echo(f"Registered {len(endpoints_list)} endpoints") + for ep in endpoints_list: + click.echo(f" - {ep.method} {ep.path}") + click.echo(f"Starting server on {server_config.host}:{server_config.port}") + if server_config.offline: + click.echo("Mode: Offline (no tunnel)") + server.run()