Skip to content

Commit fbcedb8

Browse files
author
Greyforge Admin
committed
Honor DAG task working directories
1 parent 85dbd43 commit fbcedb8

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/cortex-cli/src/dag_cmd/executor.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ impl TaskExecutor {
4141
.get("command")
4242
.and_then(|v| v.as_str())
4343
.map(String::from);
44+
let working_dir = task
45+
.metadata
46+
.get("working_dir")
47+
.and_then(|v| v.as_str())
48+
.map(String::from);
4449

4550
if self.verbose {
4651
if let Some(ref cmd) = command {
@@ -55,7 +60,7 @@ impl TaskExecutor {
5560

5661
// If there's a command, execute it
5762
let (status, output, error) = if let Some(cmd) = command {
58-
match self.run_command(&cmd).await {
63+
match self.run_command(&cmd, working_dir.as_deref()).await {
5964
Ok(output) => (TaskStatus::Completed, Some(output), None),
6065
Err(e) => (TaskStatus::Failed, None, Some(e.to_string())),
6166
}
@@ -80,14 +85,19 @@ impl TaskExecutor {
8085
}
8186

8287
/// Run a shell command with timeout.
83-
async fn run_command(&self, cmd: &str) -> Result<String> {
88+
async fn run_command(&self, cmd: &str, working_dir: Option<&str>) -> Result<String> {
8489
let timeout_duration = self.timeout;
8590

8691
let result = tokio::time::timeout(timeout_duration, async {
8792
let (shell, shell_arg) = shell_invocation();
88-
let output = tokio::process::Command::new(shell)
89-
.arg(shell_arg)
90-
.arg(cmd)
93+
let mut command = tokio::process::Command::new(shell);
94+
command.arg(shell_arg).arg(cmd);
95+
96+
if let Some(dir) = working_dir {
97+
command.current_dir(dir);
98+
}
99+
100+
let output = command
91101
.output()
92102
.await
93103
.context("Failed to execute command")?;

src/cortex-cli/src/dag_cmd/helpers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub fn convert_specs(input: &DagSpecInput) -> Vec<TaskSpec> {
6565
spec = spec.with_metadata("command", serde_json::json!(cmd));
6666
}
6767

68+
if let Some(working_dir) = &t.working_dir {
69+
spec = spec.with_metadata("working_dir", serde_json::json!(working_dir));
70+
}
71+
6872
for (key, value) in &t.metadata {
6973
spec = spec.with_metadata(key, value.clone());
7074
}

src/cortex-cli/src/dag_cmd/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fn test_convert_specs() {
4141
name: "a".to_string(),
4242
description: "Task A".to_string(),
4343
command: Some("echo A".to_string()),
44+
working_dir: None,
4445
depends_on: vec![],
4546
affected_files: vec![],
4647
priority: 10,
@@ -51,6 +52,7 @@ fn test_convert_specs() {
5152
name: "b".to_string(),
5253
description: "Task B".to_string(),
5354
command: None,
55+
working_dir: None,
5456
depends_on: vec!["a".to_string()],
5557
affected_files: vec!["file.txt".to_string()],
5658
priority: 5,

src/cortex-cli/src/dag_cmd/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub struct TaskSpecInput {
5656
/// Command to execute (optional).
5757
#[serde(default)]
5858
pub command: Option<String>,
59+
/// Working directory for command execution.
60+
#[serde(default)]
61+
pub working_dir: Option<String>,
5962
/// Task dependencies (names of tasks).
6063
#[serde(default)]
6164
pub depends_on: Vec<String>,

0 commit comments

Comments
 (0)