1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 15:56:28 +00:00

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.
This commit is contained in:
Thomas O'Donnell 2019-12-02 23:42:55 +01:00 committed by Matan Kushner
parent 8b6e657d6c
commit edc62f4518
6 changed files with 31 additions and 92 deletions

View File

@ -1,8 +1,7 @@
use std::process::Command;
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::go::GoConfig; use crate::configs::go::GoConfig;
use crate::utils;
/// Creates a module with the current Go version /// 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.set_style(config.style);
module.create_segment("symbol", &config.symbol); 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)); module.create_segment("version", &config.version.with_value(&formatted_version));
Some(module) 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> { fn format_go_version(go_stdout: &str) -> Option<String> {
// go version output looks like this: // go version output looks like this:
// go version go1.13.3 linux/amd64 // go version go1.13.3 linux/amd64

View File

@ -1,10 +1,9 @@
use crate::configs::java::JavaConfig; use crate::configs::java::JavaConfig;
use std::process::Command;
use std::process::Output;
use super::{Context, Module, RootModuleConfig, SegmentConfig}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::modules::utils::java_version_parser; use crate::modules::utils::java_version_parser;
use crate::utils;
/// Creates a module with the current Java version /// Creates a module with the current Java version
/// ///
@ -44,20 +43,8 @@ fn get_java_version() -> Option<String> {
Err(_) => String::from("java"), Err(_) => String::from("java"),
}; };
match Command::new(java_command).arg("-Xinternalversion").output() { let output = utils::exec_cmd(&java_command.as_str(), &["-Xinternalversion"])?;
Ok(output) => Some(combine_outputs(output)), Some(format!("{}{}", output.stdout, output.stderr))
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)
} }
/// Extract the java version from `java_out`. /// Extract the java version from `java_out`.

View File

@ -1,8 +1,7 @@
use std::process::Command;
use super::{Context, Module, RootModuleConfig, SegmentConfig}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::nodejs::NodejsConfig; use crate::configs::nodejs::NodejsConfig;
use crate::utils;
/// Creates a module with the current Node.js version /// Creates a module with the current Node.js version
/// ///
@ -22,8 +21,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
match get_node_version() { let node_version = utils::exec_cmd("node", &["--version"])?.stdout;
Some(node_version) => {
let mut module = context.new_module("nodejs"); let mut module = context.new_module("nodejs");
let config: NodejsConfig = NodejsConfig::try_load(module.config); let config: NodejsConfig = NodejsConfig::try_load(module.config);
@ -35,13 +34,3 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
None => None,
}
}
fn get_node_version() -> Option<String> {
match Command::new("node").arg("--version").output() {
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
Err(_) => None,
}
}

View File

@ -1,9 +1,9 @@
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::process::Command;
use super::{Context, Module, RootModuleConfig, SegmentConfig}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::python::PythonConfig; use crate::configs::python::PythonConfig;
use crate::utils;
/// Creates a module with the current Python version /// 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); module.create_segment("symbol", &config.symbol);
if config.pyenv_version_name { 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("pyenv_prefix", &config.pyenv_prefix);
module.create_segment("version", &SegmentConfig::new(&python_version.trim())); module.create_segment("version", &SegmentConfig::new(&python_version.trim()));
} else { } else {
@ -59,36 +59,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) 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> { fn get_python_version() -> Option<String> {
match Command::new("python").arg("--version").output() { match utils::exec_cmd("python", &["--version"]) {
Ok(output) => { Some(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
if output.stdout.is_empty() { if output.stdout.is_empty() {
let stderr_string = String::from_utf8(output.stderr).unwrap(); Some(output.stderr)
Some(stderr_string)
} else { } else {
let stdout_string = String::from_utf8(output.stdout).unwrap(); Some(output.stdout)
Some(stdout_string)
} }
} }
Err(_) => None, None => None,
} }
} }

View File

@ -1,8 +1,7 @@
use std::process::Command;
use super::{Context, Module, RootModuleConfig, SegmentConfig}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::ruby::RubyConfig; use crate::configs::ruby::RubyConfig;
use crate::utils;
/// Creates a module with the current Ruby version /// Creates a module with the current Ruby version
/// ///
@ -20,7 +19,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; 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 formatted_version = format_ruby_version(&ruby_version)?;
let mut module = context.new_module("ruby"); let mut module = context.new_module("ruby");
@ -33,13 +32,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) 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> { fn format_ruby_version(ruby_version: &str) -> Option<String> {
let version = ruby_version let version = ruby_version
// split into ["ruby", "2.6.0p0", "linux/amd64"] // split into ["ruby", "2.6.0p0", "linux/amd64"]

View File

@ -1,9 +1,9 @@
use std::env; use std::env;
use std::process::Command;
use super::{Context, Module, RootModuleConfig, SegmentConfig}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::username::UsernameConfig; use crate::configs::username::UsernameConfig;
use crate::utils;
/// Creates a module with the current user's username /// 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> { fn get_uid() -> Option<u32> {
match Command::new("id").arg("-u").output() { utils::exec_cmd("id", &["-u"])?
Ok(output) => String::from_utf8(output.stdout) .stdout
.map(|uid| uid.trim().parse::<u32>().ok()) .trim()
.ok()?, .parse::<u32>()
Err(_) => None, .ok()
}
} }