diff options
author | Tom Milligan <tom@reinfer.io> | 2022-11-26 21:05:01 +0000 |
---|---|---|
committer | Tom Milligan <tommilligan@users.noreply.github.com> | 2022-11-26 22:36:49 +0000 |
commit | f9eb198cd00e1f9129b4018e3b8c28bf6ee1060c (patch) | |
tree | e4ec7684d9401a9a303692b8ad064ef0243aaaf6 | |
parent | 5530da074b08b8eccd3e1fc9c7c9d06e4a4b0025 (diff) |
bin: better error handling
-rwxr-xr-x | scripts/check | 2 | ||||
-rw-r--r-- | src/bin/mdbook-admonish.rs | 62 |
2 files changed, 33 insertions, 31 deletions
diff --git a/scripts/check b/scripts/check index 6d16424..90b0e14 100755 --- a/scripts/check +++ b/scripts/check @@ -15,10 +15,12 @@ cargo fmt -- --check # - RUSTSEC-2020-0071 known unlikely segfault in `time` # - RUSTSEC-2020-0016 `net2` is unmaintained # - RUSTSEC-2020-0159 known unlikely segfault in `chrono` +# - RUSTSEC-2021-0145 known unmaintained atty transitive dep eprintln "Auditing dependencies" cargo audit --deny warnings \ --ignore RUSTSEC-2020-0071 \ --ignore RUSTSEC-2020-0016 \ + --ignore RUSTSEC-2021-0145 \ --ignore RUSTSEC-2020-0159 eprintln "Linting sources" diff --git a/src/bin/mdbook-admonish.rs b/src/bin/mdbook-admonish.rs index 04b7441..87fa73d 100644 --- a/src/bin/mdbook-admonish.rs +++ b/src/bin/mdbook-admonish.rs @@ -1,3 +1,5 @@ +use anyhow::Result; +use clap::{Parser, Subcommand}; use mdbook::{ errors::Error, preprocess::{CmdPreprocessor, Preprocessor}, @@ -7,8 +9,6 @@ use mdbook_admonish::Admonish; use std::path::PathBuf; use std::{io, process}; -use clap::{Parser, Subcommand}; - /// mdbook preprocessor to add support for admonitions #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -26,9 +26,13 @@ enum Commands { /// Install the required assset files and include it in the config Install { /// Root directory for the book, should contain the configuration file (`book.toml`) + /// + /// If not set, defaults to the current directory. dir: Option<PathBuf>, /// Relative directory for the css assets, from the book directory root + /// + /// If not set, defaults to the current directory. #[arg(long)] css_dir: Option<PathBuf>, }, @@ -38,24 +42,26 @@ fn main() { env_logger::init_from_env(env_logger::Env::default().default_filter_or("info")); let cli = Cli::parse(); + if let Err(error) = run(cli) { + log::error!("Fatal error: {}", error); + for error in error.chain() { + log::error!(" - {}", error); + } + process::exit(1); + } +} +fn run(cli: Cli) -> Result<()> { match cli.command { - None => { - if let Err(e) = handle_preprocessing() { - eprintln!("{}", e); - process::exit(1); - } - } + None => handle_preprocessing(), Some(Commands::Supports { renderer }) => { handle_supports(renderer); } #[cfg(feature = "cli-install")] - Some(Commands::Install { dir, css_dir }) => { - install::handle_install( - dir.unwrap_or_else(|| PathBuf::from(".")), - css_dir.unwrap_or_else(|| PathBuf::from(".")), - ); - } + Some(Commands::Install { dir, css_dir }) => install::handle_install( + dir.unwrap_or_else(|| PathBuf::from(".")), + css_dir.unwrap_or_else(|| PathBuf::from(".")), + ), } } @@ -90,11 +96,11 @@ fn handle_supports(renderer: String) -> ! { #[cfg(feature = "cli-install")] mod install { + use anyhow::{Context, Result}; use std::{ fs::{self, File}, io::Write, path::PathBuf, - process, }; use toml_edit::{self, Array, Document, Item, Table, Value}; @@ -116,19 +122,14 @@ mod install { } } - pub fn handle_install(proj_dir: PathBuf, css_dir: PathBuf) { + pub fn handle_install(proj_dir: PathBuf, css_dir: PathBuf) -> Result<()> { let config = proj_dir.join("book.toml"); - - if !config.exists() { - log::error!("Configuration file '{}' missing", config.display()); - process::exit(1); - } - log::info!("Reading configuration file '{}'", config.display()); - let toml = fs::read_to_string(&config).expect("can't read configuration file"); + let toml = fs::read_to_string(&config) + .with_context(|| format!("can't read configuration file '{}'", config.display()))?; let mut doc = toml .parse::<Document>() - .expect("configuration is not valid TOML"); + .context("configuration is not valid TOML")?; if let Ok(preprocessor) = preprocessor(&mut doc) { const ASSETS_VERSION: &str = std::include_str!("./assets/VERSION"); @@ -144,7 +145,7 @@ mod install { let mut additional_css = additional_css(&mut doc); for (name, content) in ADMONISH_CSS_FILES { let filepath = proj_dir.join(&css_dir).join(name); - let filepath_str = filepath.to_str().expect("non-utf8 filepath"); + let filepath_str = filepath.to_str().context("non-utf8 filepath")?; if let Ok(ref mut additional_css) = additional_css { if !additional_css.contains_str(filepath_str) { @@ -159,18 +160,18 @@ mod install { "Copying '{name}' to '{filepath}'", filepath = filepath.display() ); - let mut file = File::create(filepath).expect("can't open file for writing"); + let mut file = File::create(&filepath).context("can't open file for writing")?; file.write_all(content) - .expect("can't write content to file"); + .context("can't write content to file")?; } let new_toml = doc.to_string(); if new_toml != toml { log::info!("Saving changed configuration to '{}'", config.display()); let mut file = - File::create(config).expect("can't open configuration file for writing."); + File::create(config).context("can't open configuration file for writing.")?; file.write_all(new_toml.as_bytes()) - .expect("can't write configuration"); + .context("can't write configuration")?; } else { log::info!("Configuration '{}' already up to date", config.display()); } @@ -180,8 +181,7 @@ mod install { A beautifully styled message. ```"#; log::info!("Add a code block like:\n{}", codeblock); - - process::exit(0); + Ok(()) } /// Return the `additional-css` field, initializing if required. |