Skip to main content

fluvio_system_util/
bin.rs

1use std::io::Error as IoError;
2use std::io::ErrorKind;
3use std::process::Command;
4
5use tracing::debug;
6
7pub fn get_fluvio() -> Result<Command, IoError> {
8    get_binary("fluvio")
9}
10
11/// get path to the binary
12pub fn get_binary(bin_name: &str) -> Result<Command, IoError> {
13    let current_exe =
14        std::env::current_exe().expect("Failed to get the path of the integration test binary");
15    let mut bin_dir = current_exe
16        .parent()
17        .expect("failed to get parent")
18        .to_owned();
19    bin_dir.push(bin_name);
20    bin_dir.set_extension(std::env::consts::EXE_EXTENSION);
21
22    debug!("try to get binary: {:#?}", bin_dir);
23    if !bin_dir.exists() {
24        Err(IoError::new(
25            ErrorKind::NotFound,
26            format!("{} not founded in: {:#?}", bin_name, bin_dir),
27        ))
28    } else {
29        Ok(Command::new(bin_dir.into_os_string()))
30    }
31}
32
33use std::fs::File;
34use std::process::Stdio;
35
36#[allow(unused)]
37pub fn open_log(prefix: &str) -> (File, File) {
38    let output = File::create(format!("/tmp/flv_{}.out", prefix)).expect("log file");
39    let error = File::create(format!("/tmp/flv_{}.log", prefix)).expect("err file");
40
41    (output, error)
42}
43
44#[allow(unused)]
45pub fn command_exec(binary: &str, prefix: &str, process: impl Fn(&mut Command)) {
46    let mut cmd = get_binary(binary).unwrap_or_else(|_| panic!("unable to get binary: {}", binary));
47
48    let (output, error) = open_log(prefix);
49
50    cmd.stdout(Stdio::from(output)).stderr(Stdio::from(error));
51
52    process(&mut cmd);
53
54    let status = cmd.status().expect("topic creation failed");
55
56    if !status.success() {
57        println!("topic creation failed: {}", status);
58    }
59}
60
61use std::process::Child;
62
63#[allow(unused)]
64pub fn command_spawn(binary: &str, prefix: &str, process: impl Fn(&mut Command)) -> Child {
65    let mut cmd = get_binary(binary).unwrap_or_else(|_| panic!("unable to get binary: {}", binary));
66
67    let (output, error) = open_log(prefix);
68
69    cmd.stdout(Stdio::from(output)).stderr(Stdio::from(error));
70
71    process(&mut cmd);
72
73    cmd.spawn().expect("child")
74}