summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThomas O'Donnell <andytom@users.noreply.github.com>2019-12-02 23:42:55 +0100
committerMatan Kushner <hello@matchai.me>2019-12-02 17:42:55 -0500
commitedc62f45188b742a4d20b601170ba8b9bdb5c803 (patch)
tree7290f5942bfcfa63a826d175bb28443d2bb574fa /src
parent8b6e657d6c969d6a696694af3903235a41d92bd3 (diff)
refactor: Refactor modules to use the exec_cmd util (#676)
Have refactored the golang, java, nodejs, python, ruby and username modules to use the new `exec_cmd` util.
Diffstat (limited to 'src')
-rw-r--r--src/modules/golang.rs14
-rw-r--r--src/modules/java.rs19
-rw-r--r--src/modules/nodejs.rs29
-rw-r--r--src/modules/python.rs34
-rw-r--r--src/modules/ruby.rs12
-rw-r--r--src/modules/username.rs13
6 files changed, 30 insertions, 91 deletions
diff --git a/src/modules/golang.rs b/src/modules/golang.rs
index 325c3394a..e3e42b350 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<'a>> {
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<String> {
- Command::new("go")
- .arg("version")
- .output()
- .ok()
- .and_then(|output| String::from_utf8(output.stdout).ok())
-}
-
fn format_go_version(go_stdout: &str) -> Option<String> {
// 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 2f73a3c26..0b0feaaca 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<String> {
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 8f9bc1f43..5bd314354 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<Module<'a>> {
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,
- }
-}
+ let formatted_version = node_version.trim();
+ module.create_segment("symbol", &config.symbol);
+ module.create_segment("version", &SegmentConfig::new(formatted_version));
-fn get_node_version() -> Option<String> {
- match Command::new("node").arg("--version").output() {
- Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
- Err(_) => None,
- }
+ Some(module)
}
diff --git a/src/modules/python.rs b/src/modules/python.rs
index 491ad89a2..366fd68b5 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<'a>> {
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<Module<'a>> {
Some(module)
}
-fn get_pyenv_version() -> Option<String> {
- Command::new("pyenv")
- .arg("version-name")
- .output()
- .ok()
- .and_then(|output| String::from_utf8(output.stdout).ok())
-}
-
fn get_python_version() -> Option<String> {
- 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 247b941eb..8556cb939 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<Module<'a>> {
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<Module<'a>> {
Some(module)
}
-fn get_ruby_version() -> Option<String> {
- 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<String> {
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 e89336bb6..495d54d6a 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<Module<'a>> {
}
fn get_uid() -> Option<u32> {
- match Command::new("id").arg("-u").output() {
- Ok(output) => String::from_utf8(output.stdout)
- .map(|uid| uid.trim().parse::<u32>().ok())
- .ok()?,
- Err(_) => None,
- }
+ utils::exec_cmd("id", &["-u"])?
+ .stdout
+ .trim()
+ .parse::<u32>()
+ .ok()
}