Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colored for error messages and add WrappedCommandError #403

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ git2 = "0.15.0"
# included to build PyPi Wheels (see .github/workflow/README.md)
openssl = { version = "0.10", features = ["vendored"], optional = true }
pep440_rs = { git = "https://github.com/konstin/pep440-rs", rev = "3148c9016cbc01a9e6116ae8080b10e14e985487", version = "0.1.1" }
colored = "2.0.0"


[dev-dependencies]
Expand Down
15 changes: 12 additions & 3 deletions src/bin/huak/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::process::ExitCode;
use clap::Parser;

mod commands;
use colored::Colorize;
use commands::Cli;
mod errors;

Expand All @@ -15,9 +16,17 @@ pub fn main() -> ExitCode {

match cli.run() {
Ok(_) => ExitCode::SUCCESS,
Err(err) => {
eprintln!("{err}");
err.exit_code
Err(it) => {
// TODO: Still want to return sterr from wrapped commands which return non-zero error
// codes. Will revisit. Ideally if you HUAK_MUTE_COMMAND=1 you're expecting to
// ignore errors from wrapped commands, but I haven't done enough "integration"
// testing with huak to feel comfortable with that.
if it.error.to_string().is_empty() {
eprintln!("{}", it.error);
} else {
eprintln!("{}{} {}", "error".red(), ":".bold(), it.error);
}
it.exit_code
}
}
}
50 changes: 26 additions & 24 deletions src/huak/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@ pub type HuakResult<T> = Result<T, HuakError>;

#[derive(Error, Debug)]
pub enum HuakError {
#[error("A Clap error occurred: {0}.")]
#[error("A Clap error occurred: {0}")]
ClapError(#[from] clap::Error),
#[error("{0} already exists.")]
#[error("{0} already exists")]
DirectoryExists(PathBuf),
#[error("Expected environment variable not found: {0}.")]
#[error("Expected environment variable not found: {0}")]
EnvVarError(#[from] std::env::VarError),
#[error("A pseudo-terminal error occurred: {0}.")]
#[error("A pseudo-terminal error occurred: {0}")]
ExpectrlError(#[from] expectrl::Error),
#[error("A formatter error occurred: {0}.")]
#[error("A formatter error occurred: {0}")]
FormatterError(String),
#[error("A Git error occurred: {0}.")]
#[error("A Git error occurred: {0}")]
GitError(#[from] git2::Error),
#[error("An HTTP error occurred: {0}.")]
#[error("An HTTP error occurred: {0}")]
HTTPError(#[from] reqwest::Error),
#[error("A Huak configuration error occurred: {0}.")]
#[error("A Huak configuration error occurred: {0}")]
HuakConfigurationError(String),
#[error("An internal error occurred: {0}.")]
#[error("An internal error occurred: {0}")]
InternalError(String),
#[error("An IO error occurred: {0}.")]
#[error("An IO error occurred: {0}")]
IOError(#[from] io::Error),
#[error("A linter error occurred: {0}.")]
#[error("A linter error occurred: {0}")]
LinterError(String),
#[error("Failed to build the project.")]
#[error("Failed to build the project")]
PyPackageBuildError,
#[error("A package index error occurred: {0}.")]
#[error("A package index error occurred: {0}")]
PyPackageIndexError(String),
#[error("Failed to initialize Python package: {0}.")]
#[error("Failed to initialize Python package: {0}")]
PyPackageInitalizationError(String),
#[error("Failed to install Python package: {0}.")]
#[error("Failed to install Python package: {0}")]
PyPackageInstallationError(String),
#[error("Invalid version operator: {0}.")]
#[error("Invalid version operator: {0}")]
PyPackageInvalidVersionOperator(String),
#[error("Invalid version: {0}.")]
#[error("Invalid version: {0}")]
PyPackageInvalidVersion(String),
#[error("Unable to build the version specifier.")]
#[error("Unable to build the version specifier")]
PyPackageVersionSpecifierError,
#[error(
"The project's manifest file could not be found. \
Currently only pyproject.toml files are supported."
)]
PyProjectFileNotFound,
#[error("A pyproject.toml already exists.")]
#[error("A pyproject.toml already exists")]
PyProjectTomlExistsError,
#[error("Failed to find the project's version.")]
#[error("Failed to find the project's version")]
PyProjectVersionNotFound,
#[error(
"Python was not found on your operating system. Please \
Expand All @@ -62,12 +62,14 @@ install Python (https://www.python.org/)."
PythonNotFoundError,
#[error("No venv was found.")]
PyVenvNotFoundError,
#[error("A testing error occurred: {0}.")]
#[error("A testing error occurred: {0}")]
TestingError(String),
#[error("Failed to deserialize toml: {0}.")]
#[error("Failed to deserialize toml: {0}")]
TOMLDeserializationError(#[from] toml_edit::de::Error),
#[error("Failed to serialize toml: {0}.")]
#[error("Failed to serialize toml: {0}")]
TOMLSerializationError(#[from] toml_edit::ser::Error),
#[error("A UTF-8 error occurred: {0}.")]
#[error("A UTF-8 error occurred: {0}")]
UTF8Error(#[from] std::str::Utf8Error),
#[error("{0}")]
WrappedCommandError(String),
}
18 changes: 10 additions & 8 deletions src/huak/utils/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ pub(crate) fn run_command(
from: &Path,
) -> HuakResult<(i32, String)> {
let (code, msg) = match should_mute() {
true => run_command_with_output(cmd, args, from)?,
false => run_command_with_spawn(cmd, args, from)?,
true => run_command_quiet(cmd, args, from)?,
false => run_command_noisy(cmd, args, from)?,
};

if code != 0 {
// TODO: Capture status codes.
return Err(HuakError::InternalError(msg));
return Err(HuakError::WrappedCommandError(msg));
}

Ok((code, msg))
}

/// Mute command utilities with HUAK_MUTE_COMMAND ("True", "true").
fn should_mute() -> bool {
let _mute = match env::var("HUAK_MUTE_COMMAND") {
let mute = match env::var("HUAK_MUTE_COMMAND") {
Ok(m) => m,
Err(_) => "False".to_string(),
};

matches!(_mute.as_str(), "TRUE" | "True" | "true" | "1")
matches!(mute.as_str(), "TRUE" | "True" | "true" | "1")
}

/// Run initialized command with .output() to mute stdout.
fn run_command_with_output(
fn run_command_quiet(
cmd: &str,
args: &[&str],
from: &Path,
Expand All @@ -55,7 +55,7 @@ fn run_command_with_output(
}

/// Run command and capture entire stdout.
fn run_command_with_spawn(
fn run_command_noisy(
cmd: &str,
args: &[&str],
from: &Path,
Expand All @@ -71,7 +71,9 @@ fn run_command_with_spawn(
let status = match child.try_wait() {
Ok(Some(s)) => s,
Ok(None) => child.wait()?,
Err(e) => return Err(HuakError::from(e)),
Err(e) => {
return Err(HuakError::from(e));
}
};

// TODO: Capture through spawn.
Expand Down