Skip to content

Commit

Permalink
Pretty errors (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII authored Nov 12, 2022
1 parent 4b966aa commit 6cb4e3f
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 17 deletions.
158 changes: 154 additions & 4 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ humanode-distribution-installer = { path = "../installer" }
humanode-distribution-resolver = { path = "../resolver" }
humanode-distribution-schema = { path = "../schema" }

anyhow = "1"
clap = { version = "4", features = ["derive"] }
color-eyre = "0.6"
eyre = "0.6"
futures = "0.3"
reqwest = "0.11"
serde = "1.0"
Expand Down
30 changes: 20 additions & 10 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![allow(missing_docs)]
#![allow(clippy::missing_docs_in_private_items)]

use std::process::ExitCode;

use clap::{Args, Parser, Subcommand};
use humanode_distribution_resolver::resolve::Contextualized;
use humanode_distribution_schema::manifest::Package;
Expand Down Expand Up @@ -91,20 +93,28 @@ struct Install {
}

#[tokio::main]
async fn main() {
async fn main() -> ExitCode {
color_eyre::install().unwrap();
let cli = Cli::parse();

match cli.command {
Command::List(args) => list(args).await.unwrap(),
Command::Eval(args) => eval(args).await.unwrap(),
Command::Install(args) => install(args).await.unwrap(),
let result = match cli.command {
Command::List(args) => list(args).await,
Command::Eval(args) => eval(args).await,
Command::Install(args) => install(args).await,
};

if let Err(error) = result {
eprintln!("Error: {error:?}");
return ExitCode::FAILURE;
}

ExitCode::SUCCESS
}

/// Common CLI logic to run the resolver from the given args.
async fn resolve(
resolution_args: ResolutionArgs,
) -> Result<Vec<Contextualized<Package>>, anyhow::Error> {
) -> Result<Vec<Contextualized<Package>>, eyre::Error> {
let ResolutionArgs {
repo_urls,
manifest_urls,
Expand Down Expand Up @@ -145,7 +155,7 @@ async fn resolve(
fn select(
args: SelectionArgs,
packages: Vec<Contextualized<Package>>,
) -> Result<Contextualized<Package>, anyhow::Error> {
) -> Result<Contextualized<Package>, eyre::Error> {
let SelectionArgs {
package_display_name,
} = args;
Expand All @@ -159,7 +169,7 @@ fn select(
}

/// List command.
async fn list(args: List) -> Result<(), anyhow::Error> {
async fn list(args: List) -> Result<(), eyre::Error> {
let List {
resolution_args,
rendering_args,
Expand All @@ -175,7 +185,7 @@ async fn list(args: List) -> Result<(), anyhow::Error> {
}

/// Eval command.
async fn eval(args: Eval) -> Result<(), anyhow::Error> {
async fn eval(args: Eval) -> Result<(), eyre::Error> {
let Eval {
resolution_args,
selection_args,
Expand All @@ -189,7 +199,7 @@ async fn eval(args: Eval) -> Result<(), anyhow::Error> {
}

/// Install command.
async fn install(args: Install) -> Result<(), anyhow::Error> {
async fn install(args: Install) -> Result<(), eyre::Error> {
let Install {
resolution_args,
selection_args,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/package_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Renderer {
&self,
mut writer: impl std::io::Write,
package: &Package,
) -> Result<(), anyhow::Error> {
) -> Result<(), eyre::Error> {
match self {
Self::DisplayName => writer
.write_all(package.display_name.as_bytes())
Expand All @@ -33,7 +33,7 @@ impl Renderer {
}

/// Render the package into a string according to the renderer config.
pub fn render_to_string(&self, package: &Package) -> Result<String, anyhow::Error> {
pub fn render_to_string(&self, package: &Package) -> Result<String, eyre::Error> {
let mut buf = Vec::new();
self.render(&mut buf, package)?;
Ok(String::from_utf8(buf)?)
Expand Down

0 comments on commit 6cb4e3f

Please sign in to comment.