From edcc4e32e278974656b1b1ad6ca4c6473d0de678 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Sat, 17 May 2025 23:24:43 +0330 Subject: [PATCH] feat: Add CLI command to edit NormalSub subpath - Added `edit_normalsub_subpath` function to `cli_api.py` to interact with the `normalsub.sh edit_subpath` command, including input validation for the new subpath. - Updated the `normal-sub` command in `cli.py`: - Added 'edit_subpath' as a valid action. - Introduced a '--subpath' option for specifying the new path. - Modified the command logic to call the new API function when the 'edit_subpath' action is selected. --- core/cli.py | 16 ++++++++++++---- core/cli_api.py | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/core/cli.py b/core/cli.py index 8c684f6..6672e5b 100644 --- a/core/cli.py +++ b/core/cli.py @@ -421,10 +421,13 @@ def singbox(action: str, domain: str, port: int): @cli.command('normal-sub') -@click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False)) -@click.option('--domain', '-d', required=False, help='Domain name for SSL', type=str) -@click.option('--port', '-p', required=False, help='Port number for NormalSub service', type=int) -def normalsub(action: str, domain: str, port: int): +@click.option('--action', '-a', required=True, + type=click.Choice(['start', 'stop', 'edit_subpath'], case_sensitive=False), + help='Action to perform: start, stop, or edit_subpath') +@click.option('--domain', '-d', required=False, help='Domain name for SSL (for start action)', type=str) +@click.option('--port', '-p', required=False, help='Port number for NormalSub service (for start action)', type=int) +@click.option('--subpath', '-sp', required=False, help='New subpath (alphanumeric, for edit_subpath action)', type=str) +def normalsub(action: str, domain: str, port: int, subpath: str): try: if action == 'start': if not domain or not port: @@ -434,6 +437,11 @@ def normalsub(action: str, domain: str, port: int): elif action == 'stop': cli_api.stop_normalsub() click.echo(f'NormalSub stopped successfully.') + elif action == 'edit_subpath': + if not subpath: + raise click.UsageError('Error: --subpath is required for the edit_subpath action.') + cli_api.edit_normalsub_subpath(subpath) + click.echo(f'NormalSub subpath updated to {subpath} successfully.') except Exception as e: click.echo(f'{e}', err=True) diff --git a/core/cli_api.py b/core/cli_api.py index 4bbc44e..0ac9348 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -510,6 +510,14 @@ def start_normalsub(domain: str, port: int): raise InvalidInputError('Error: Both --domain and --port are required for the start action.') run_cmd(['bash', Command.INSTALL_NORMALSUB.value, 'start', domain, str(port)]) +def edit_normalsub_subpath(new_subpath: str): + '''Edits the subpath for NormalSub service.''' + if not new_subpath: + raise InvalidInputError('Error: New subpath cannot be empty.') + if not new_subpath.isalnum(): + raise InvalidInputError('Error: New subpath must contain only alphanumeric characters (a-z, A-Z, 0-9).') + + run_cmd(['bash', Command.INSTALL_NORMALSUB.value, 'edit_subpath', new_subpath]) def stop_normalsub(): '''Stops NormalSub.'''