-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55a3fa2
commit 1627a61
Showing
7 changed files
with
147 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use indexmap::IndexMap; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Debug(pub IndexMap<String, String>); | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Register { | ||
pub stdout: String, | ||
pub stderr: String, | ||
pub rc: i32, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use colored::Colorize; | ||
use indexmap::IndexMap; | ||
use serde_json::Value; | ||
use ssh2::Session; | ||
|
||
use crate::common; | ||
use crate::utils; | ||
|
||
fn handle_command_execution( | ||
is_localhost: bool, | ||
session: Option<&Session>, | ||
command: &str, | ||
use_shell: bool, | ||
display_output: bool, | ||
chdir: Option<&str>, | ||
register: Option<&String>, | ||
vars_map: &mut IndexMap<String, Value>, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let result = if is_localhost { | ||
utils::execute_local_command(command, use_shell, display_output, chdir) | ||
} else { | ||
utils::execute_ssh_command(session.unwrap(), command, use_shell, display_output, chdir) | ||
}; | ||
|
||
match result { | ||
Ok((stdout, stderr, exit_status)) => { | ||
if exit_status != 0 { | ||
return Err(format!( | ||
"Command execution failed with exit status: {}. Stopping further tasks.", | ||
exit_status | ||
) | ||
.red() | ||
.into()); | ||
} | ||
|
||
if let Some(register) = register { | ||
let register_value = serde_json::to_value(common::Register { | ||
stdout: stdout.clone(), | ||
stderr: stderr.clone(), | ||
rc: exit_status, | ||
})?; | ||
vars_map.insert(register.clone(), register_value); | ||
println!( | ||
"{}", | ||
format!("Registering output to: {}", register).yellow() | ||
); | ||
} | ||
} | ||
Err(e) => { | ||
return Err(format!( | ||
"Command execution failed with error: {}. Stopping further tasks.", | ||
e | ||
) | ||
.red() | ||
.into()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn process( | ||
commands: Vec<String>, | ||
is_localhost: bool, | ||
session: Option<&Session>, | ||
use_shell: bool, | ||
task_chdir: Option<&str>, | ||
register: Option<&String>, | ||
vars_map: &mut IndexMap<String, Value>, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
for cmd in commands { | ||
let substituted_cmd = utils::replace_placeholders(&cmd, vars_map); | ||
println!("{}", format!("> {}", substituted_cmd).magenta()); | ||
|
||
let display_output = register.is_none(); | ||
handle_command_execution( | ||
is_localhost, | ||
session, | ||
&substituted_cmd, | ||
use_shell, | ||
display_output, | ||
task_chdir, | ||
register, | ||
vars_map, | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use colored::Colorize; | ||
use indexmap::IndexMap; | ||
use serde_json::Value; | ||
|
||
use crate::common::Debug; | ||
use crate::utils; | ||
|
||
pub fn process(debug: &Debug, vars_map: &IndexMap<String, Value>) { | ||
println!("{}", "Debug:".blue()); | ||
for (key, msg) in debug.0.iter() { | ||
println!("{}", format!("{}:", key).blue()); | ||
let debug_msg = utils::replace_placeholders(msg, vars_map); | ||
println!("{}", format!("{}", debug_msg).blue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod command; | ||
pub mod debug; | ||
pub mod when; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use indexmap::IndexMap; | ||
use serde_json::Value; | ||
|
||
use crate::utils; | ||
|
||
pub fn process(condition: &Option<String>, vars_map: &IndexMap<String, Value>) -> bool { | ||
if let Some(cond) = condition { | ||
let template_str = format!("{{% if {} %}}true{{% else %}}false{{% endif %}}", cond); | ||
let rendered_cond = utils::replace_placeholders(&template_str, vars_map); | ||
if rendered_cond == "false" { | ||
false | ||
} else { | ||
true | ||
} | ||
} else { | ||
true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters