eldiron_cli/
common.rs

1use std::{fs::remove_dir_all, path::Path, process::Output};
2
3use anyhow::{bail, Error, Result};
4use console::style;
5use which::which;
6
7pub fn remove_dir_if_exist<P>(dir: P) -> Result<bool>
8where
9    P: AsRef<Path>,
10{
11    if dir.as_ref().exists() {
12        remove_dir_all(dir)?;
13
14        return Ok(true);
15    }
16
17    Ok(false)
18}
19
20pub fn has_installed(bin_name: &str) -> bool {
21    which(bin_name).is_ok()
22}
23
24pub fn pipe_exec_err(output: Output) -> Result<()> {
25    if output.status.success() {
26        Ok(())
27    } else {
28        bail!(String::from_utf8_lossy(&output.stderr).to_string())
29    }
30}
31
32pub fn pipe_exec_output(output: Output) -> Result<String> {
33    if output.status.success() {
34        let mut out = String::from_utf8_lossy(&output.stdout).to_string();
35
36        out.retain(|c| c != '\n');
37
38        Ok(out)
39    } else {
40        bail!(String::from_utf8_lossy(&output.stderr).to_string())
41    }
42}
43
44pub fn print_err(err: Error) {
45    println!();
46    println!("{}", style("Eldiron Cli exited with an error").red());
47    println!("{}", style(err).red());
48}
49
50pub fn welcome() {
51    println!("Welcome to Eldiron Cli");
52    println!("Version: {}", env!("CARGO_PKG_VERSION"));
53    println!();
54}