summaryrefslogtreecommitdiffstats
path: root/src/modules/python.rs
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/modules/python.rs
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/modules/python.rs')
-rw-r--r--src/modules/python.rs34
1 files changed, 7 insertions, 27 deletions
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,
}
}