Add check_output
This commit is contained in:
@@ -176,3 +176,20 @@ impl Check for ExecutorExitStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extension methods for `std::process::Command`
|
||||||
|
pub trait CommandExt {
|
||||||
|
/// Run the command, wait for it to complete, check the return code and decode the output as UTF-8.
|
||||||
|
fn check_output(&mut self) -> Result<String, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandExt for Command {
|
||||||
|
fn check_output(&mut self) -> Result<String, Error> {
|
||||||
|
let output = self.output().context(ErrorKind::ProcessExecution)?;
|
||||||
|
let status = output.status;
|
||||||
|
if !status.success() {
|
||||||
|
Err(ErrorKind::ProcessFailed(status))?
|
||||||
|
}
|
||||||
|
Ok(String::from_utf8(output.stdout).context(ErrorKind::ProcessExecution)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use crate::error::{Error, ErrorKind};
|
use crate::error::Error;
|
||||||
use crate::executor::RunType;
|
use crate::executor::{CommandExt, RunType};
|
||||||
use crate::terminal::print_separator;
|
use crate::terminal::print_separator;
|
||||||
use crate::utils::{self, Check, PathExt};
|
use crate::utils::{self, PathExt};
|
||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
use failure::ResultExt;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
@@ -202,16 +201,10 @@ pub fn run_custom_command(name: &str, command: &str, run_type: RunType) -> Resul
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
||||||
if let Some(composer) = utils::which("composer") {
|
if let Some(composer) = utils::which("composer") {
|
||||||
let composer_home = || -> Result<PathBuf, Error> {
|
let composer_home = Command::new(&composer)
|
||||||
let output = Command::new(&composer)
|
.args(&["global", "config", "--absolute", "home"])
|
||||||
.args(&["global", "config", "--absolute", "home"])
|
.check_output()
|
||||||
.output()
|
.map(PathBuf::from);
|
||||||
.context(ErrorKind::ProcessExecution)?;
|
|
||||||
output.status.check()?;
|
|
||||||
Ok(PathBuf::from(
|
|
||||||
&String::from_utf8(output.stdout).context(ErrorKind::ProcessExecution)?,
|
|
||||||
))
|
|
||||||
}();
|
|
||||||
|
|
||||||
if let Ok(composer_home) = composer_home {
|
if let Ok(composer_home) = composer_home {
|
||||||
if composer_home.is_descendant_of(base_dirs.home_dir()) {
|
if composer_home.is_descendant_of(base_dirs.home_dir()) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::executor::RunType;
|
use crate::executor::{CommandExt, RunType};
|
||||||
use crate::terminal::print_separator;
|
use crate::terminal::print_separator;
|
||||||
use crate::utils::which;
|
use crate::utils::which;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
@@ -38,15 +38,10 @@ impl Git {
|
|||||||
let output = Command::new(&git)
|
let output = Command::new(&git)
|
||||||
.args(&["rev-parse", "--show-toplevel"])
|
.args(&["rev-parse", "--show-toplevel"])
|
||||||
.current_dir(path)
|
.current_dir(path)
|
||||||
.output();
|
.check_output()
|
||||||
|
.ok()
|
||||||
if let Ok(output) = output {
|
.map(|output| output.trim().to_string());
|
||||||
if !output.status.success() {
|
return output;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => match e.kind() {
|
Err(e) => match e.kind() {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use crate::error::{Error, ErrorKind};
|
use crate::error::Error;
|
||||||
use crate::executor::RunType;
|
use crate::executor::{CommandExt, RunType};
|
||||||
use crate::terminal::print_separator;
|
use crate::terminal::print_separator;
|
||||||
use crate::utils::{which, Check, PathExt};
|
use crate::utils::{which, PathExt};
|
||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
use failure::ResultExt;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
@@ -17,16 +16,10 @@ impl NPM {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn root(&self) -> Result<PathBuf, Error> {
|
fn root(&self) -> Result<PathBuf, Error> {
|
||||||
let output = Command::new(&self.command)
|
Command::new(&self.command)
|
||||||
.args(&["root", "-g"])
|
.args(&["root", "-g"])
|
||||||
.output()
|
.check_output()
|
||||||
.context(ErrorKind::ProcessExecution)?;
|
.map(PathBuf::from)
|
||||||
|
|
||||||
output.status.check()?;
|
|
||||||
|
|
||||||
Ok(PathBuf::from(
|
|
||||||
&String::from_utf8(output.stdout).context(ErrorKind::ProcessExecution)?,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn upgrade(&self, run_type: RunType) -> Result<(), Error> {
|
fn upgrade(&self, run_type: RunType) -> Result<(), Error> {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
use crate::error::{Error, ErrorKind};
|
use crate::error::Error;
|
||||||
use crate::executor::RunType;
|
use crate::executor::{CommandExt, RunType};
|
||||||
use crate::terminal::print_separator;
|
use crate::terminal::print_separator;
|
||||||
use crate::utils::{self, which, Check};
|
use crate::utils::{self, which};
|
||||||
use failure::ResultExt;
|
|
||||||
use log::error;
|
use log::error;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
@@ -57,9 +56,7 @@ impl Powershell {
|
|||||||
|| -> Result<(), Error> {
|
|| -> Result<(), Error> {
|
||||||
Command::new(&powershell)
|
Command::new(&powershell)
|
||||||
.args(&["-Command", &format!("Get-Command {}", command)])
|
.args(&["-Command", &format!("Get-Command {}", command)])
|
||||||
.output()
|
.check_output()?;
|
||||||
.context(ErrorKind::ProcessExecution)?
|
|
||||||
.check()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}()
|
}()
|
||||||
.is_ok()
|
.is_ok()
|
||||||
@@ -67,16 +64,10 @@ impl Powershell {
|
|||||||
|
|
||||||
pub fn profile(&self) -> Option<PathBuf> {
|
pub fn profile(&self) -> Option<PathBuf> {
|
||||||
if let Some(powershell) = &self.path {
|
if let Some(powershell) = &self.path {
|
||||||
let result = || -> Result<PathBuf, Error> {
|
let result = Command::new(powershell)
|
||||||
let output = Command::new(powershell)
|
.args(&["-Command", "echo $profile"])
|
||||||
.args(&["-Command", "echo $profile"])
|
.check_output()
|
||||||
.output()
|
.map(PathBuf::from);
|
||||||
.context(ErrorKind::ProcessExecution)?;
|
|
||||||
output.status.check()?;
|
|
||||||
Ok(PathBuf::from(
|
|
||||||
String::from_utf8_lossy(&output.stdout).trim().to_string(),
|
|
||||||
))
|
|
||||||
}();
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Err(e) => error!("Error getting Powershell profile: {}", e),
|
Err(e) => error!("Error getting Powershell profile: {}", e),
|
||||||
|
|||||||
Reference in New Issue
Block a user