diff --git a/src/modules/golang.rs b/src/modules/golang.rs index 325c3394..e3e42b35 100644 --- a/src/modules/golang.rs +++ b/src/modules/golang.rs @@ -1,8 +1,7 @@ -use std::process::Command; - use super::{Context, Module, RootModuleConfig}; use crate::configs::go::GoConfig; +use crate::utils; /// Creates a module with the current Go version /// @@ -32,20 +31,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { module.set_style(config.style); module.create_segment("symbol", &config.symbol); - let formatted_version = format_go_version(&get_go_version()?)?; + let formatted_version = + format_go_version(&utils::exec_cmd("go", &["version"])?.stdout.as_str())?; module.create_segment("version", &config.version.with_value(&formatted_version)); Some(module) } -fn get_go_version() -> Option { - Command::new("go") - .arg("version") - .output() - .ok() - .and_then(|output| String::from_utf8(output.stdout).ok()) -} - fn format_go_version(go_stdout: &str) -> Option { // go version output looks like this: // go version go1.13.3 linux/amd64 diff --git a/src/modules/java.rs b/src/modules/java.rs index 2f73a3c2..0b0feaac 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -1,10 +1,9 @@ use crate::configs::java::JavaConfig; -use std::process::Command; -use std::process::Output; use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::modules::utils::java_version_parser; +use crate::utils; /// Creates a module with the current Java version /// @@ -44,20 +43,8 @@ fn get_java_version() -> Option { Err(_) => String::from("java"), }; - match Command::new(java_command).arg("-Xinternalversion").output() { - Ok(output) => Some(combine_outputs(output)), - Err(_) => None, - } -} - -/// Combines the standard and error outputs. -/// -/// This is due some Java vendors using `STDERR` as the output. -fn combine_outputs(output: Output) -> String { - let std_out = String::from_utf8(output.stdout).unwrap(); - let std_err = String::from_utf8(output.stderr).unwrap(); - - format!("{}{}", std_out, std_err) + let output = utils::exec_cmd(&java_command.as_str(), &["-Xinternalversion"])?; + Some(format!("{}{}", output.stdout, output.stderr)) } /// Extract the java version from `java_out`. diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 8f9bc1f4..5bd31435 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -1,8 +1,7 @@ -use std::process::Command; - use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::configs::nodejs::NodejsConfig; +use crate::utils; /// Creates a module with the current Node.js version /// @@ -22,26 +21,16 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - match get_node_version() { - Some(node_version) => { - let mut module = context.new_module("nodejs"); - let config: NodejsConfig = NodejsConfig::try_load(module.config); + let node_version = utils::exec_cmd("node", &["--version"])?.stdout; - module.set_style(config.style); + let mut module = context.new_module("nodejs"); + let config: NodejsConfig = NodejsConfig::try_load(module.config); - let formatted_version = node_version.trim(); - module.create_segment("symbol", &config.symbol); - module.create_segment("version", &SegmentConfig::new(formatted_version)); + module.set_style(config.style); - Some(module) - } - None => None, - } -} - -fn get_node_version() -> Option { - match Command::new("node").arg("--version").output() { - Ok(output) => Some(String::from_utf8(output.stdout).unwrap()), - Err(_) => None, - } + let formatted_version = node_version.trim(); + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(formatted_version)); + + Some(module) } diff --git a/src/modules/python.rs b/src/modules/python.rs index 491ad89a..366fd68b 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -1,9 +1,9 @@ use std::env; use std::path::Path; -use std::process::Command; use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::configs::python::PythonConfig; +use crate::utils; /// Creates a module with the current Python version /// @@ -40,7 +40,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { module.create_segment("symbol", &config.symbol); if config.pyenv_version_name { - let python_version = get_pyenv_version()?; + let python_version = utils::exec_cmd("pyenv", &["version-name"])?.stdout; module.create_segment("pyenv_prefix", &config.pyenv_prefix); module.create_segment("version", &SegmentConfig::new(&python_version.trim())); } else { @@ -59,36 +59,16 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn get_pyenv_version() -> Option { - Command::new("pyenv") - .arg("version-name") - .output() - .ok() - .and_then(|output| String::from_utf8(output.stdout).ok()) -} - fn get_python_version() -> Option { - match Command::new("python").arg("--version").output() { - Ok(output) => { - if !output.status.success() { - log::warn!( - "Non-Zero exit code '{}' when executing `python --version`", - output.status - ); - return None; - } - // We have to check both stdout and stderr since for Python versions - // < 3.4, Python reports to stderr and for Python version >= 3.5, - // Python reports to stdout + match utils::exec_cmd("python", &["--version"]) { + Some(output) => { if output.stdout.is_empty() { - let stderr_string = String::from_utf8(output.stderr).unwrap(); - Some(stderr_string) + Some(output.stderr) } else { - let stdout_string = String::from_utf8(output.stdout).unwrap(); - Some(stdout_string) + Some(output.stdout) } } - Err(_) => None, + None => None, } } diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs index 247b941e..8556cb93 100644 --- a/src/modules/ruby.rs +++ b/src/modules/ruby.rs @@ -1,8 +1,7 @@ -use std::process::Command; - use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::configs::ruby::RubyConfig; +use crate::utils; /// Creates a module with the current Ruby version /// @@ -20,7 +19,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let ruby_version = get_ruby_version()?; + let ruby_version = utils::exec_cmd("ruby", &["-v"])?.stdout; let formatted_version = format_ruby_version(&ruby_version)?; let mut module = context.new_module("ruby"); @@ -33,13 +32,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn get_ruby_version() -> Option { - match Command::new("ruby").arg("-v").output() { - Ok(output) => Some(String::from_utf8(output.stdout).unwrap()), - Err(_) => None, - } -} - fn format_ruby_version(ruby_version: &str) -> Option { let version = ruby_version // split into ["ruby", "2.6.0p0", "linux/amd64"] diff --git a/src/modules/username.rs b/src/modules/username.rs index e89336bb..495d54d6 100644 --- a/src/modules/username.rs +++ b/src/modules/username.rs @@ -1,9 +1,9 @@ use std::env; -use std::process::Command; use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::configs::username::UsernameConfig; +use crate::utils; /// Creates a module with the current user's username /// @@ -38,10 +38,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { } fn get_uid() -> Option { - match Command::new("id").arg("-u").output() { - Ok(output) => String::from_utf8(output.stdout) - .map(|uid| uid.trim().parse::().ok()) - .ok()?, - Err(_) => None, - } + utils::exec_cmd("id", &["-u"])? + .stdout + .trim() + .parse::() + .ok() }