use crate::env;
fn launch_command(env: &env::DotenvFile, commands: &Vec<String>) -> Result<i32, Box<dyn std::error::Error>> {
if commands.len() == 0 {
return Err("ERROR: command is empty.".into());
}
let (command_str, args) = commands.split_first().unwrap();
let mut command = std::process::Command::new(&command_str);
command.args(args);
for (k, v) in &env.map {
command.env(k, v);
}
let result = command.spawn()?.wait()?;
if !result.success() {
let exit_code = result.code().unwrap();
return Ok(exit_code);
}
return Ok(0);
}
pub struct Application;
impl Application {
pub fn execute(&self, use_stdin: bool, env_file: Option<String>, commands: &Vec<String>) -> Result<i32, Box<dyn std::error::Error>> {
let dotenv = env::DotenvFile::configure(use_stdin, env_file)?;
return launch_command(&dotenv, &commands);
}
pub fn dump_variables(&self, use_stdin: bool, env_file: Option<String>) -> Result<(), Box<dyn std::error::Error>> {
let dotenv = env::DotenvFile::configure(use_stdin, env_file)?;
let map = dotenv.get_inner_map();
for (key, value) in map {
println!("{}={}", key, value);
}
return Ok(());
}
}