1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-25 19:21:08 +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 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

View File

@ -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`.

View File

@ -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,
}
}
fn get_node_version() -> Option<String> {
match Command::new("node").arg("--version").output() {
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
Err(_) => None,
}
let formatted_version = node_version.trim();
module.create_segment("symbol", &config.symbol);
module.create_segment("version", &SegmentConfig::new(formatted_version));
Some(module)
}

View File

@ -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,
}
}

View File

@ -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"]

View File

@ -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()
}