diff --git a/src/modules/java.rs b/src/modules/java.rs index 2829b98c..754fe89a 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 { }) .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 e3d39a44..fa3961ce 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[\\d\\.]+[\\d\\.]+[\\d\\.]+)"; @@ -60,17 +61,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { } fn get_kotlin_version(context: &Context, kotlin_binary: &str) -> Option { - 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 { diff --git a/src/modules/lua.rs b/src/modules/lua.rs index ec9e4b8c..489e59c5 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> { @@ -57,12 +58,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { } fn get_lua_version(context: &Context, lua_binary: &str) -> Option { - 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 d8354b49..7f01e4b7 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> { @@ -73,13 +74,7 @@ fn get_python_version(context: &Context, config: &PythonConfig) -> Option(context: &'a Context) -> Option> { let mut module = context.new_module("rlang"); @@ -55,7 +56,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { } fn get_r_version(context: &Context) -> Option { - 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 b88d5684..ffc87dc2 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> { let mut module = context.new_module("scala"); @@ -56,13 +57,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { } fn get_scala_version(context: &Context) -> Option { - 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 5564f34b..c5038994 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,12 +18,21 @@ pub fn read_file + Debug>(file_name: P) -> Result { 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"); + } }