Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication across binaries #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/bin/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
37 changes: 13 additions & 24 deletions src/bin/git-rsl-init.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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),
}
}
46 changes: 13 additions & 33 deletions src/bin/git-secure-fetch.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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),
}
}
47 changes: 14 additions & 33 deletions src/bin/git-secure-push.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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),
}
}