Skip to content

Commit 4c80927

Browse files
committed
Code revamp
1 parent 92e75a5 commit 4c80927

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

Cargo.lock

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use env_reader::env_var_exists;
22
use log::*;
3+
use std::option::Option;
34
use std::path::Path;
45
use std::time::Duration;
5-
use std::option::Option;
66

77
pub mod env_reader;
88
pub mod sleeper;
@@ -15,7 +15,7 @@ pub struct Command {
1515
pub struct Config {
1616
pub hosts: String,
1717
pub paths: String,
18-
pub command: Option<Command>,
18+
pub command: Option<(Command, String)>,
1919
pub global_timeout: u64,
2020
pub tcp_connection_timeout: u64,
2121
pub wait_before: u64,
@@ -25,11 +25,7 @@ pub struct Config {
2525

2626
const LINE_SEPARATOR: &str = "--------------------------------------------------------";
2727

28-
pub fn wait(
29-
sleep: &mut dyn sleeper::Sleeper,
30-
config: &Config,
31-
on_timeout: &mut dyn FnMut(),
32-
) {
28+
pub fn wait(sleep: &mut dyn sleeper::Sleeper, config: &Config, on_timeout: &mut dyn FnMut()) {
3329
info!("{}", LINE_SEPARATOR);
3430
info!(" docker-compose-wait {}", env!("CARGO_PKG_VERSION"));
3531
info!("---------------------------");
@@ -44,12 +40,11 @@ pub fn wait(
4440
" - TCP connection timeout before retry: {} seconds ",
4541
config.tcp_connection_timeout
4642
);
47-
if config.command.is_some() {
48-
debug!(
49-
" - Command to run once ready: {}",
50-
env_reader::env_var("WAIT_COMMAND", "".to_string())
51-
);
43+
44+
if let Some((_, command_string)) = &config.command {
45+
debug!(" - Command to run once ready: {}", command_string);
5246
}
47+
5348
debug!(
5449
" - Sleeping time before checking for hosts/paths availability: {} seconds",
5550
config.wait_before
@@ -130,20 +125,30 @@ pub fn wait(
130125
info!("docker-compose-wait - Everything's fine, the application can now start!");
131126
info!("{}", LINE_SEPARATOR);
132127

133-
if let Some(command) = &config.command {
134-
let err = exec::Command::new(&command.program).args(&command.argv).exec();
128+
if let Some((command, _)) = &config.command {
129+
let err = exec::Command::new(&command.program)
130+
.args(&command.argv)
131+
.exec();
135132
panic!("{}", err);
136133
}
137134
}
138135

139-
pub fn parse_command<S: Into<String>>(raw_cmd: S) -> Result<Option<Command>, shell_words::ParseError> {
136+
pub fn parse_command<S: Into<String>>(
137+
raw_cmd: S,
138+
) -> Result<Option<(Command, String)>, shell_words::ParseError> {
140139
let s = raw_cmd.into();
141-
let t = s.trim();
142-
if t.len() == 0 {
143-
return Ok(None)
140+
let command_string = s.trim().to_string();
141+
if command_string.len() == 0 {
142+
return Ok(None);
144143
}
145-
let argv = shell_words::split(&t)?;
146-
Ok(Some(Command { program: argv[0].clone(), argv }))
144+
let argv = shell_words::split(&command_string)?;
145+
Ok(Some((
146+
Command {
147+
program: argv[0].clone(),
148+
argv,
149+
},
150+
command_string,
151+
)))
147152
}
148153

149154
pub fn config_from_env() -> Config {
@@ -261,7 +266,7 @@ mod test {
261266

262267
#[test]
263268
#[should_panic]
264-
fn should_panic_when_given_an_invalid_command(){
269+
fn should_panic_when_given_an_invalid_command() {
265270
let _guard = TEST_MUTEX.lock().unwrap();
266271
set_env("", "", "", "", "", "", "a 'b");
267272
config_from_env();
@@ -300,29 +305,41 @@ mod test {
300305

301306
#[test]
302307
fn parse_command_handles_commands_without_args() {
303-
let p = parse_command("ls".to_string()).unwrap().unwrap();
304-
assert_eq!("ls", p.program);
305-
assert_eq!(vec!["ls"], p.argv);
308+
let (command, command_string) = parse_command("ls".to_string()).unwrap().unwrap();
309+
assert_eq!("ls", command_string);
310+
assert_eq!("ls", command.program);
311+
assert_eq!(vec!["ls"], command.argv);
306312
}
307313

308314
#[test]
309315
fn parse_command_handles_commands_with_args() {
310-
let p = parse_command("ls -al".to_string()).unwrap().unwrap();
311-
assert_eq!("ls", p.program);
312-
assert_eq!(vec!["ls", "-al"], p.argv);
316+
let (command, command_string) = parse_command("ls -al".to_string()).unwrap().unwrap();
317+
assert_eq!("ls -al", command_string);
318+
assert_eq!("ls", command.program);
319+
assert_eq!(vec!["ls", "-al"], command.argv);
313320
}
314321

315322
#[test]
316323
fn parse_command_discards_leading_and_trailing_whitespace() {
317-
let p = parse_command(" hello world ".to_string()).unwrap().unwrap();
318-
assert_eq!("hello", p.program);
319-
assert_eq!(vec!["hello", "world"], p.argv);
324+
let (command, command_string) = parse_command(" hello world ".to_string())
325+
.unwrap()
326+
.unwrap();
327+
assert_eq!("hello world", command_string);
328+
assert_eq!("hello", command.program);
329+
assert_eq!(vec!["hello", "world"], command.argv);
320330
}
321331

322332
#[test]
323333
fn parse_command_strips_shell_quotes() {
324-
let p = parse_command(" find . -type \"f\" -name '*.rs' ".to_string()).unwrap().unwrap();
325-
assert_eq!("find", p.program);
326-
assert_eq!(vec!["find", ".", "-type", "f", "-name", "*.rs"], p.argv);
334+
let (command, command_string) =
335+
parse_command(" find . -type \"f\" -name '*.rs' ".to_string())
336+
.unwrap()
337+
.unwrap();
338+
assert_eq!("find . -type \"f\" -name '*.rs'", command_string);
339+
assert_eq!("find", command.program);
340+
assert_eq!(
341+
vec!["find", ".", "-type", "f", "-name", "*.rs"],
342+
command.argv
343+
);
327344
}
328345
}

0 commit comments

Comments
 (0)