From 38a871f28bad90e238021d8cc46b9fa926f9df75 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 2 Aug 2020 04:31:11 +0200 Subject: git-sugpatch: Error if no remote and suggestion ID argument We want to allow not having a remote when URL arguments are given, but require it when a suggestion ID argument is given (otherwise we wouldn't have an owner-repo pair to make the GitHub request). Had some trouble with the `OwnerRepo.o_r` value. It was being moved into the closure, so tried a loop. There was a similar problem with the loop. However, by returning, I was able to get a reference to the `Result` instead of having it be moved. --- src/bin/git-sugpatch.rs | 158 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 115 insertions(+), 43 deletions(-) (limited to 'src/bin/git-sugpatch.rs') diff --git a/src/bin/git-sugpatch.rs b/src/bin/git-sugpatch.rs index 0159307..4560c9a 100644 --- a/src/bin/git-sugpatch.rs +++ b/src/bin/git-sugpatch.rs @@ -4,10 +4,9 @@ use std::process; use exitcode; use github_suggestion::{Client, Suggestion, SuggestionUrl}; -use github_suggestion_cli::gseprintln; +use github_suggestion_cli::{gseprintln, is_suggestion_id, owner_repo}; use github_suggestion_cli::config::Config; use github_suggestion_cli::error::Error; -use github_suggestion_cli::is_suggestion_id; fn main() { @@ -18,7 +17,7 @@ fn main() { Err(e) => { gseprintln!(e); - process::exit(exitcode::DATAERR); + process::exit(exitcode::CONFIG); }, }; @@ -26,52 +25,125 @@ fn main() { process::exit(111); } - let suggestions: Vec> = config.suggestions - .iter() - .map(|s| { - let suggestion = if is_suggestion_id(s)? { - let client = Client::new( - &config.github_token, - &config.owner, - &config.repo, - ).unwrap(); - - client.fetch(&s).unwrap() - } else { - let url: SuggestionUrl = args[1].parse().unwrap(); - - let client = Client::new( - &config.github_token, - &url.owner, - &url.repo, - ).unwrap(); - - client.fetch(&url.comment_id).unwrap() + // let mut owner_repo_error: owner_repo::Error; + // let o_r = match config.o_r { + // Ok(o_r) => o_r, + // Err(e @ owner_repo::Error::NoRemote(_)) => owner_repo_error = e, + // Err(e) => { + // gseprintln!(e); + // + // process::exit(111); + // }, + // }; + + for suggestion_arg in config.suggestions { + let suggestion = if match is_suggestion_id(&suggestion_arg) { + Ok(p) => p, + Err(e) => { + gseprintln!(e); + + process::exit(exitcode::SOFTWARE); + } + } { + // let o_r = match config.o_r { + // Ok(o_r) => o_r, + // Err(e) => { + // gseprintln!(e); + // + // process::exit(exitcode::CONFIG); + // } + // }; + // let o_r = match owner_repo { + // Ok(o_r) => o_r, + // Err(e) => { + // gseprintln!(e); + // + // process::exit(exitcode::CONFIG); + // }, + // }; + // if let owner_repo::Error::NoRemote(e) = owner_repo_error { + // gseprintln!(e); + // + // process::exit(exitcode::CONFIG); + // } + let o_r = match &config.o_r { + Ok(o_r) => o_r, + Err(e) => { + gseprintln!(e); + process::exit(exitcode::CONFIG); + }, }; - Ok(suggestion) - }) - .collect(); + let client = Client::new( + &config.github_token, + &o_r.owner, + &o_r.repo, + ).unwrap(); - let errors: Vec<&Error> = suggestions.iter() - .filter(|r| r.is_err()) + client.fetch(&suggestion_arg).unwrap() + } else { + let url: SuggestionUrl = args[1].parse().unwrap(); - // We know these `Results` are `Err`s. - .map(|r| r.as_ref().err().unwrap()) - .collect(); + let client = Client::new( + &config.github_token, + &url.owner, + &url.repo, + ).unwrap(); - if !errors.is_empty() { - for error in errors { - eprintln!("error: {}", error); - } + client.fetch(&url.comment_id).unwrap() + }; - return; + print!("{}", suggestion.diff().unwrap()); } - suggestions - .iter() - - // We've already checked for `Err`s above. - .map(|r| r.as_ref().unwrap()) - .for_each(|s| print!("{}", s.diff().unwrap())); + // let suggestions: Vec> = config.suggestions + // .iter() + // .map(|s| { + // let suggestion = if is_suggestion_id(s)? { + // let o_r = owner_repo?; + // + // let client = Client::new( + // &config.github_token, + // &o_r.owner, + // &o_r.repo, + // ).unwrap(); + // + // client.fetch(&s).unwrap() + // } else { + // let url: SuggestionUrl = args[1].parse().unwrap(); + // + // let client = Client::new( + // &config.github_token, + // &url.owner, + // &url.repo, + // ).unwrap(); + // + // client.fetch(&url.comment_id).unwrap() + // }; + // + // Ok(suggestion) + // }) + // .collect(); + // + // let errors: Vec<&Error> = suggestions.iter() + // .filter(|r| r.is_err()) + // + // // We know these `Results` are `Err`s. + // .map(|r| r.as_ref().err().unwrap()) + // .collect(); + // + // if !errors.is_empty() { + // for error in errors { + // eprintln!("error: {}", error); + // } + // + // return; + // } + // + // suggestions + // .iter() + // + // // We've already checked for `Err`s above. + // .map(|r| r.as_ref().unwrap()) + // .for_each(|s| print!("{}", s.diff().unwrap())); } -- cgit v1.2.3