Skip to content

Commit

Permalink
chore: update error messages when installation command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nokazn committed Apr 9, 2024
1 parent 801446c commit 029faea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub enum Error {
#[error("Invalid glob pattern: {:?}", .0)]
InvalidGlobPattern(&'static str),

#[error("Failed to install dependencies by `{}` at: `{:?}`", stringify_install_command(.0), .1)]
FailedToInstallDependencies(PackageManager, PathBuf),
#[error("Failed to install dependencies by `{}` at `{:?}`: {}", stringify_install_command(.0), .1, .2)]
FailedToInstallDependencies(PackageManager, PathBuf, String),

#[error("Error: {:?}", .0)]
Any(String),
Expand Down
19 changes: 10 additions & 9 deletions src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ use std::{path::Path, process::Command, vec};
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;

use crate::{
core::Result,
errors::{to_error, Error},
utils::path::to_absolute_path,
};
use crate::{core::Result, errors::Error};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct PackageManager {
pub executable_name: String,
pub install_sub_command: String,
Expand Down Expand Up @@ -52,20 +48,25 @@ impl PackageManager {
}

pub fn execute_install(self, base_dir: impl AsRef<Path>) -> Result<()> {
let base_dir = to_absolute_path(base_dir)?;
let base_dir = base_dir.as_ref().to_path_buf();
let to_error =
|message: String| Error::FailedToInstallDependencies(self.clone(), base_dir.clone(), message);
let output = Command::new(&self.executable_name)
.arg(&self.install_sub_command)
.current_dir(&base_dir)
.output()
.map_err(to_error)?;
.map_err(|error| to_error(error.to_string()))?;

// TODO: stream
let text = String::from_utf8_lossy(&output.stdout);
println!("{}", text);

if output.status.success() {
Ok(())
} else {
Err(Error::FailedToInstallDependencies(self, base_dir))
Err(to_error(
String::from_utf8_lossy(&output.stderr).to_string(),
))
}
}
}
Expand Down

0 comments on commit 029faea

Please sign in to comment.