diff --git a/client/gefyra/cli/run.py b/client/gefyra/cli/run.py index c32c541b..7b909b6c 100644 --- a/client/gefyra/cli/run.py +++ b/client/gefyra/cli/run.py @@ -1,6 +1,12 @@ import ast import click -from gefyra.cli.utils import OptionEatAll, check_connection_name, parse_ip_port_map +from gefyra.cli.utils import ( + OptionEatAll, + check_connection_name, + parse_env, + parse_ip_port_map, + parse_workload, +) @click.command() @@ -32,6 +38,7 @@ "--env-from", help="Copy the environment from the container in the notation 'Pod/Container'", type=str, + callback=parse_workload, ) @click.option( "-v", @@ -50,6 +57,7 @@ " times" ), type=str, + callback=parse_env, multiple=True, ) @click.option( diff --git a/client/gefyra/cli/utils.py b/client/gefyra/cli/utils.py index 41a89158..86f09669 100644 --- a/client/gefyra/cli/utils.py +++ b/client/gefyra/cli/utils.py @@ -216,6 +216,25 @@ def v(p: str): return res +def parse_env(ctx, param, envs: Tuple[str]) -> List[str]: + res = [] + for env in envs: + if "=" not in env: + raise ValueError("Invalid value for env variable. Please use 'ENV=value'.") + res.append(env) + return res + + +def parse_workload(ctx, param, workload: str) -> str: + MSG = ( + "Invalid workload format. Please provide the workload " + "in the format 'type/name' or 'type/name/container-name'." + ) + if "/" not in workload: + raise ValueError(MSG) + return workload + + def check_connection_name(ctx, param, selected: Optional[str] = None) -> str: from gefyra import api diff --git a/client/tests/unit/test_cli_utils.py b/client/tests/unit/test_cli_utils.py index 7855a903..ca9abbe6 100644 --- a/client/tests/unit/test_cli_utils.py +++ b/client/tests/unit/test_cli_utils.py @@ -7,7 +7,7 @@ from gefyra.local.utils import get_connection_from_kubeconfig, get_processed_paths from gefyra.api.utils import generate_env_dict_from_strings, get_workload_type -from gefyra.cli.utils import parse_ip_port_map +from gefyra.cli.utils import parse_env, parse_ip_port_map, parse_workload @patch("kubernetes.config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION", "/tmp/kube.yaml") @@ -97,6 +97,26 @@ def test_env_dict_creation(): ) +def test_env_parsing(): + try: + parse_env(None, None, ("APP",)) + except Exception as e: + assert "use 'ENV=value'" in str(e) + + res = parse_env(None, None, ("APP=test-app",)) + TestCase().assertListEqual(res, ["APP=test-app"]) + + +def test_workload_parsing(): + res = parse_workload(None, None, "pod/test") + assert res == "pod/test" + + try: + res = parse_workload(None, None, "pod") + except Exception as e: + assert "Invalid workload format" in str(e) + + def test_ip_port_map_parsing(): # single res = parse_ip_port_map(None, None, ("1234:1234",))