Skip to content

Commit

Permalink
fix(#500): better argument parsing for the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
SteinRobert committed Apr 22, 2024
1 parent 316fb02 commit 285c1a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
10 changes: 9 additions & 1 deletion client/gefyra/cli/run.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -50,6 +57,7 @@
" times"
),
type=str,
callback=parse_env,
multiple=True,
)
@click.option(
Expand Down
19 changes: 19 additions & 0 deletions client/gefyra/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 21 additions & 1 deletion client/tests/unit/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",))
Expand Down

0 comments on commit 285c1a5

Please sign in to comment.