From 8de695eb3cd8a1ed6eb78550b18914856e15f775 Mon Sep 17 00:00:00 2001 From: Henrique Alves Date: Mon, 25 Jul 2022 15:44:54 -0300 Subject: Update package commit --- src/utils.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 271cf6d..e925f9d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,26 @@ +use colored::Colorize; +use std::io::Write; + +pub fn log_warning(msg: &str) { + let msg = format!("⚠️ {}", msg).yellow(); + println!("{}", msg); +} + +pub fn log_error(msg: &str) { + let msg = format!("❌ {}", msg).red(); + println!("{}", msg); +} + +pub fn log_info(msg: &str) { + let msg = format!("ℹ️ {}", msg).blue(); + println!("{}", msg); +} + +pub fn log_check(msg: &str) { + let msg = format!("✅ {}", msg).green(); + println!("{}", msg); +} + pub fn get_repo_name(repo: &str) -> String { let mut chars = repo.chars().rev(); let length = repo.chars().count(); @@ -5,7 +28,6 @@ pub fn get_repo_name(repo: &str) -> String { let mut first_i = 0; let mut i = length; - println!("{}", i); while i > 0 { match chars.next() { @@ -25,20 +47,35 @@ pub fn get_repo_name(repo: &str) -> String { last_i = length; } - println!("{} {}", first_i, last_i); let name = &repo[first_i..last_i]; - println!("{}", name); // TODO: Return a Result (may be error) return name.to_string(); } -pub fn run_shell_command(command : &str, folder : Option<&str>) -> bool { - let status = std::process::Command:: +pub fn run_shell_command(command : &str, folder : &str, verbose : bool) -> Result { + let output = std::process::Command:: new("sh") - .current_dir(folder.unwrap_or("./")) + .current_dir(folder) + .stdin(std::process::Stdio::inherit()) .arg("-c") .arg(command) - .status() + .output() .expect("Error running command."); - return status.success(); + + if verbose { + std::io::stdout().write_all(&output.stdout).unwrap(); + std::io::stderr().write_all(&output.stderr).unwrap(); + } + + match output.status.success() { + true => { + let stdout_str = String::from_utf8(output.stdout).unwrap(); + return Ok(stdout_str); + } + + false => { + let stderr_str = String::from_utf8(output.stderr).unwrap(); + return Err(stderr_str); + } + } } -- cgit v1.2.3