Skip to content

Commit

Permalink
Add support for additional arguments in run command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmurai committed Jan 23, 2025
1 parent d591668 commit a1484ae
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 85 deletions.
4 changes: 3 additions & 1 deletion dem/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
class RunCommandRequest(BaseModel):
dev_env_name: str
task_name: str
extra_args: str

@Platform.fastapi_app.post("/run")
def run_command(request: RunCommandRequest) -> dict:
dev_env_name = request.dev_env_name
task_name = request.task_name
extra_args = request.extra_args

try:
run_execute(platform, dev_env_name, task_name)
run_execute(platform, dev_env_name, task_name, extra_args)
return {"status": "success", "message": f"Task {task_name} executed in {dev_env_name}"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
7 changes: 4 additions & 3 deletions dem/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# dem/cli/main.py

import typer, importlib.metadata
from typing import Generator
from typing import Generator, Optional
from typing_extensions import Annotated
import os
from dem import __command__, __app_name__
Expand Down Expand Up @@ -352,7 +352,8 @@ def init(project_path: Annotated[str, typer.Argument(help="Path of the project."
def run(dev_env_name: Annotated[str, typer.Argument(help="Name of the Development Environment to run the task in. If not set, the default Dev Env will be used.",
autocompletion=autocomplete_installed_dev_env_name)] = "",
task_name: Annotated[str, typer.Argument(help="The name of the task to run.",
autocompletion=autocomplete_task_name)] = "") -> None:
autocompletion=autocomplete_task_name)] = "",
extra_args: Annotated[str, typer.Option(help="Additional arguments to pass to the task")] = "") -> None:
"""
Run the task of the Development Environment. The Dev Env must be installed.
Expand All @@ -364,7 +365,7 @@ def run(dev_env_name: Annotated[str, typer.Argument(help="Name of the Developmen
if not task_name and dev_env_name:
task_name = dev_env_name
dev_env_name = ""
run_cmd.execute(platform, dev_env_name, task_name)
run_cmd.execute(platform, dev_env_name, task_name, extra_args)
else:
raise InternalError("Error: The platform hasn't been initialized properly!")

Expand Down
7 changes: 5 additions & 2 deletions dem/core/commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def dev_env_health_check(platform: Platform, dev_env: DevEnv) -> None:
platform.install_dev_env(dev_env)
stdout.print(f"[green]DEM successfully fixed the {dev_env.name} Development Environment![/]")

def execute(platform: Platform, dev_env_name: str, task_name: str) -> None:
def execute(platform: Platform, dev_env_name: str, task_name: str, cmd_extra_args: str) -> None:
""" Run a task in a Development Environment.
Args:
Expand Down Expand Up @@ -65,7 +65,10 @@ def execute(platform: Platform, dev_env_name: str, task_name: str) -> None:
if advanced_task["mount_workdir"] == True:
pass

command += f" {advanced_task['extra_options']} {advanced_task['image']} {advanced_task['command']}"
command += f" {advanced_task['extra_args']} {advanced_task['image']}"

if advanced_task['command'] or cmd_extra_args:
command += f" /bin/bash -c \"{advanced_task['command']} {cmd_extra_args}\""

if advanced_task['enable_api'] == True:
platform.api_server.start()
Expand Down
1 change: 0 additions & 1 deletion dem/core/dev_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# dem/core/dev_env.py

from dem.core.tool_images import ToolImage, ToolImages
from enum import Enum
import json, os

class DevEnv():
Expand Down
8 changes: 7 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ Rename the Development Environment.

---

## **`dem run [DEV_ENV_NAME] TASK_NAME`**
## **`dem run [DEV_ENV_NAME] TASK_NAME [OPTIONS]`**

**Description:**

Expand All @@ -391,6 +391,12 @@ Run the task of the Development Environment. The Dev Env must be installed.
If the Dev Env is not specified, the default Dev Env will be used. If the default Dev Env is not
set, an error message will be printed.

**Options:**

| Options | Description |
|---------------------|---------------------------------------------------------|
| `--extra-args` | Additional arguments to pass to the container |

**Arguments:**

| Argument | Description | Required |
Expand Down
78 changes: 1 addition & 77 deletions tests/cli/test_run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,80 +41,4 @@ def test_dev_env_health_check(mock_stderr_print: MagicMock, mock_stdout_print: M
mock_stderr_print.assert_called_once_with("[red]Error: Incorrect installation![/]")
mock_typer_confirm.assert_called_once_with("Should DEM reinstall the DevEnv?", abort=True)
mock_platform.install_dev_env.assert_called_once_with(mock_dev_env)
mock_stdout_print.assert_called_once_with(f"[green]DEM successfully fixed the {mock_dev_env.name} Development Environment![/]")

@patch("dem.core.commands.run_cmd.execute")
def test_run_without_dev_env_name(mock_execute: MagicMock) -> None:
# Setup
mock_platform = MagicMock()
main.platform = mock_platform

# Run
result = runner.invoke(main.typer_cli, ["run", "my-task"])

# Check
assert result.exit_code == 0

mock_execute.assert_called_once_with(mock_platform, "", "my-task")

@patch("dem.core.commands.run_cmd.stderr.print")
def test_run_cmd_no_dev_env_name_no_default(mock_stderr_print: MagicMock) -> None:
# Setup
mock_platform = MagicMock()
mock_platform.default_dev_env_name = ""
main.platform = mock_platform

# Run
run_cmd.execute(mock_platform, "", "my-task")

# Check
mock_stderr_print.assert_called_once_with("[red]Error: Only one parameter is supplied but no default Dev Env is set! Please specify the Dev Env to run the task in or set a default one![/]")

@patch("dem.core.commands.run_cmd.stderr.print")
def test_run_cmd_dev_env_not_found(mock_stderr_print: MagicMock) -> None:
# Setup
mock_platform = MagicMock()
main.platform = mock_platform
mock_platform.get_dev_env_by_name.return_value = None

# Run
run_cmd.execute(mock_platform, "my-dev-env", "my-task")

# Check
mock_stderr_print.assert_called_once_with("[red]Error: Unknown Development Environment: my-dev-env[/]")

@patch("dem.core.commands.run_cmd.stderr.print")
def test_run_cmd_dev_env_not_installed(mock_stderr_print: MagicMock) -> None:
# Setup
mock_platform = MagicMock()
main.platform = mock_platform

mock_dev_env = MagicMock()
mock_dev_env.is_installed = False
mock_platform.get_dev_env_by_name.return_value = mock_dev_env

# Run
run_cmd.execute(mock_platform, "my-dev-env", "my-task")

# Check
mock_stderr_print.assert_called_once_with("[red]Error: Development Environment [bold]my-dev-env[/bold] is not installed![/]")

@patch("dem.core.commands.run_cmd.dev_env_health_check")
@patch("dem.core.commands.run_cmd.stderr.print")
def test_run_cmd_task_not_found(mock_stderr_print: MagicMock,
mock_dev_env_health_check: MagicMock) -> None:
# Setup
mock_platform = MagicMock()
main.platform = mock_platform

mock_dev_env = MagicMock()
mock_dev_env.is_installed = True
mock_dev_env.tasks = {}
mock_platform.get_dev_env_by_name.return_value = mock_dev_env

# Run
run_cmd.execute(mock_platform, "my-dev-env", "my-task")

# Check
mock_dev_env_health_check.assert_called_once_with(mock_platform, mock_dev_env)
mock_stderr_print.assert_called_once_with("[red]Error: Task [bold]my-task[/bold] not found in Development Environment [bold]my-dev-env[/bold]![/]")
mock_stdout_print.assert_called_once_with(f"[green]DEM successfully fixed the {mock_dev_env.name} Development Environment![/]")

0 comments on commit a1484ae

Please sign in to comment.