Skip to content

Commit

Permalink
global chdir
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Nov 4, 2024
1 parent 8f76574 commit b0837fc
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct TargetHost {
struct Deployment {
name: String,
hosts: String,
chdir: Option<String>,
tasks: Vec<Task>,
}

Expand Down Expand Up @@ -270,16 +271,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for task in dep.tasks {
println!("{}", format!("Executing task: {}", task.name).cyan()); // Print task name in cyan

let task_chdir = task.chdir.as_deref().or(dep.chdir.as_deref()); // Use task-level chdir if present, otherwise use top-level chdir

if let Some(shell_command) = task.shell {
// Substitute Jinja variables in shell_command
let substituted_shell_command = replace_placeholders(&shell_command, &register_map, &vars_map);
println!("{}", format!("> {}", substituted_shell_command).magenta());

let display_output = task.register.is_none();
let result = if is_localhost {
execute_local_task(&substituted_shell_command, true, display_output, task.chdir.as_deref())
execute_local_task(&substituted_shell_command, true, display_output, task_chdir)
} else {
execute_task(session.as_ref().unwrap(), &substituted_shell_command, true, display_output, task.chdir.as_deref())
execute_task(session.as_ref().unwrap(), &substituted_shell_command, true, display_output, task_chdir)
};

match result {
Expand Down Expand Up @@ -322,9 +325,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let display_output = task.register.is_none();
let result = if is_localhost {
execute_local_task(&substituted_command, false, display_output, task.chdir.as_deref())
execute_local_task(&substituted_command, false, display_output, task_chdir)
} else {
execute_task(session.as_ref().unwrap(), &substituted_command, false, display_output, task.chdir.as_deref())
execute_task(session.as_ref().unwrap(), &substituted_command, false, display_output, task_chdir)
};

match result {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Test
hosts: local
chdir: tests/mocks/ls-dir
tasks:
- name: Global working directory
shell: cat file1.txt

- name: Task-level working directory
chdir: tests/mocks/ls-dir2
shell: cat file1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- name: Set vars
chdir: tests/mocks/ls-dir
command: ls

- name: Set vars
chdir: tests/mocks/ls-dir2
shell: ls
27 changes: 27 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,30 @@ AccessKeyId: abc, SecretAccessKey: def";

assert_eq!(stdout, expected_output);
}

#[test]
fn test_setting_global_working_directory_before_running_commands() {
let output = Command::new("cargo")
.args(&["run", "test-ymls/setting-global-working-directory-before-running-commands.yml"])
.output()
.expect("Failed to execute command");

assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();

let expected_output = "\
Starting deployment: Test
Executing task: Global working directory
> cat file1.txt
Output:
File 1 of ls-dir
Executing task: Task-level working directory
> cat file1.txt
Output:
File 1 of ls-dir2";

assert_eq!(stdout, expected_output);
}
2 changes: 1 addition & 1 deletion tests/mocks/ls-dir/file1.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
File 1
File 1 of ls-dir
2 changes: 1 addition & 1 deletion tests/mocks/ls-dir/file2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
File 2
File 2 of ls-dir
2 changes: 1 addition & 1 deletion tests/mocks/ls-dir2/file1.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
File 1
File 1 of ls-dir2
2 changes: 1 addition & 1 deletion tests/mocks/ls-dir2/file2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
File 2
File 2 of ls-dir2
2 changes: 1 addition & 1 deletion tests/mocks/ls-dir2/file3.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
File 3
File 3 of ls-dir2

0 comments on commit b0837fc

Please sign in to comment.