diff options
-rw-r--r-- | src/modules/java.rs | 9 | ||||
-rw-r--r-- | src/modules/kotlin.rs | 16 | ||||
-rw-r--r-- | src/modules/lua.rs | 9 | ||||
-rw-r--r-- | src/modules/python.rs | 9 | ||||
-rw-r--r-- | src/modules/rlang.rs | 3 | ||||
-rw-r--r-- | src/modules/scala.rs | 10 | ||||
-rw-r--r-- | src/utils.rs | 24 |
7 files changed, 41 insertions, 39 deletions
diff --git a/src/modules/java.rs b/src/modules/java.rs index 2829b98c7..754fe89ae 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -1,5 +1,6 @@ use crate::configs::java::JavaConfig; use crate::formatter::{StringFormatter, VersionFormatter}; +use crate::utils::get_command_string_output; use std::path::PathBuf; use super::{Context, Module, RootModuleConfig}; @@ -64,12 +65,8 @@ fn get_java_version(context: &Context, config: &JavaConfig) -> Option<String> { }) .unwrap_or_else(|| String::from("java")); - let output = context.exec_cmd(&java_command, &["-Xinternalversion"])?; - let java_version = if output.stdout.is_empty() { - output.stderr - } else { - output.stdout - }; + let command = context.exec_cmd(&java_command, &["-Xinternalversion"])?; + let java_version = get_command_string_output(command); format_java_version(&java_version, config.version_format) } diff --git a/src/modules/kotlin.rs b/src/modules/kotlin.rs index e3d39a449..fa3961ce7 100644 --- a/src/modules/kotlin.rs +++ b/src/modules/kotlin.rs @@ -3,6 +3,7 @@ use super::{Context, Module, RootModuleConfig}; use crate::configs::kotlin::KotlinConfig; use crate::formatter::StringFormatter; use crate::formatter::VersionFormatter; +use crate::utils::get_command_string_output; use regex::Regex; const KOTLIN_VERSION_PATTERN: &str = "(?P<version>[\\d\\.]+[\\d\\.]+[\\d\\.]+)"; @@ -60,17 +61,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { } fn get_kotlin_version(context: &Context, kotlin_binary: &str) -> Option<String> { - match context.exec_cmd(kotlin_binary, &["-version"]) { - Some(output) => { - let kotlin_output = if output.stdout.is_empty() { - output.stderr - } else { - output.stdout - }; - parse_kotlin_version(&kotlin_output) - } - None => None, - } + let command = context.exec_cmd(kotlin_binary, &["-version"])?; + let kotlin_version = get_command_string_output(command); + + parse_kotlin_version(&kotlin_version) } fn parse_kotlin_version(kotlin_stdout: &str) -> Option<String> { diff --git a/src/modules/lua.rs b/src/modules/lua.rs index ec9e4b8cf..489e59c5e 100644 --- a/src/modules/lua.rs +++ b/src/modules/lua.rs @@ -3,6 +3,7 @@ use super::{Context, Module, RootModuleConfig}; use crate::configs::lua::LuaConfig; use crate::formatter::StringFormatter; use crate::formatter::VersionFormatter; +use crate::utils::get_command_string_output; /// Creates a module with the current Lua version pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { @@ -57,12 +58,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { } fn get_lua_version(context: &Context, lua_binary: &str) -> Option<String> { - let output = context.exec_cmd(lua_binary, &["-v"])?; - let lua_version = if output.stdout.is_empty() { - output.stderr - } else { - output.stdout - }; + let command = context.exec_cmd(lua_binary, &["-v"])?; + let lua_version = get_command_string_output(command); parse_lua_version(&lua_version) } diff --git a/src/modules/python.rs b/src/modules/python.rs index d8354b497..7f01e4b73 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -5,6 +5,7 @@ use super::{Context, Module, RootModuleConfig}; use crate::configs::python::PythonConfig; use crate::formatter::StringFormatter; use crate::formatter::VersionFormatter; +use crate::utils::get_command_string_output; /// Creates a module with the current Python version and, if active, virtual environment. pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { @@ -73,13 +74,7 @@ fn get_python_version(context: &Context, config: &PythonConfig) -> Option<String .0 .iter() .find_map(|binary| context.exec_cmd(binary, &["--version"])) - .map(|output| { - if output.stdout.is_empty() { - output.stderr - } else { - output.stdout - } - })?; + .map(get_command_string_output)?; format_python_version(&version, config.version_format) } diff --git a/src/modules/rlang.rs b/src/modules/rlang.rs index e6cac083f..5128d79c2 100644 --- a/src/modules/rlang.rs +++ b/src/modules/rlang.rs @@ -3,6 +3,7 @@ use crate::formatter::VersionFormatter; use crate::configs::rlang::RLangConfig; use crate::formatter::StringFormatter; +use crate::utils::get_command_string_output; pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let mut module = context.new_module("rlang"); @@ -55,7 +56,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { } fn get_r_version(context: &Context) -> Option<String> { - let r_version = context.exec_cmd("R", &["--version"])?.stderr; + let r_version = get_command_string_output(context.exec_cmd("R", &["--version"])?); parse_version(&r_version) } diff --git a/src/modules/scala.rs b/src/modules/scala.rs index b88d56846..ffc87dc24 100644 --- a/src/modules/scala.rs +++ b/src/modules/scala.rs @@ -3,6 +3,7 @@ use crate::formatter::StringFormatter; use super::{Context, Module, RootModuleConfig}; use crate::formatter::VersionFormatter; +use crate::utils::get_command_string_output; pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let mut module = context.new_module("scala"); @@ -56,13 +57,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { } fn get_scala_version(context: &Context) -> Option<String> { - let output = context.exec_cmd("scalac", &["-version"])?; - let scala_version = if output.stdout.is_empty() { - output.stderr - } else { - output.stdout - }; - + let command = context.exec_cmd("scalac", &["-version"])?; + let scala_version = get_command_string_output(command); parse_scala_version(&scala_version) } diff --git a/src/utils.rs b/src/utils.rs index 5564f34b6..c50389946 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,12 +18,21 @@ pub fn read_file<P: AsRef<Path> + Debug>(file_name: P) -> Result<String> { if result.is_err() { log::debug!("Error reading file: {:?}", result); } else { - log::trace!("File read sucessfully"); + log::trace!("File read successfully"); }; result } +/// Reads command output from stderr or stdout depending on to which stream program streamed it's output +pub fn get_command_string_output(command: CommandOutput) -> String { + if command.stdout.is_empty() { + command.stderr + } else { + command.stdout + } +} + /// Attempt to resolve `binary_name` from and creates a new `Command` pointing at it /// This allows executing cmd files on Windows and prevents running executable from cwd on Windows /// This function also initialises std{err,out,in} to protect against processes changing the console mode @@ -631,4 +640,17 @@ mod tests { test ); } + #[test] + fn test_get_command_string_output() { + let case1 = CommandOutput { + stdout: String::from("stdout"), + stderr: String::from("stderr"), + }; + assert_eq!(get_command_string_output(case1), "stdout"); + let case2 = CommandOutput { + stdout: String::from(""), + stderr: String::from("stderr"), + }; + assert_eq!(get_command_string_output(case2), "stderr"); + } } |