diff --git a/Cargo.lock b/Cargo.lock index e75fbb6..dc4a58e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,9 +10,9 @@ checksum = "62f447d68cfa5a9ab0c1c862a703da2a65b5ed1b7ce1153c9eb0169506d56019" [[package]] name = "cc" -version = "1.0.73" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" [[package]] name = "cfg-if" @@ -158,7 +158,7 @@ dependencies = [ name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "winapi" @@ -181,4 +181,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - diff --git a/src/lib.rs b/src/lib.rs index 9c03367..f3b1b3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ use env_reader::env_var_exists; use log::*; +use std::option::Option; use std::path::Path; use std::time::Duration; -use std::option::Option; pub mod env_reader; pub mod sleeper; @@ -15,7 +15,7 @@ pub struct Command { pub struct Config { pub hosts: String, pub paths: String, - pub command: Option, + pub command: Option<(Command, String)>, pub global_timeout: u64, pub tcp_connection_timeout: u64, pub wait_before: u64, @@ -25,11 +25,7 @@ pub struct Config { const LINE_SEPARATOR: &str = "--------------------------------------------------------"; -pub fn wait( - sleep: &mut dyn sleeper::Sleeper, - config: &Config, - on_timeout: &mut dyn FnMut(), -) { +pub fn wait(sleep: &mut dyn sleeper::Sleeper, config: &Config, on_timeout: &mut dyn FnMut()) { info!("{}", LINE_SEPARATOR); info!(" docker-compose-wait {}", env!("CARGO_PKG_VERSION")); info!("---------------------------"); @@ -44,12 +40,11 @@ pub fn wait( " - TCP connection timeout before retry: {} seconds ", config.tcp_connection_timeout ); - if config.command.is_some() { - debug!( - " - Command to run once ready: {}", - env_reader::env_var("WAIT_COMMAND", "".to_string()) - ); + + if let Some((_, command_string)) = &config.command { + debug!(" - Command to run once ready: {}", command_string); } + debug!( " - Sleeping time before checking for hosts/paths availability: {} seconds", config.wait_before @@ -130,20 +125,30 @@ pub fn wait( info!("docker-compose-wait - Everything's fine, the application can now start!"); info!("{}", LINE_SEPARATOR); - if let Some(command) = &config.command { - let err = exec::Command::new(&command.program).args(&command.argv).exec(); + if let Some((command, _)) = &config.command { + let err = exec::Command::new(&command.program) + .args(&command.argv) + .exec(); panic!("{}", err); } } -pub fn parse_command>(raw_cmd: S) -> Result, shell_words::ParseError> { +pub fn parse_command>( + raw_cmd: S, +) -> Result, shell_words::ParseError> { let s = raw_cmd.into(); - let t = s.trim(); - if t.len() == 0 { - return Ok(None) + let command_string = s.trim().to_string(); + if command_string.len() == 0 { + return Ok(None); } - let argv = shell_words::split(&t)?; - Ok(Some(Command { program: argv[0].clone(), argv })) + let argv = shell_words::split(&command_string)?; + Ok(Some(( + Command { + program: argv[0].clone(), + argv, + }, + command_string, + ))) } pub fn config_from_env() -> Config { @@ -261,7 +266,7 @@ mod test { #[test] #[should_panic] - fn should_panic_when_given_an_invalid_command(){ + fn should_panic_when_given_an_invalid_command() { let _guard = TEST_MUTEX.lock().unwrap(); set_env("", "", "", "", "", "", "a 'b"); config_from_env(); @@ -300,29 +305,41 @@ mod test { #[test] fn parse_command_handles_commands_without_args() { - let p = parse_command("ls".to_string()).unwrap().unwrap(); - assert_eq!("ls", p.program); - assert_eq!(vec!["ls"], p.argv); + let (command, command_string) = parse_command("ls".to_string()).unwrap().unwrap(); + assert_eq!("ls", command_string); + assert_eq!("ls", command.program); + assert_eq!(vec!["ls"], command.argv); } #[test] fn parse_command_handles_commands_with_args() { - let p = parse_command("ls -al".to_string()).unwrap().unwrap(); - assert_eq!("ls", p.program); - assert_eq!(vec!["ls", "-al"], p.argv); + let (command, command_string) = parse_command("ls -al".to_string()).unwrap().unwrap(); + assert_eq!("ls -al", command_string); + assert_eq!("ls", command.program); + assert_eq!(vec!["ls", "-al"], command.argv); } #[test] fn parse_command_discards_leading_and_trailing_whitespace() { - let p = parse_command(" hello world ".to_string()).unwrap().unwrap(); - assert_eq!("hello", p.program); - assert_eq!(vec!["hello", "world"], p.argv); + let (command, command_string) = parse_command(" hello world ".to_string()) + .unwrap() + .unwrap(); + assert_eq!("hello world", command_string); + assert_eq!("hello", command.program); + assert_eq!(vec!["hello", "world"], command.argv); } #[test] fn parse_command_strips_shell_quotes() { - let p = parse_command(" find . -type \"f\" -name '*.rs' ".to_string()).unwrap().unwrap(); - assert_eq!("find", p.program); - assert_eq!(vec!["find", ".", "-type", "f", "-name", "*.rs"], p.argv); + let (command, command_string) = + parse_command(" find . -type \"f\" -name '*.rs' ".to_string()) + .unwrap() + .unwrap(); + assert_eq!("find . -type \"f\" -name '*.rs'", command_string); + assert_eq!("find", command.program); + assert_eq!( + vec!["find", ".", "-type", "f", "-name", "*.rs"], + command.argv + ); } }