diff options
author | Teddy Wing | 2020-08-02 10:43:54 +0200 |
---|---|---|
committer | Teddy Wing | 2020-08-02 10:43:54 +0200 |
commit | 48e4130e825de244592d7cf0631d3b0552d42ef1 (patch) | |
tree | e6d2a5b5fba0bdf4b6a9cb406c7b6f00679575f1 /src | |
parent | 20e98432afb02262703a2bd4524e6a87f3145086 (diff) | |
download | git-suggestion-48e4130e825de244592d7cf0631d3b0552d42ef1.tar.bz2 |
Move suggestion arguments check to `Config::get`
Since this check for suggestion arguments is in both binaries, we can
move it to the common preflight function.
Clean up other areas now that we moved the function.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/git-sugapply.rs | 6 | ||||
-rw-r--r-- | src/bin/git-sugpatch.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 28 | ||||
-rw-r--r-- | src/suggestion.rs | 2 |
4 files changed, 15 insertions, 27 deletions
diff --git a/src/bin/git-sugapply.rs b/src/bin/git-sugapply.rs index 96e4423..18d670c 100644 --- a/src/bin/git-sugapply.rs +++ b/src/bin/git-sugapply.rs @@ -22,12 +22,6 @@ fn main() { }, }; - if config.suggestions.is_empty() { - config.print_usage(); - - process::exit(exitcode::USAGE); - } - for_suggestion( &config, |suggestion| { diff --git a/src/bin/git-sugpatch.rs b/src/bin/git-sugpatch.rs index 10499e9..58d9a04 100644 --- a/src/bin/git-sugpatch.rs +++ b/src/bin/git-sugpatch.rs @@ -22,12 +22,6 @@ fn main() { }, }; - if config.suggestions.is_empty() { - config.print_usage(); - - process::exit(exitcode::USAGE); - } - for_suggestion( &config, |suggestion| { diff --git a/src/config.rs b/src/config.rs index 07768f0..e605439 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,17 +28,14 @@ pub enum Error { Git(#[from] git2::Error), } -pub struct Config<'a> { +pub struct Config { pub github_token: String, pub o_r: Result<OwnerRepo, owner_repo::Error>, pub suggestions: Vec<String>, - - opts: Options, - usage_brief: &'a str, } -impl<'a> Config<'a> { - pub fn get(args: &[String], usage_brief: &'a str) -> Result<Self, Error> { +impl Config { + pub fn get(args: &[String], usage_brief: &str) -> Result<Self, Error> { let mut opts = Options::new(); opts.optopt( @@ -58,7 +55,13 @@ impl<'a> Config<'a> { let opt_matches = opts.parse(&args[1..])?; if opt_matches.opt_present("h") { - print!("{}", opts.usage(&usage_brief)); + print_usage(&opts, usage_brief); + + process::exit(exitcode::USAGE); + } + + if opt_matches.free.is_empty() { + print_usage(&opts, usage_brief); process::exit(exitcode::USAGE); } @@ -73,16 +76,9 @@ impl<'a> Config<'a> { github_token: Self::github_token(&opt_matches, &git_config)?, o_r: o_r, suggestions: opt_matches.free, - - opts: opts, - usage_brief, }) } - pub fn print_usage(&self) { - print!("{}", self.opts.usage(&self.usage_brief)) - } - fn github_token( opt_matches: &getopts::Matches, git_config: &git2::Config, @@ -119,6 +115,10 @@ impl<'a> Config<'a> { } } +fn print_usage(opts: &Options, brief: &str) { + print!("{}", opts.usage(brief)); +} + fn git_config_key(key: &str) -> String { format!("{}.{}", GIT_CONFIG_PREFIX, key) } diff --git a/src/suggestion.rs b/src/suggestion.rs index f1684e0..fc22a7c 100644 --- a/src/suggestion.rs +++ b/src/suggestion.rs @@ -9,7 +9,7 @@ use crate::arg::is_suggestion_id; use crate::config::Config; -pub fn for_suggestion<F>(config: &Config<'_>, f: F) +pub fn for_suggestion<F>(config: &Config, f: F) where F: Fn(&Suggestion) { for suggestion_arg in &config.suggestions { |