diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs new file mode 100644 index 0000000..49de3f7 --- /dev/null +++ b/src/bin/cli/mod.rs @@ -0,0 +1,37 @@ +use clap::ArgMatches; +use git2::Repository; +use git_rsl::errors::*; +use git_rsl::utils::git; +use git_rsl::{BranchName, RemoteName}; +use std::process; + +pub fn collect_args<'a>( + matches: &'a ArgMatches, +) -> Result<(RemoteName<'a>, BranchName<'a>, Repository)> { + let remote = match matches.value_of("REMOTE") { + None => bail!("Must supply a REMOTE argument"), + Some(v) => RemoteName::new(v), + }; + + let branch = match matches.value_of("BRANCH") { + None => bail!("Must supply a BRANCH argument"), + Some(v) => BranchName::new(v), + }; + + let repo = match git::discover_repo() { + Err(_) => { + bail!("You don't appear to be in a git project. Please check yourself and try again") + } + Ok(repo) => repo, + }; + + Ok((remote, branch, repo)) +} + +pub fn handle_error(e: &Error) -> () { + report_error(&e); + match *e { + Error(ErrorKind::ReadError(_), _) => process::exit(1), + Error(_, _) => process::exit(2), + } +} diff --git a/src/bin/git-rsl-init.rs b/src/bin/git-rsl-init.rs index f6dae6d..865b08d 100644 --- a/src/bin/git-rsl-init.rs +++ b/src/bin/git-rsl-init.rs @@ -1,15 +1,16 @@ #[macro_use] extern crate clap; +#[macro_use] +extern crate error_chain; extern crate git2; extern crate git_rsl; -use std::process; +mod cli; use clap::{App, Arg}; -use git_rsl::errors::*; -use git_rsl::utils::git; -use git_rsl::{rsl_init_with_cleanup, RemoteName}; +use cli::{collect_args, handle_error}; +use git_rsl::rsl_init_with_cleanup; fn main() { let matches = App::new("git-rsl-init") @@ -23,25 +24,13 @@ fn main() { .author(crate_authors!()) .get_matches(); - let remote = match matches.value_of("REMOTE") { - None => panic!("Must supply a REMOTE argument"), - Some(v) => v.to_owned(), + match collect_args(&matches) { + Ok((remote, _, mut repo)) => { + if let Err(ref e) = rsl_init_with_cleanup(&mut repo, &remote) { + cli::handle_error(e); + } + println!("Success!") + } + Err(ref e) => handle_error(e), }; - // TODO - reduce code duplication across the top level of the binaries - let mut repo = git::discover_repo() - .expect("You don't appear to be in a git project. Please check yourself and try again"); - - if let Err(ref e) = rsl_init_with_cleanup(&mut repo, &RemoteName::new(&remote)) { - handle_error(e); - process::exit(1); - } - println!("Success!") -} - -fn handle_error(e: &Error) -> () { - report_error(&e); - match *e { - Error(ErrorKind::ReadError(_), _) => process::exit(1), - Error(_, _) => process::exit(2), - } } diff --git a/src/bin/git-secure-fetch.rs b/src/bin/git-secure-fetch.rs index 19add6c..9c4155e 100644 --- a/src/bin/git-secure-fetch.rs +++ b/src/bin/git-secure-fetch.rs @@ -1,15 +1,16 @@ #[macro_use] extern crate clap; +#[macro_use] +extern crate error_chain; extern crate git2; extern crate git_rsl; -use std::process; +mod cli; use clap::{App, Arg}; -use git_rsl::errors::*; -use git_rsl::utils::git; -use git_rsl::{secure_fetch_with_cleanup, BranchName, RemoteName}; +use cli::{collect_args, handle_error}; +use git_rsl::secure_fetch_with_cleanup; fn main() { let matches = App::new("git-secure-fetch") @@ -27,34 +28,13 @@ fn main() { .author(crate_authors!()) .get_matches(); - let remote = match matches.value_of("REMOTE") { - None => panic!("Must supply a REMOTE argument"), - Some(v) => v.to_owned(), - }; - - let branch = match matches.value_of("BRANCH") { - None => panic!("Must supply a BRANCH argument"), - Some(v) => v.to_owned(), + match collect_args(&matches) { + Ok((remote, branch, mut repo)) => { + if let Err(ref e) = secure_fetch_with_cleanup(&mut repo, &remote, &branch) { + cli::handle_error(e); + } + println!("Success!") + } + Err(ref e) => handle_error(e), }; - // TODO - reduce code duplication across the top level of the binaries - let mut repo = git::discover_repo() - .expect("You don't appear to be in a git project. Please check yourself and try again"); - - if let Err(ref e) = secure_fetch_with_cleanup( - &mut repo, - &RemoteName::new(&remote), - &BranchName::new(&branch), - ) { - handle_error(e); - process::exit(1); - } - println!("Success!") -} - -fn handle_error(e: &Error) -> () { - report_error(&e); - match *e { - Error(ErrorKind::ReadError(_), _) => process::exit(1), - Error(_, _) => process::exit(2), - } } diff --git a/src/bin/git-secure-push.rs b/src/bin/git-secure-push.rs index 56d9ebe..9af6423 100644 --- a/src/bin/git-secure-push.rs +++ b/src/bin/git-secure-push.rs @@ -1,14 +1,16 @@ #[macro_use] extern crate clap; +#[macro_use] +extern crate error_chain; extern crate git2; extern crate git_rsl; +mod cli; + use clap::{App, Arg}; -use git_rsl::errors::*; -use git_rsl::utils::git; -use git_rsl::{secure_push_with_cleanup, BranchName, RemoteName}; -use std::process; +use cli::{collect_args, handle_error}; +use git_rsl::secure_push_with_cleanup; fn main() { let matches = App::new("git-secure-push") @@ -26,34 +28,13 @@ fn main() { .author(crate_authors!()) .get_matches(); - let remote = match matches.value_of("REMOTE") { - None => panic!("Must supply a REMOTE argument"), - Some(v) => v.to_owned(), - }; - - let branch = match matches.value_of("BRANCH") { - None => panic!("Must supply a BRANCH argument"), - Some(v) => v.to_owned(), + match collect_args(&matches) { + Ok((remote, branch, mut repo)) => { + if let Err(ref e) = secure_push_with_cleanup(&mut repo, &remote, &branch) { + cli::handle_error(e); + } + println!("Success!") + } + Err(ref e) => handle_error(e), }; - // TODO - reduce code duplication across the top level of the binaries - let mut repo = git::discover_repo() - .expect("You don't appear to be in a git project. Please check yourself and try again"); - - if let Err(ref e) = secure_push_with_cleanup( - &mut repo, - &RemoteName::new(&remote), - &BranchName::new(&branch), - ) { - handle_error(e); - process::exit(1); - } - println!("Success!") -} - -fn handle_error(e: &Error) -> () { - report_error(&e); - match *e { - Error(ErrorKind::ReadError(_), _) => process::exit(1), - Error(_, _) => process::exit(2), - } }