diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 53 |
1 files changed, 45 insertions, 8 deletions
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<String, String> { + 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); + } + } } |