chore: Upgrade proc-exit

This commit is contained in:
Ed Page 2022-10-04 10:46:45 -05:00
parent 17a90d814a
commit 3598acea5d
3 changed files with 54 additions and 32 deletions

4
Cargo.lock generated
View file

@ -1121,9 +1121,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-exit" name = "proc-exit"
version = "1.0.3" version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0da6bbc8ef87314d4f596ad9d02db375c3f2d77fba91067a6f6a5754fdc8cb49" checksum = "d2d778539881515d37cd91925d169f4a351120c5a1b44fce2b7c462b0d7f4ec6"
[[package]] [[package]]
name = "proc-macro-error" name = "proc-macro-error"

View file

@ -73,7 +73,7 @@ bstr = "1.0"
once_cell = "1.15.0" once_cell = "1.15.0"
ahash = "0.8" ahash = "0.8"
difflib = "0.4" difflib = "0.4"
proc-exit = "1.0" proc-exit = "2.0"
human-panic = "1.0.3" human-panic = "1.0.3"
content_inspector = "0.2.4" content_inspector = "0.2.4"
unicode-segmentation = "1.10.0" unicode-segmentation = "1.10.0"

View file

@ -5,7 +5,7 @@ use clap::Parser;
mod args; mod args;
mod report; mod report;
use proc_exit::WithCodeResultExt; use proc_exit::prelude::*;
fn main() { fn main() {
human_panic::setup_panic!(); human_panic::setup_panic!();
@ -19,7 +19,7 @@ fn run() -> proc_exit::ExitResult {
Ok(args) => args, Ok(args) => args,
Err(e) if e.use_stderr() => { Err(e) if e.use_stderr() => {
let _ = e.print(); let _ = e.print();
return proc_exit::Code::USAGE_ERR.ok(); return proc_exit::sysexits::USAGE_ERR.ok();
} }
Err(e) => { Err(e) => {
let _ = e.print(); let _ = e.print();
@ -52,19 +52,24 @@ fn run() -> proc_exit::ExitResult {
} }
fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exit::ExitResult { fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir()?; let global_cwd = std::env::current_dir().to_sysexits()?;
let path = &args.path[0]; let path = &args.path[0];
let cwd = if path == std::path::Path::new("-") { let cwd = if path == std::path::Path::new("-") {
global_cwd global_cwd
} else if path.is_file() { } else if path.is_file() {
let mut cwd = path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; let mut cwd = path
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
cwd.pop(); cwd.pop();
cwd cwd
} else { } else {
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? path.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?
}; };
let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; let cwd = cwd
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
let storage = typos_cli::policy::ConfigStorage::new(); let storage = typos_cli::policy::ConfigStorage::new();
let mut engine = typos_cli::policy::ConfigEngine::new(&storage); let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
@ -72,8 +77,8 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
let mut overrides = typos_cli::config::Config::default(); let mut overrides = typos_cli::config::Config::default();
if let Some(path) = args.custom_config.as_ref() { if let Some(path) = args.custom_config.as_ref() {
let custom = let custom = typos_cli::config::Config::from_file(path)
typos_cli::config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom); overrides.update(&custom);
} }
overrides.update(&args.config.to_config()); overrides.update(&args.config.to_config());
@ -81,35 +86,42 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
let config = engine let config = engine
.load_config(&cwd) .load_config(&cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
let mut defaulted_config = typos_cli::config::Config::from_defaults(); let mut defaulted_config = typos_cli::config::Config::from_defaults();
defaulted_config.update(&config); defaulted_config.update(&config);
let output = let output =
toml_edit::easy::to_string_pretty(&defaulted_config).with_code(proc_exit::Code::FAILURE)?; toml_edit::easy::to_string_pretty(&defaulted_config).with_code(proc_exit::Code::FAILURE)?;
if output_path == std::path::Path::new("-") { if output_path == std::path::Path::new("-") {
std::io::stdout().write_all(output.as_bytes())?; std::io::stdout()
.write_all(output.as_bytes())
.to_sysexits()?;
} else { } else {
std::fs::write(output_path, &output)?; std::fs::write(output_path, &output).to_sysexits()?;
} }
Ok(()) Ok(())
} }
fn run_type_list(args: &args::Args) -> proc_exit::ExitResult { fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir()?; let global_cwd = std::env::current_dir().to_sysexits()?;
let path = &args.path[0]; let path = &args.path[0];
let cwd = if path == std::path::Path::new("-") { let cwd = if path == std::path::Path::new("-") {
global_cwd global_cwd
} else if path.is_file() { } else if path.is_file() {
let mut cwd = path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; let mut cwd = path
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
cwd.pop(); cwd.pop();
cwd cwd
} else { } else {
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? path.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?
}; };
let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; let cwd = cwd
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
let storage = typos_cli::policy::ConfigStorage::new(); let storage = typos_cli::policy::ConfigStorage::new();
let mut engine = typos_cli::policy::ConfigEngine::new(&storage); let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
@ -117,8 +129,8 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
let mut overrides = typos_cli::config::Config::default(); let mut overrides = typos_cli::config::Config::default();
if let Some(path) = args.custom_config.as_ref() { if let Some(path) = args.custom_config.as_ref() {
let custom = let custom = typos_cli::config::Config::from_file(path)
typos_cli::config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom); overrides.update(&custom);
} }
overrides.update(&args.config.to_config()); overrides.update(&args.config.to_config());
@ -126,13 +138,13 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
engine engine
.init_dir(&cwd) .init_dir(&cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
let definitions = engine.file_types(&cwd); let definitions = engine.file_types(&cwd);
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let mut handle = stdout.lock(); let mut handle = stdout.lock();
for (name, globs) in definitions { for (name, globs) in definitions {
writeln!(handle, "{}: {}", name, itertools::join(globs, ", "))?; writeln!(handle, "{}: {}", name, itertools::join(globs, ", ")).to_sysexits()?;
} }
Ok(()) Ok(())
@ -143,7 +155,7 @@ fn run_checks(
stdout_palette: report::Palette, stdout_palette: report::Palette,
stderr_palette: report::Palette, stderr_palette: report::Palette,
) -> proc_exit::ExitResult { ) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir()?; let global_cwd = std::env::current_dir().to_sysexits()?;
let storage = typos_cli::policy::ConfigStorage::new(); let storage = typos_cli::policy::ConfigStorage::new();
let mut engine = typos_cli::policy::ConfigEngine::new(&storage); let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
@ -151,8 +163,8 @@ fn run_checks(
let mut overrides = typos_cli::config::Config::default(); let mut overrides = typos_cli::config::Config::default();
if let Some(path) = args.custom_config.as_ref() { if let Some(path) = args.custom_config.as_ref() {
let custom = let custom = typos_cli::config::Config::from_file(path)
typos_cli::config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom); overrides.update(&custom);
} }
overrides.update(&args.config.to_config()); overrides.update(&args.config.to_config());
@ -164,16 +176,19 @@ fn run_checks(
let cwd = if path == std::path::Path::new("-") { let cwd = if path == std::path::Path::new("-") {
global_cwd.clone() global_cwd.clone()
} else if path.is_file() { } else if path.is_file() {
let mut cwd = path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; let mut cwd = path
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
cwd.pop(); cwd.pop();
cwd cwd
} else { } else {
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? path.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?
}; };
engine engine
.init_dir(&cwd) .init_dir(&cwd)
.with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
let walk_policy = engine.walk(&cwd); let walk_policy = engine.walk(&cwd);
let threads = if path.is_file() { 1 } else { args.threads }; let threads = if path.is_file() { 1 } else { args.threads };
@ -193,9 +208,11 @@ fn run_checks(
for pattern in walk_policy.extend_exclude.iter() { for pattern in walk_policy.extend_exclude.iter() {
overrides overrides
.add(&format!("!{}", pattern)) .add(&format!("!{}", pattern))
.with_code(proc_exit::Code::CONFIG_ERR)?; .with_code(proc_exit::sysexits::CONFIG_ERR)?;
} }
let overrides = overrides.build().with_code(proc_exit::Code::CONFIG_ERR)?; let overrides = overrides
.build()
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
walk.overrides(overrides); walk.overrides(overrides);
} }
@ -234,7 +251,12 @@ fn run_checks(
} }
.map_err(|e| { .map_err(|e| {
e.io_error() e.io_error()
.map(|i| proc_exit::Code::from(i.kind())) .map(|i| {
let kind = i.kind();
proc_exit::sysexits::io_to_sysexists(kind)
.or_else(|| proc_exit::bash::io_to_signal(kind))
.unwrap_or(proc_exit::sysexits::IO_ERR)
})
.unwrap_or_default() .unwrap_or_default()
.with_message(e) .with_message(e)
})?; })?;
@ -253,7 +275,7 @@ fn run_checks(
// `Failure` from something else and get it mixed up with typos. // `Failure` from something else and get it mixed up with typos.
// //
// Can't use DataErr or anything else an std::io::ErrorKind might map to. // Can't use DataErr or anything else an std::io::ErrorKind might map to.
proc_exit::Code::UNKNOWN.ok() proc_exit::Code::new(2).ok()
} else { } else {
proc_exit::Code::SUCCESS.ok() proc_exit::Code::SUCCESS.ok()
} }