From 2393a9bc1a97ba95f5c9546fec231e0b3ff02df7 Mon Sep 17 00:00:00 2001 From: Iam54r1n4 Date: Fri, 7 Feb 2025 03:01:51 +0000 Subject: [PATCH] Refactor ip-address command(May break functionality) --- core/cli.py | 10 +++++++++- core/cli_api.py | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/cli.py b/core/cli.py index 47860b5..4e8b052 100644 --- a/core/cli.py +++ b/core/cli.py @@ -237,7 +237,15 @@ def ip_address(edit: bool, ipv4: str, ipv6: str): - Use --edit with -4 or -6 to manually update IPs. """ try: - cli_api.ip_address(edit, ipv4, ipv6) + if not edit: + cli_api.add_ip_address() + click.echo("IP addresses added successfully.") + return + + if not ipv4 and not ipv6: + raise click.UsageError('Error: You must specify either -4 or -6') + + cli_api.edit_ip_address(ipv4, ipv6) click.echo("IP address configuration updated successfully.") except Exception as e: click.echo(f'{e}', err=True) diff --git a/core/cli_api.py b/core/cli_api.py index 133849d..3237962 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -298,6 +298,30 @@ def ip_address(edit: bool, ipv4: str, ipv6: str): run_cmd(['bash', Command.IP_ADD.value, 'add']) +def add_ip_address(): + ''' + Adds IP addresses from the environment to the .configs.env file. + ''' + run_cmd(['bash', Command.IP_ADD.value, 'add']) + + +def edit_ip_address(ipv4: str, ipv6: str): + """ + Edits the IP address configuration based on provided IPv4 and/or IPv6 addresses. + + :param ipv4: The new IPv4 address to be configured. If provided, the IPv4 address will be updated. + :param ipv6: The new IPv6 address to be configured. If provided, the IPv6 address will be updated. + :raises InvalidInputError: If neither ipv4 nor ipv6 is provided. + """ + + if not ipv4 and not ipv6: + raise InvalidInputError('Error: --edit requires at least one of --ipv4 or --ipv6.') + if ipv4: + run_cmd(['bash', Command.IP_ADD.value, 'edit', '-4', ipv4]) + if ipv6: + run_cmd(['bash', Command.IP_ADD.value, 'edit', '-6', ipv6]) + + def update_geo(country: str): ''' Updates geographic data files based on the specified country.