Skip to content

Commit

Permalink
Use ErrorWithExitCode in the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Mar 2, 2023
1 parent c1751a1 commit 2cbce5f
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 41 deletions.
5 changes: 3 additions & 2 deletions scarb/src/bin/scarb/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::{anyhow, Result};
use anyhow::anyhow;

use scarb::core::Config;
use scarb::manifest_editor::{AddDependency, DepId, EditManifestOptions, Op};
use scarb::{manifest_editor, ops};

use crate::args::{AddArgs, AddSourceArgs};
use crate::errors::ScarbResult;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: AddArgs, config: &mut Config) -> Result<()> {
pub fn run(args: AddArgs, config: &mut Config) -> ScarbResult<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;

// TODO(#127): Extract more generic pattern for this. See `Packages` struct in Cargo.
Expand Down
7 changes: 3 additions & 4 deletions scarb/src/bin/scarb/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;

use crate::errors::ScarbResult;
use scarb::core::Config;
use scarb::ops;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(config: &Config) -> Result<()> {
pub fn run(config: &Config) -> ScarbResult<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;
ops::compile(&ws)
ops::compile(&ws).map_err(Into::into)
}
6 changes: 3 additions & 3 deletions scarb/src/bin/scarb/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use scarb::core::Config;

use crate::errors::ScarbResult;
use scarb::ops;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(config: &Config) -> Result<()> {
ops::clean(config)
pub fn run(config: &Config) -> ScarbResult<()> {
ops::clean(config).map_err(Into::into)
}
4 changes: 2 additions & 2 deletions scarb/src/bin/scarb/commands/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use serde::{Serialize, Serializer};
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::{env, fmt, fs};

use crate::args::ScarbArgs;
use crate::errors::ScarbResult;
use scarb::core::Config;
use scarb::process::is_executable;
use scarb::ui::Message;
Expand Down Expand Up @@ -86,7 +86,7 @@ fn list_commands(config: &Config, builtins: &BTreeMap<String, Option<String>>) -
}

#[tracing::instrument(skip_all, level = "info")]
pub fn run(config: &Config) -> Result<()> {
pub fn run(config: &Config) -> ScarbResult<()> {
let builtins = ScarbArgs::get_builtin_subcommands();
config.ui().print(list_commands(config, &builtins));
Ok(())
Expand Down
7 changes: 4 additions & 3 deletions scarb/src/bin/scarb/commands/external.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::ffi::OsString;

use anyhow::{anyhow, Result};
use anyhow::anyhow;

use crate::errors::ScarbResult;
use scarb::core::Config;
use scarb::ops::execute_external_subcommand;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: Vec<OsString>, config: &Config) -> Result<()> {
pub fn run(args: Vec<OsString>, config: &Config) -> ScarbResult<()> {
assert!(!args.is_empty());
let cmd = &args[0]
.clone()
.into_string()
.map_err(|_| anyhow!("command name must be valid UTF-8"))?;
let args = &args.iter().skip(1).map(AsRef::as_ref).collect::<Vec<_>>();
execute_external_subcommand(cmd, args, config)
execute_external_subcommand(cmd, args, config).map_err(Into::into)
}
15 changes: 8 additions & 7 deletions scarb/src/bin/scarb/commands/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use anyhow::Result;

use crate::args::FmtArgs;
use crate::errors::{ErrorWithExitCode, ScarbResult};
use scarb::core::Config;
use scarb::ops;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: FmtArgs, config: &Config) -> Result<()> {
pub fn run(args: FmtArgs, config: &Config) -> ScarbResult<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;
match ops::format(

if ops::format(
ops::FmtOptions {
check: args.check,
pkg_name: args.package,
color: !args.no_color,
},
&ws,
) {
Ok(true) => Ok(()),
_ => std::process::exit(1),
)? {
Ok(())
} else {
Err(ErrorWithExitCode::code(1))
}
}
5 changes: 3 additions & 2 deletions scarb/src/bin/scarb/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::env;

use anyhow::{anyhow, Result};
use anyhow::anyhow;
use camino::Utf8PathBuf;

use scarb::core::Config;
use scarb::ops::{self, VersionControl};

use crate::args::InitArgs;
use crate::errors::ScarbResult;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: InitArgs, config: &Config) -> Result<()> {
pub fn run(args: InitArgs, config: &Config) -> ScarbResult<()> {
let path = Utf8PathBuf::from_path_buf(env::current_dir()?)
.map_err(|path| anyhow!("path `{}` is not UTF-8 encoded", path.display()))?;

Expand Down
5 changes: 2 additions & 3 deletions scarb/src/bin/scarb/commands/manifest_path.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;

use crate::errors::ScarbResult;
use scarb::core::Config;
use scarb::ui::ValueMessage;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(config: &Config) -> Result<()> {
pub fn run(config: &Config) -> ScarbResult<()> {
let canonical = dunce::canonicalize(config.manifest_path())
.unwrap_or_else(|_| config.manifest_path().into());
let canonical = canonical.to_string_lossy();
Expand Down
5 changes: 2 additions & 3 deletions scarb/src/bin/scarb/commands/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use anyhow::Result;

use scarb::core::Config;
use scarb::metadata::{Metadata, MetadataOptions};
use scarb::ops;
use scarb::ui::MachineMessage;

use crate::args::MetadataArgs;
use crate::errors::ScarbResult;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: MetadataArgs, config: &Config) -> Result<()> {
pub fn run(args: MetadataArgs, config: &Config) -> ScarbResult<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;

let opts = MetadataOptions {
Expand Down
5 changes: 2 additions & 3 deletions scarb/src/bin/scarb/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![allow(clippy::module_inception)]

use anyhow::Result;

use scarb::core::Config;

use crate::args::Command;
use crate::errors::ScarbResult;

pub mod add;
pub mod build;
Expand All @@ -18,7 +17,7 @@ pub mod metadata;
pub mod new;
pub mod remove;

pub fn run(command: Command, config: &mut Config) -> Result<()> {
pub fn run(command: Command, config: &mut Config) -> ScarbResult<()> {
use Command::*;

match command {
Expand Down
5 changes: 2 additions & 3 deletions scarb/src/bin/scarb/commands/new.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anyhow::Result;

use scarb::core::Config;
use scarb::ops::{self, VersionControl};

use crate::args::NewArgs;
use crate::errors::ScarbResult;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: NewArgs, config: &Config) -> Result<()> {
pub fn run(args: NewArgs, config: &Config) -> ScarbResult<()> {
let result = ops::new_package(
ops::InitOptions {
name: args.init.name,
Expand Down
5 changes: 3 additions & 2 deletions scarb/src/bin/scarb/commands/remove.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::{anyhow, Result};
use anyhow::anyhow;

use scarb::core::{Config, PackageName};
use scarb::manifest_editor::{EditManifestOptions, Op, RemoveDependency};
use scarb::{manifest_editor, ops};

use crate::args::RemoveArgs;
use crate::errors::ScarbResult;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: RemoveArgs, config: &mut Config) -> Result<()> {
pub fn run(args: RemoveArgs, config: &mut Config) -> ScarbResult<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;

// TODO(#127): Extract more generic pattern for this. See `Packages` struct in Cargo.
Expand Down
19 changes: 15 additions & 4 deletions scarb/src/bin/scarb/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::env;

use anyhow::Result;
use clap::Parser;
use tracing::debug;
use tracing_log::AsTrace;
use tracing_subscriber::EnvFilter;

use crate::errors::{ErrorWithExitCode, ScarbResult};
use args::ScarbArgs;
use scarb::core::Config;
use scarb::ops;
use scarb::ui::Ui;

mod args;
mod commands;
mod errors;

fn main() {
let args = ScarbArgs::parse();
Expand All @@ -30,12 +32,21 @@ fn main() {
let ui = Ui::new(args.ui_verbosity(), args.output_format());

if let Err(err) = cli_main(args) {
ui.anyhow(&err);
std::process::exit(1);
exit_with_error(err, &ui);
}
}

fn cli_main(args: ScarbArgs) -> Result<()> {
fn exit_with_error(err: ErrorWithExitCode, ui: &Ui) {
debug!("exit_with_error; err={:?}", err);

let ErrorWithExitCode { source, exit_code } = err;
if let Some(source_err) = source {
ui.anyhow(&source_err);
}
std::process::exit(exit_code);
}

fn cli_main(args: ScarbArgs) -> ScarbResult<()> {
let ui_verbosity = args.ui_verbosity();
let ui_output_format = args.output_format();

Expand Down

0 comments on commit 2cbce5f

Please sign in to comment.