From 13710b49cc40c0e78eec9a94ce9b0ea8a09c3860 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 2 Aug 2020 00:47:53 +0200 Subject: git-sugpatch: Print error and exit on `Config::get` error Add `exitcode` to exit with an appropriate code. Add the `gseprintln` macro to wrap `eprintln!()`, prefixing the output with "error: ". --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/bin/git-sugpatch.rs | 12 +++++++++++- src/error.rs | 8 ++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 4be1b47..f0130fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + [[package]] name = "fnv" version = "1.0.7" @@ -285,6 +291,7 @@ dependencies = [ name = "github-suggestion-cli" version = "0.0.1" dependencies = [ + "exitcode", "getopts", "git2", "github-suggestion", diff --git a/Cargo.toml b/Cargo.toml index d0ce530..dccaa62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.0.1" edition = "2018" [dependencies] +exitcode = "1.1.2" getopts = "0.2.21" git2 = "0.13.8" regex = "1.3.9" diff --git a/src/bin/git-sugpatch.rs b/src/bin/git-sugpatch.rs index 04b687d..0159307 100644 --- a/src/bin/git-sugpatch.rs +++ b/src/bin/git-sugpatch.rs @@ -1,7 +1,10 @@ use std::env; use std::process; +use exitcode; + use github_suggestion::{Client, Suggestion, SuggestionUrl}; +use github_suggestion_cli::gseprintln; use github_suggestion_cli::config::Config; use github_suggestion_cli::error::Error; use github_suggestion_cli::is_suggestion_id; @@ -10,7 +13,14 @@ use github_suggestion_cli::is_suggestion_id; fn main() { let args: Vec<_> = env::args().collect(); - let config = Config::get(&args).unwrap(); + let config = match Config::get(&args) { + Ok(c) => c, + Err(e) => { + gseprintln!(e); + + process::exit(exitcode::DATAERR); + }, + }; if config.suggestions.is_empty() { process::exit(111); diff --git a/src/error.rs b/src/error.rs index c857bd2..bcca3e1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,3 +7,11 @@ pub enum Error { #[error("Unable to parse regex")] Regex(#[from] regex::Error), } + + +#[macro_export] +macro_rules! gseprintln { + ($arg:expr) => ({ + eprintln!("error: {}", $arg); + }) +} -- cgit v1.2.3